简体   繁体   中英

Java: How to compare two Times/Dates

I am retrieving value from my notepad(database) like this

            strLine = br.readLine().trim();
            if ((strLine.length()!=0) && (strLine.charAt(0)!='#')) {
            String[] teachers = strLine.split("\\s+");
             Interviews interview = new Interviews();
             interview.setTime(teachers[4]+" "+ teachers[5]);

at this point my timevalue is ( given be debugger)

             "2011-9-5 0:00"

I want this Time to be compare with some other Time

But when i try to parse these values in a date format

              SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
               Date d1=df.parse(interviewList.get(0).getTime());

It gives me error

             java.text.ParseException: Unparseable date: "2011-9-5 0:00"

This is the format i have in my Notepad (Database) 2011-9-5 0:00

How can i compare this value as a Time with some other Time

Thanks

This is the format you are looking for. It will parse both single/double digit month/day yyyy-MM-dd hh:mm . Notice that mm means minutes so you should use MM for months.

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
System.out.println(df.parse("2011-9-5 9:00"));

prints

Mon Sep 05 09:00:00 EDT 2011

Verified this with jdk 1.6. Now, I doesn't see you comparing dates anywhere. You can use Calendar 's after() and before() methods or you can call getTime() on Date and use < or > (faster). It all depends upon what you are comparing for.

Here's an example of how to do this:

/**
 * Date difference, as minutes (abs)
 */

public class DateDiff {

  static SimpleDateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");

  private String sD1;
  private String sD2;

  public DateDiff(String sD1, String sD2) {
    this.sD1=sD1;
    this.sD2=sD2;
  }
  /**
   * Parse date strings in format like: 2005-01-01 22:10:35
   */
  public long getDifferenceInMinutes () throws ParseException {
    Date d1=df.parse(sD1);
    Date d2=df.parse(sD2);
    long d1Ms=d1.getTime();
    long d2Ms=d2.getTime();
    return Math.abs((d1Ms-d2Ms)/60000);
  }
  /**
   ***************** MAIN *******************
   * @param args
   */
  public static void main(String[] args) {
    DateDiff dd = new DateDiff("2005-01-01 22:10:35",
                               "2005-01-01 23:11:35");
    try {
      long diff=dd.getDifferenceInMinutes();
      System.out.println("Time difference (abs): " + diff);
    }
    catch (ParseException ex) {
      ex.printStackTrace();
    }
  }
}

Your date should be in yyyy-MM-dd hh:mm:ss format.

Once your date is formatted like above, you can get date object

 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");             
    Date d1=df.parse(dateToPars); 

d1.after(otherDateYouWantTocompare);  OR 
d1.before(otherDateyouWantToCompare); 

EDITED after Pangea validation.

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