Neon Warge Neon Warge - 9 months ago 46
Java Question

Joda DateTime doesn't set date correctly via milliseconds

I am providing a millisecond data

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();

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();

return dateTimeFormat.print(dateTime);


Answer Source

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

The withMillis method may return a new instance. Change



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


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(new DateTime(1473069000000L));