我有可以采用两种不同类型的日期格式的方法:
- MM/YY(信用卡到期日期)
- yyyyMMdd(资金到期日期)
信用卡
过期日期被视为在该月的最后一天过期。因此,如果抄送日期是 2017 年 5 月 (05/17),则该抄送将被视为在 5 月 31 日过期。
资金到期日期将在其规定的到期日到期。因此,如果我在同一天查看它,它应该返回 TRUE,因为资金已过期。
这是我的代码:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public static boolean dateHasExpired(String dateInput)
{
LocalDate d = LocalDate.now();
LocalDate dateParsed = null;
if (dateInput.contains("/"))
{
int iYear = Integer.parseInt(dateInput.substring(dateInput.indexOf("/") + 1));
int iMonth = Integer.parseInt(dateInput.substring(0, dateInput.indexOf("/")));
int daysInMonth = LocalDate.of(iYear, iMonth, 1).getMonth().maxLength();
dateInput = iMonth+"/"+daysInMonth+"/"+iYear;
}
else
{
dateInput = ConvertDate(dateInput, "yyyyMMdd", "MM/dd/yyyy");
}
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
dateParsed = LocalDate.parse(dateInput, dateTimeFormatter);
return d.compareTo(dateParsed) <= 0;
}
public static String ConvertDate(String dateValue, String currentFormat, String requiredFormat)
{
SimpleDateFormat inFormatter = new SimpleDateFormat(currentFormat);
SimpleDateFormat outFormatter = new SimpleDateFormat(requiredFormat);
String outDate = "";
try
{
java.util.Date date = inFormatter.parse(dateValue);
outDate = outFormatter.format(date);
}
catch (ParseException e) {
ErrorLogger.logError ( e );
}
return outDate;
}
有谁知道更好的方法吗?
我还注意到 LocalDate
没有考虑闰年
,因此 2015 年 2 月有 29 天,就像 2016 年 2 月一样,所以我的 daysInMonth
不会是一个好数字。
当涉及到年份为 yy 和月份 5 为 5 月时,看起来 Date 比 LocalDate 更宽容。
最佳答案
您可以使用java.time.YearMonth
类,它包含一个返回相应月份最后一天的方法(并且还处理闰年):
public static boolean dateHasExpired(String dateInput) {
LocalDate today = LocalDate.now();
LocalDate dateParsed = null;
if (dateInput.contains("/")) {
// parse credit card expiration date
YearMonth ym = YearMonth.parse(dateInput, DateTimeFormatter.ofPattern("MM/yy"));
// get last day of month (taking care of leap years)
dateParsed = ym.atEndOfMonth();
} else {
// parse funding expiration date
dateParsed = LocalDate.parse(dateInput, DateTimeFormatter.ofPattern("yyyyMMdd"));
}
// expired if today is equals or after dateParsed
return ! today.isBefore(dateParsed);
}
使用此代码(考虑到今天是2017 年 5 月 2 日):
System.out.println(dateHasExpired("04/17")); // true
System.out.println(dateHasExpired("05/17")); // false
System.out.println(dateHasExpired("06/17")); // false
System.out.println(dateHasExpired("20170501")); //true
System.out.println(dateHasExpired("20170502")); // true
System.out.println(dateHasExpired("20170503")); // false
请注意,atEndOfMonth()
方法会处理闰年,因此这些也将起作用:
System.out.println(dateHasExpired("02/15"));
System.out.println(dateHasExpired("02/16"));
我在dateHasExpired
方法中添加了System.out.println(dateParsed);
,只是为了检查日期是否被正确解析。上述日期的输出(分别)为:
2015-02-28
2016-02-29
并且 dateHasExpired
按预期返回 true
。
关于java - 将信用卡日期 MM/yy 格式化为 MM/dd/yyyy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43745565/