Home > OS >  How to convert date format to UTC in Scala?
How to convert date format to UTC in Scala?

Time:09-09

How to convert date format "2021-02-28 13:38:00.597 0000" to "Mon, Feb 28,2021 15:25:00 UTC" UTC format in Scala?

CodePudding user response:

If you are using an older Java version prior to Java 8, it's best to use the DateTimeFormat from joda-time. BTW, the 0000 zone offset is for UTC, so I could have omitted withZoneUTC(), but I still used it for the first date just to be safe:

  val oldDateString = "2021-02-28 13:38:00.597 0000"
  val OldFormat     = "yyyy-MM-dd HH:mm:ss.SSSZ"
  val NewFormat     = "EEE, MMM dd, yyyy HH:mm:ss z"

  val formatterOld = DateTimeFormat.forPattern(OldFormat)
  val formatterNew = DateTimeFormat.forPattern(NewFormat)

  val dt              = formatterOld.withZoneUTC().parseDateTime(oldDateString)
  val dateStringInUTC = formatterNew.withZoneUTC().print(dt)

  println(dt)              // 2021-02-28T13:38:00.597Z
  println(dateStringInUTC) // Sun, Feb 28, 2021 13:38:00 UTC

UPDATE: For Java 8 and newer, the java.time API is your friend. Similarly, withZoneSameInstant(ZoneOffset.UTC) was not really needed:

  val oldDateString = "2021-02-28 13:38:00.597 0000"
  val OldFormat     = "yyyy-MM-dd HH:mm:ss.SSSZZZ"
  val NewFormat     = "EEE, MMM dd, yyyy HH:mm:ss z"

  val formatterOld = DateTimeFormatter.ofPattern(OldFormat)
  val formatterNew = DateTimeFormatter.ofPattern(NewFormat)

  val zdt             = ZonedDateTime.parse(oldDateString, formatterOld)
  val dateStringInUTC = zdt.withZoneSameInstant(ZoneId.of("UTC")).format(formatterNew)

  println(zdt)             // 2021-02-28T13:38:00.597Z
  println(dateStringInUTC) // Sun, Feb 28, 2021 13:38:00 UTC

UPDATE: Switched to using ZoneId.of("UTC") instead of ZoneOffset.UTC because the latter does not get the String UTC printed at the end, even though ZoneOffset extends ZoneId, as @deHaar mentioned.

CodePudding user response:

If you could use java.time, you would need

  • a DateTimeFormatter for parsing Strings with the format of your input example, which is quite near to ISO standard, but is missing the 'T' between date and time of day
  • another DateTimeFormatter for outputting the temporal content in the desired format, which includes (English) abbreviations for day of week and month of year
  • an OffsetDateTime for parsing the String with the first DateTimeFormatter and
  • a ZonedDateTime for the temporal value in UTC

This is how I would do it in Java:

public static void main(String[] args) {
    // example String
    String utcDatetimeString = "2021-02-28 13:38:00.597 0000";
    // prepare a formatter that can parse a String of this format
    DateTimeFormatter dtfIn = DateTimeFormatter.ofPattern(
                                        "uuuu-MM-dd HH:mm:ss.SSSxxxx",
                                        Locale.ENGLISH
                                    );
    // parse it to an OffsetDateTime
    OffsetDateTime odt = OffsetDateTime.parse(utcDatetimeString, dtfIn);
    // then convert it to a ZonedDateTime applying UTC zone
    ZonedDateTime zdt = odt.atZoneSameInstant(ZoneId.of("UTC"));
    // prepare a formatter that produces the desired output
    DateTimeFormatter dtfOut = DateTimeFormatter.ofPattern(
                                        "EEE, MMM dd, uuuu HH:mm:ss zzz",
                                        Locale.ENGLISH
                                    );
    // and print the ZonedDateTime using the formatter
    System.out.println(zdt.format(dtfOut));
}

Output:

Sun, Feb 28, 2021 13:38:00 UTC
  • Related