簡體   English   中英

將信用卡日期MM / yy格式化為MM / dd / yyyy

[英]Format credit card date MM/yy to MM/dd/yyyy

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

  • MM / YY (信用卡有效期)
  • yyyyMMdd (資金到期日期)

Credit card到期日被視為在該月的最后一天到期。 因此,如果抄送日期為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不代表daysInMonth Leap Year ,因此2015 Leap Year 2月有29天,就像2016年2月一樣,所以我的daysInMonth並不是一個好數字。

看起來當日期為yy和5月的第5個月時,日期比LocalDate更寬容。

您可以使用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"));

我添加了一個System.out.println(dateParsed); dateHasExpired方法中,只是檢查日期是否被正確解析。 以上日期的輸出分別是:

2015-02-28
2016-02-29

dateHasExpired兩者都返回true ,正如預期的那樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM