Hot questions for Using Joda-Time in java 7


First, I'm required to use Java 7, so I can't use SE8 Time stuff sadly though I'd love to learn it instead!

I have a List of Duration as follows:

List<Duration> durations;

Most of these values are about 8 hours long, but some are zero (intentionally). How do I check if they are 0 days, 0 minutes, 0 seconds and 0 ms long? Do I need to check each method as follows or is there a neater method?

for(Duration duration : durations){
    if(duration.getStandardDays() == 0 && 
       duration.getStandardHours() == 0 && 
       duration.getStandardSeconds() == 0){
        //do something

The reason I ask is I notice there's the constant ZERO in the API, and I assume this is somehow linked into it. How do I compare to it instead? Or should I?


You should just be able to do

if (duration.getMillis()==0)

getMillis() returns the length of the duration, in milliseconds.


I have date formats as: EEE, dd MMM yyyy HH:mm:ss Z

For ex.,

Date 1 : Mon Sep 10 08:32:58 GMT 2018

Date 2 : Tue Sep 11 03:56:10 GMT 2018

I need date difference as 1 in above case, but I am getting value as 0 if I use joda date time or manually converting date to milliseconds.

For reference :

Any leads will be helpful. Example :

Date date1 = new Date("Mon Sep 10 08:32:58 GMT 2018");
Date date2 = new Date("Tue Sep 11 03:56:10 GMT 2018");
DateTime start = new DateTime(date1 );
DateTime end = new DateTime(date2);
int days = Days.daysBetween(start, end).getDays();
System.out.println("Date difference: " + days);

Output: Date difference: 0


Joda-Time counts only whole days, in other words, truncates the difference to a whole number. So with a little over 19 hours between your values it counts as 0 days. If you want to ignore the time part of the dates, convert to LocalDate first:

    int days = Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays();

(Thanks for providing the concrete code yourself in a comment. Since you said it worked, I thought it deserved to be an answer.)


I have a string for date time coming as like this without any spaces in between them:


Is there any way to convert this to unix timestamps? The date time string will always be in that format.


java.time and ThreeTen Backport

The comment by ernest_k is well thought out and solves your issue:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("EEEMMMddHH:mm:sszyyyy", Locale.ENGLISH);
    String dateTimeString = "TueDec2618:47:09UTC2017";
    ZonedDateTime dateTime = ZonedDateTime.parse(dateTimeString, formatter);
    long unixTimestamp = dateTime.toEpochSecond();
    System.out.println("Parsed date-time " + dateTime + " Unix timestamp " + unixTimestamp);

The output from running on ThreeTen Backport 1.3.6, tested on Java 1.7.0_79, is:

Parsed date-time 2017-12-26T18:47:09Z[Zulu] Unix timestamp 1514314029

Question: How can I use ZonedDateTime and DateTimeFormatter on Java 7?

I am still on Java 7 btw so can't use ZonedDataTime and DateTimeFormatter but I can use joda library.

Indeed you can. java.time just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

While Joda-Time would be another nice solution, I believe that you should prefer the ThreeTen Backport over Joda-Time (though opinions differ). The Joda-Time home page advises:

Note that Joda-Time is considered to be a largely "finished" project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

So java.time seems to be the future-proof solution.