1

I am using Kotlin and somehow I'm receiving this weird type of date format i had never ever seen before:

/Date(1224043200000)/

What is this format, and how can we display the date using this? I have been using Json ISO standard 8601. And I know how to convert it's format to date but this one is new for me.

halfer
  • 19,824
  • 17
  • 99
  • 186
Aldor
  • 193
  • 4
  • 19

1 Answers1

1

1224043200000 refers to the number of milliseconds from 1970-01-01T00:00:00Z and is equivalent to 2008-10-15T04:00:00Z at UTC.

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {
    public static void main(final String[] args) {
        Instant instant = Instant.ofEpochMilli(1224043200000L);
        System.out.println(instant);

        // Get ZonedDateTime from Instant
        // I have used time-zone of Europe/London. Use a time-zone as per your
        // requirement. You can also use ZoneId.systemDefault()
        ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("Europe/London"));

        // Get OffsetDateTime from ZonedDateTime
        OffsetDateTime odt = zdt.toOffsetDateTime();

        // Get LocalDateTime from ZonedDateTime if required. However, note that
        // LocalDateTime drops the valuable information, time-zone from ZonedDateTime
        // and zone-offset from OffsetDateTime
        LocalDateTime ldt = zdt.toLocalDateTime();
        System.out.println(ldt);
    }
}

Output:

2008-10-15T04:00:00Z
2008-10-15T05:00

Backport of the Java SE 8 date-time classes to Java SE 6 and 7:

Check ThreeTen-Backport and How to use ThreeTenABP

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • But can u please tell me whether we should use this type of old json? Coz i searched and people said that it's bad to use this for new development? We should use json ISO 8601 instead. – Aldor Jul 04 '20 at 00:09
  • Correct, ISO 8601 is preferred. – Ole V.V. Jul 04 '20 at 10:20
  • Thank you, this works but only in api level >= 26 – Aldor Jul 05 '20 at 17:59
  • @Aldor - I've added the links at the end about how you can backport this to Java SE 6 and 7. I suggest you switch from the outdated and error-prone `java.util` date-time API to the [modern date-time API](https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html). – Arvind Kumar Avinash Jul 05 '20 at 18:23
  • 1
    okay thanks for helping me out! – Aldor Jul 07 '20 at 06:12