0
我想比較日期選擇器格式爲「dd/mm/yyyy」的日期與系統的當前日期,並應用操作currentdate.before(pickeddate)
。我會怎麼做?比較日期選擇器日期和當前日期並應用before()函數
我想比較日期選擇器格式爲「dd/mm/yyyy」的日期與系統的當前日期,並應用操作currentdate.before(pickeddate)
。我會怎麼做?比較日期選擇器日期和當前日期並應用before()函數
您可以獲得當前日期有:
String pattern = "MM-dd-yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date date = format.parse("01-07-2013");
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
}
http://kiddyandroid.blogspot.in/2016/03/date-time-in-android.html
獲取當前日期:
private SimpleDateFormat dfDate = new SimpleDateFormat("yyyy-MM-dd");
public String getCurrentDate() {
int yr, month, day, hour;
Calendar today = Calendar.getInstance();
yr = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);
hour = today.get(Calendar.HOUR_OF_DAY);
return (yr + "-" + getLength(month + 1) + "-" + getLength(day));
}
public static String getLength(int i) {
if (i > 9)
return "" + i;
return "0" + i;
}
比較當前日期機智h日期選取日期:
String fromDate = yr + "-" + month + "-" + day;
public boolean fromDateCheck(String fromDate) {
try {
String toDate = 12-02-2013
Date toDATE = dfDate.parse(toDate); // dfDate --> Format of Date
Date fromDATE = dfDate.parse(fromDate);
if (fromDATE.before(toDATE) || fromDATE.compareTo(toDATE) == 0) {
// Do the things if FromDate(Current Date) and To Date are same or FromDate is not a Future date
return true;
} else if (fromDATE.after(toDATE)) {
Toast.makeText(this.context, "Invalid Date",Toast.LENGTH_SHORT).show();
return false;
} else {
Toast.makeText(this.context, "Choose Correct Data", Toast.LENGTH_SHORT).show();
return false;
}
} catch (Exception e) {
}
return false;
}