difference in seconds between two dates using joda time?

前端 未结 2 1854
春和景丽
春和景丽 2020-12-01 23:28

Suppose there are two dates A(start time) & B(end time). A & B could be the time on the same day or even different day. My task is to show difference in seconds. Dat

相关标签:
2条回答
  • 2020-12-01 23:44

    Use the Seconds class:

    DateTime now = DateTime.now();
    DateTime dateTime = now.plusMinutes(10);
    Seconds seconds = Seconds.secondsBetween(now, dateTime);
    System.out.println(seconds.getSeconds());
    

    This piece of code prints out 600. I think this is what you need.

    As further advice, explore the documentation of joda-time. It's pretty good, and most things are very easy to discover.

    In case you need some help with the parsing of dates (It's in the docs, really), check out the related questions, like this:

    Parsing date with Joda with time zone

    0 讨论(0)
  • 2020-12-01 23:54

    The answer of @pcalcao will be best in most cases. Be aware that seconds will be rounded to an integer.

    If you are interested in sub-seconds accuracy just substract the milliseconds:

    double seconds = (now.getMillis() - dateTime.getMillis()) / 1000d;
    
    0 讨论(0)
提交回复
热议问题