Calculate Date/Time Difference in Java considering AM/PM

前端 未结 6 553
陌清茗
陌清茗 2020-12-16 04:33

I want to calculate the difference between two date/time in java using Date and Calendar classes. The format that I have is \"2012-01-24 12:30:00 PM\".

I have implem

相关标签:
6条回答
  • 2020-12-16 05:23

    Don't build it yourself, use an established library. Jodatime is used widely.

    0 讨论(0)
  • 2020-12-16 05:28

    Calendar has a methode add() that can be used for substracting too. Take a look at it. You should be using Calendar instead of Date anyway because most of the Date operations are deprecated.

    0 讨论(0)
  • 2020-12-16 05:29

    The problem is your date format: instead of yyyy-MM-dd HH:mm:ss a use yyyy-MM-dd hh:mm:ss a.

    HH will be the hour in day (0-23) whereas hh will be the hour in AM/PM (1-12). Thus with your date format 02:30:00 will be parsed as just that instead of being converted to the PM version (which in hour of day would be 14:30:00).

    0 讨论(0)
  • 2020-12-16 05:29

    So, you have these dates as strings? Parse them with a SimpleDateFormat with the appropriate format string, and compute the difference in hours:

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
    
    Date d1 = df.parse("2012-01-24 12:30:00 PM");
    Date d2 = df.parse("2012-01-24 02:30:00 PM");
    
    int hoursDifference = (int)((d2.getTime() - d1.getTime()) / 3600000L);
    System.out.println("Difference in hours: " + hoursDifference);
    

    Your error is that you are using HH (24-hour hours) instead of hh (12-hour hours) in your format string.

    0 讨论(0)
  • 2020-12-16 05:34

    onsider using Apache Commons DateUtils DurationFormatUtils formatPeriod method will do the magic.

    Good luck!

    EDIT Assuming that you have Date a and Date b initialized at this point,

    String format="HH:mm:ss.SSS";   // whatever you wish
    boolean padWithZeros=true; // whatever you wish
    TimeZone timezone=null; // whatever you wish
    long timeA = a.getTime();
    long timeB = b.getTime();
    
    String period = DurationFormatUtils.formatPeriod(timeB, timeA, format, padWithZeros, timezone);
    
    0 讨论(0)
  • 2020-12-16 05:35

    The reason why it shows 10 hours as the difference is that you've got an error in the pattern when parsing the input.

    Here's an example using SimpleDateFormat:

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
    
    Date date1 = df.parse("2012-01-24 12:30:00 PM");
    Date date2 = df.parse("2012-01-24 02:30:00 PM");
    
    long differenceInHours = Math.abs(date1.getTime() - date2.getTime()) / 1000 / 60 / 60);
    

    Will return 10.

    When we just slightly change the date format pattern, using hh for hour in am/pm (1-12) instead of HH for hour in day (0-23):

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

    It returns (the expected) 2.

    See the documentation for SimpleDateFormat to get your patterns right.

    0 讨论(0)
提交回复
热议问题