-1

I have a string for date time coming as like this without any spaces in between them:

TueDec2618:47:09UTC2017

Is there any way to convert this to unix timestamps? The date time string will always be in that format.

user85421
  • 28,957
  • 10
  • 64
  • 87
flash
  • 1,455
  • 11
  • 61
  • 132
  • Take a look at this: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – Skrelp Mar 08 '19 at 18:27
  • look at this : [https://stackoverflow.com/questions/2742380/how-to-convert-string-date-to-timestamp-in-java/2742398](https://stackoverflow.com/questions/2742380/how-to-convert-string-date-to-timestamp-in-java/2742398) – Bhasher Mar 08 '19 at 18:29
  • What's the time in this string? – ernest_k Mar 08 '19 at 18:29
  • 1
    Don't follow the advice to use `SimpleDateFormat`, if you need to parse a string to date and time use `DateTimeFormatter` – Joakim Danielson Mar 08 '19 at 18:33
  • 1
    @ernest_k my interpretation of that string is `2017-12-26 18:47:09 UTC` – Joakim Danielson Mar 08 '19 at 18:34
  • 1
    @JoakimDanielson yes thats what the above time is. – flash Mar 08 '19 at 18:35
  • 3
    This should work: `ZonedDateTime.parse("TueDec2618:47:09UTC2017", DateTimeFormatter.ofPattern("EEEMMMddHH:mm:sszyyyy"))`. It produces `2017-12-26T18:47:09Z[UTC]` – ernest_k Mar 08 '19 at 18:37
  • I am still on Java 7 btw so can't use ZonedDataTime and DateTimeFormatter but I can use joda library. – flash Mar 08 '19 at 18:38
  • @Skrelp Even on Java 7 please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Yes, you can use it on Java 7 when you add [ThreeTen Backport](https://www.threeten.org/threetenbp/) to your project. Better than and a successor of Joda-Time. – Ole V.V. Mar 08 '19 at 19:48

1 Answers1

3

java.time and ThreeTen Backport

The comment by ernest_k is well thought out and solves your issue:

    DateTimeFormatter formatter
            = DateTimeFormatter.ofPattern("EEEMMMddHH:mm:sszyyyy", Locale.ENGLISH);
    String dateTimeString = "TueDec2618:47:09UTC2017";
    ZonedDateTime dateTime = ZonedDateTime.parse(dateTimeString, formatter);
    long unixTimestamp = dateTime.toEpochSecond();
    System.out.println("Parsed date-time " + dateTime + " Unix timestamp " + unixTimestamp);

The output from running on ThreeTen Backport 1.3.6, tested on Java 1.7.0_79, is:

Parsed date-time 2017-12-26T18:47:09Z[Zulu] Unix timestamp 1514314029

Question: How can I use ZonedDateTime and DateTimeFormatter on Java 7?

I am still on Java 7 btw so can't use ZonedDataTime and DateTimeFormatter but I can use joda library.

Indeed you can. java.time just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

While Joda-Time would be another nice solution, I believe that you should prefer the ThreeTen Backport over Joda-Time (though opinions differ). The Joda-Time home page advises:

Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to java.time (JSR-310).

So java.time seems to be the future-proof solution.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161