我想以格式" dd / mm / yyyy"来比较来自日期选择器的日期。针对系统的当前日期,并应用操作currentdate.before(pickeddate)
。我该怎么做?
答案 0 :(得分:0)
您可以使用以下内容获取当前日期
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();
}
答案 1 :(得分:0)
<强> 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;
}
将当前日期与日期选择日期进行比较:
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;
}