将信用卡日期 MM/yy 格式化为 MM/dd/yyyy
Format credit card date MM/yy to MM/dd/yyyy
我有方法可以采用两种不同类型的日期格式:
- MM/YY(信用卡有效期)
- yyyyMMdd(资金到期日)
Credit card
过期日期被视为在该月的最后一天过期。因此,如果抄送日期是 2017 年 5 月 (05/17),则此抄送将被视为在 5 月 31 日过期。
资金到期日将在其显示的到期日到期。所以,如果我在同一天查看它,它应该 return 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
没有考虑 Leap Year
,所以 2015 年 2 月有 29 天,就像 2016 年 2 月一样,所以我的 daysInMonth
不会是一个好数字。
当年份为 yy 且月份为 5 为 5 月时,看起来 Date 比 LocalDate 更宽容。
您可以使用 java.time.YearMonth
class,其中包含一个方法 returns 相应月份的最后一天(并且还处理闰年):
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
returns true
,如预期。
我有方法可以采用两种不同类型的日期格式:
- MM/YY(信用卡有效期)
- yyyyMMdd(资金到期日)
Credit card
过期日期被视为在该月的最后一天过期。因此,如果抄送日期是 2017 年 5 月 (05/17),则此抄送将被视为在 5 月 31 日过期。
资金到期日将在其显示的到期日到期。所以,如果我在同一天查看它,它应该 return 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
没有考虑 Leap Year
,所以 2015 年 2 月有 29 天,就像 2016 年 2 月一样,所以我的 daysInMonth
不会是一个好数字。
当年份为 yy 且月份为 5 为 5 月时,看起来 Date 比 LocalDate 更宽容。
您可以使用 java.time.YearMonth
class,其中包含一个方法 returns 相应月份的最后一天(并且还处理闰年):
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
returns true
,如预期。