0

I try to convert from Milliseconds to string of date. However, the result is not correct as my expected.

The input is milliseconds (Ex: 1508206600485)

My time zone is UTC +10:00

------Expected-------------------------------------------- Actual------

01:32 (PM) 17/10/2017--------------------------------02:32 (PM) 17/10/2017

Here is the method of that

public static String getDate(long milliSeconds) {
    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM/yyyy");
    String dateString = formatter.format(new Date(milliSeconds));
    return dateString;
}
Alex Crowel
  • 97
  • 1
  • 10

2 Answers2

6

Good you found a solution, I just like to add an approach with Java 8 new java.time API. The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and it's strongly recommended to switch to the new API if possible.

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, you'll also need the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

To convert the millis value to a specific timezone, you can use the Instant class, then use a ZoneId to convert to a timezone, creating a ZonedDateTime. Then you use a DateTimeFormatter to format it:

// convert millis value to a timezone
Instant instant = Instant.ofEpochMilli(1508206600485L);
ZonedDateTime z = instant.atZone(ZoneId.of("Australia/Sydney"));
// format it
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("hh:mm dd/MM/yyyy");
System.out.println(fmt.format(z)); // 01:16 17/10/2017

The output is:

01:16 17/10/2017

Note that I used hh for the hours. According to javadoc, this lettern represents the clock-hour-of-am-pm field (values from 1 to 12), so without the AM/PM indicator, it can be ambiguous. Maybe you want to add AM/PM field (adding the letter a to the format pattern), or change the hours to HH (hour-of-day, with values from 0 to 23).

Also note that the actual value of the ZonedDateTime is 2017-10-17T13:16:40.485+11:00 (01:16 PM), because in October 17th 2017, Sydney is in Daylight Saving Time, so the actual offset is +11:00.

  • 1
    This is the correct way to solve this. I'm still wondering (1) why OP's expected output was 1:32pm, (2) why OP's time zone wasn't correct to start with. Your solution doesn't address those issues; but I've upvoted it here because it's the best thing to show to anyone who stumbles across this question in the future. – Dawood ibn Kareem Oct 17 '17 at 03:30
  • @DawoodibnKareem Item 1, I guess it's probably a typo or OP actually tested with another input value. Item 2, I based on OP's own answer saying that Sydney timezone solved the problem - maybe DST wasn't being considered, as Sydney actually uses +10:00 offset when not in DST. Or maybe their JVM default timezone is set to a different one, who knows... –  Oct 17 '17 at 03:37
  • 1
    Yeah, probably. I was thinking of voting to close the question as "can't reproduce", which of course would also cover the typo case. However, your answer here now makes it worth keeping the question. – Dawood ibn Kareem Oct 17 '17 at 03:38
0

Basing on @phlaxyr, I have solved my problem. You can get your time zone in this link below

http://tutorials.jenkov.com/java-date-time/java-util-timezone.html

public static String getDate(long milliSeconds) {
    SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM/yyyy");
    formatter.setTimeZone(TimeZone.getTimeZone("Australia/Sydney"));
    String dateString = formatter.format(new Date(milliSeconds));
    return dateString;
}
Alex Crowel
  • 97
  • 1
  • 10