简体   繁体   中英

Comparison of Date and Time

I have to get count of days which are past to the current day.I have list of days in arraylist.I got the list and I dont know how to compare?Can anyone help me?

This is the code I tried,

private void weeklylogeval(){
    int i;
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
dateFormatter.setLenient(false);
Date today = new Date();
String s = dateFormatter.format(today);
System.out.println("current date & time new:::"+s);
for(i=0;i<datetime.size();i++){
String daytime=datetime.get(i);
if(today.before(daytime))
}
}

Pls some one help me!

http://developer.android.com/reference/java/util/Calendar.html

This should be allot easier to use for your purpose Edit:

Methods you can use:

boolean after(Object calendar) Returns whether the Date represented by this Calendar instance is after the Date represented by the parameter. boolean before(Object calendar) Returns whether the Date represented by this Calendar instance is before the Date represented by the parameter.

也许您可以使用从数据库获得的字符串构造一个Date,然后使用today.before(daytime)进行比较。

Date daytime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(datetime.get(i));

Try this code for date difference manipulation.

String fd=from_date;//date get from mysql database as string.
String td=to_date;//Today's date as string.
if(!fd.equalsIgnoreCase("") && !td.equalsIgnoreCase("")){
       SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
           SimpleDateFormat formatter;
           Date frmdt=new Date(fd);
           formatter = new SimpleDateFormat("dd/MM/yyyy");
           String s1 = formatter.format(frmdt);
           Date todt=new Date(td);
           String s2 = formatter.format(todt);
           Date frmdate = sdf.parse(s1);
           Date todate = sdf.parse(s2);
                  if(frmdate.compareTo(todate)<=0) {
                                //do your stuff
                     } else {
                                // do your stuff
                     }
             }

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