0

I know this is really a beginner question, but it seems I can't find any good solution for this.

So I have a String that I get from a JSON database website that is:

DateTime = "\/Date(1598036400000)\/" 

But the question is how do I convert this String to a real DateTime?

deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    Are those milliseconds since epoch? Which timezone, any additional information? – Zabuzard Aug 21 '20 at 11:48
  • You will have to remove the parts `Date(` and `)` before you can parse the value (I guess those are epoch milliseconds). Oh, and remove the slashes, too. – deHaar Aug 21 '20 at 11:48
  • 2
    `Instant.ofEpochMilli(1598036400000)` would work, if that is what you want. You can add a timezone to it to convert it to a different class which is timezone aware, for example `.atZone(ZoneID.systemDefault())`. – Zabuzard Aug 21 '20 at 11:49
  • @Zabuzard its Central European Summer Time – stefan josimovski Aug 21 '20 at 11:52
  • 1
    Then `.atZone(ZoneId.of("Europe/Berlin"))` could be what you are looking for. Note that the location matters, time is not the same even inside the same "timezone", due to things like DST or country specific extra rules, especially historically. – Zabuzard Aug 21 '20 at 11:53
  • 1
    It would also help if you explain what you want to do with this _"real DateTime"_, as it is still unclear what kind of class you are expecting. An `Instant`? a `ZonedDateTime`? Maybe a `String` in a specific format? – Zabuzard Aug 21 '20 at 11:55
  • @Zabuzard Thank you for your answers, I am getting this string from a URL database, and I use it to provide it to a TextView in a mobile application, but when it only shows me the string not the DateTime , so I need to convert it , it can return a string with the datetime.. – stefan josimovski Aug 21 '20 at 11:59

1 Answers1

3

You will have to perform several steps due to the value not being a long in its original representation "/Date(1598036400000)/". The numeric value inside that String is representing a moment in time in epoch milliseconds and you have to remove the remaining characters or substrings. Here's an example...

public static void main(String[] args) {
    // take the original String value,
    String datetime = "/Date(1598036400000)/";
    // remove anything that isn't a digit,
    String millisStr = datetime.replace("/", "").replace("Date(", "").replace(")", "");
    // then convert it to a long,
    long millis = Long.valueOf(millisStr);
    // and create a moment in time (Instant) from the long
    Instant instant = Instant.ofEpochMilli(millis);
    // and finally use the moment in time to express that moment in a specific time zone 
    ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.of("CET"));
    // and print its default String representation
    System.out.println(zdt);
}

... which outputs

2020-08-21T21:00+02:00[CET]

If you need a differently formatted String, you can use a DateTimeFormatter that even considers different locales or languages.
The output of

System.out.println(zdt.format(
        DateTimeFormatter.ofPattern("EEEE, dd. MMMM yyyy HH:mm:ss", Locale.GERMAN))
);

is

Freitag, 21. August 2020 21:00:00
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 1
    It’s a very good answer. Only use `ZoneId.of("Europe/Paris")` or `ZoneId.of("Europe/Berlin")` or some other time zone ID in the *region/city* format. – Ole V.V. Aug 21 '20 at 16:02