Neon Warge Neon Warge - 1 month ago 9
Java Question

Joda DateTime doesn't set date correctly via milliseconds

I am providing a millisecond data

1473080981L
which should date to:
September 5, 2016 9:50 PM
. I am doing unit testing and this was my first time to play around with JUnit. What I noticed so far is that everytime I call:

DateTime dateTime = new DateTime();
dateTime.withMillis(1473080981L)


It still return the correct date but currently, at the time of this writing, it is now 10:00 PM in the evening, this method call produces
September 5, 2016 10:00 PM
. DateTime did not respect the time I set it to.

Is this the correct behavior? Is there a way I could instantiate a DateTime by setting my own predefined selected date time?

Here is my unit test (please bear with it being pointless, I am learning Unit testing):

String testDate = "September 5, 2016 9:50 PM"; // MMMM dd, yyyy K:mm a
String testDateResult1 = DateTimeFormatter.format(dateTime, DateTimeFormatter.FORMAT_DEFAULT_DATE_TIME_12);
String testDateResult2 = DateTimeFormatter.format(thisDay , DateTimeFormatter.FORMAT_DEFAULT_DATE_TIME_12);

assertTrue(testDate.compareTo(testDateResult1) == 0);
assertTrue(testDate.compareTo(testDateResult2) == 0);


My DateTimeFormatter looks like this:

public static String format(@NonNull DateTime dateTime, String validFormat)
{
org.joda.time.format.DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(validFormat);
return dateTimeFormat.print(dateTime);
}

public static String format(long dateToMillis, String validFormat)
{
org.joda.time.format.DateTimeFormatter dateTimeFormat = DateTimeFormat.forPattern(validFormat);

DateTime dateTime = new DateTime();
dateTime.withMillis(dateToMillis);

return dateTimeFormat.print(dateTime);
}


Thanks!

Answer

First note that 1473080981L is Sun Jan 18 1970 01:11:20 in UTC Time. You can check it at https://currentmillis.com/

The withMillis method may return a new instance. Change

dateTime.withMillis(dateToMillis);

to

dateTime = dateTime.withMillis(dateToMillis);

or simply do

dateTime = new DateTime(dateToMillis);

public DateTime withMillis(long newMillis)
Returns a copy of this datetime with different millis. The returned object will be either be a new instance or this. Only the millis will change, the chronology and time zone are kept.

Parameters: newMillis - the new millis, from 1970-01-01T00:00:00Z
Returns: a copy of this datetime with different millis

Demo:

import org.joda.time.DateTime;

public class JodaTest {

    public static void main(String[] args) {
        DateTime dateTime = new DateTime();
        dateTime = dateTime.withMillis(1473069000000L);

        System.out.println(dateTime);
        System.out.println(new DateTime(1473069000000L));
    }
}

Output:

2016-09-05T11:50:00.000+02:00
2016-09-05T11:50:00.000+02:00
Comments