简体   繁体   中英

How to compare Date + Time with current date and time?

I have user selected date and time in string

2016-11-05 09:30 AM

I want to compare this date time with currect date time.

I used .compareTo() method along with System.getTimeInMilliseconds() but it is not working if i use current year.

I have used following code but it goes in else block if user selects date with current year.

SimpleDateFormat sdf = new     SimpleDateFormat("yyyy-MM-dd K:mm",Local.getDefault());
Date date = sdf.parse("2016-09-07 04:11 am");
    if(date.compareTo(System.currentTimeInMillis) > 0){
Log.d("date","Date in future");
}
else
{
    Log.d("date","Date in past");
}

Try this

DateComparedateTime(String date1)
{
    System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm aa").format(date1).compareTo(new Date()))
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM