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 every time I call:
DateTime dateTime = new DateTime();
dateTime.withMillis(1473080981L)
It still returns 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!