格式信用卡日期mm/yy至mm/dd/yyyy



我有可以采用2种不同类型的日期格式的方法:

  • mm/yy (信用卡到期日期(
  • yyyymmdd (资金到期日期(

Credit card到期日期被认为在该月的最后一天过期。因此,如果CC日期是2017年5月(05/17(,则该CC将于5月31日到期。

资金到期日期将在其说已过期的那一天到期。因此,如果我在同一天看它,它应该在资金过期后返回真实。

这是我的代码:

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不是一个好数。

看来,约会比Localdate更为平整,而5月的年度为YY和第5个月。

您可以使用java.time.YearMonth类,其中包含一种返回各个月最后一天的方法(并且还照顾Leap年(:

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()方法会照顾LEAP年,因此这些方法也将起作用:

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,正如预期的。

最新更新