Date

如何處理日期?

API

處理範例

檢查是否為今日

輸入為String

使用的API:

import java.util.Date;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

程式碼:

String endDate = "20160909";

String yesterDate = endDate;
boolean isTodayFlag = false;
DateFormat format1 = new SimpleDateFormat("yyyyMMdd");
Date d = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(d);
String formatted = format1.format(cal.getTime());
if(formatted.equals(endDate)){
  isTodayFlag = true;
  cal.add(Calendar.DATE, -1);
  yesterDate = format1.format(cal.getTime());
}

if(isTodayFlag){
  System.out.println("is Today");
}
System.out.println(isTodayFlag);
System.out.println(endDate);
System.out.println(yesterDate);

results matching ""

    No results matching ""