Java Date Format - The Correct Way!

Поделиться
HTML-код
  • Опубликовано: 28 дек 2024

Комментарии • 4

  • @johnvoorhees2331
    @johnvoorhees2331 2 месяца назад +1

    XMLGregorianCalendar is used in a lot of legacy java applications. Any suggestions to convert? and dealing with the offset while dealing with Daylight savings time?

    • @DoSomeDev
      @DoSomeDev  2 месяца назад

      True, XMLGregorianCalendar is rather legacy. I took a look into converting XMLGregorianCalendar to LocalDateTime from the java.time package. One way I'd go would be to get the Instant out of XMLGregorianCalendar, and then continuing by creating myself a ZoneId, which also respects daylight saving. Now I have everything to get my LocalDateTime:
      Instant instant = xmlGregorianCalendar.toGregorianCalendar().toInstant();
      ZoneId amsterdamZoneId = ZoneId.of("Europe/Amsterdam");
      LocalDateTime amsterdamLocalDateTime = instant.atZone(amsterdamZoneId).toLocalDateTime();
      Depending on your use case, you could just as well take the manual route, extracting from XMLGregorianCalendar then year, month, day, etc.

    • @johnvoorhees2331
      @johnvoorhees2331 2 месяца назад

      @@DoSomeDev Thankyou for responding, however there is an offset, which depending on daylights daving time the offset will change
      Sorry if question os newbish, but how do I determine in my case 8 or 7

    • @DoSomeDev
      @DoSomeDev  2 месяца назад

      Do you have a date/time string from your XML that you can show? Or do you have a Git repo with an example?
      One possible value could look like this "2024-10-11T02:44:42.193+02:00", meaning you have a time and at the end you have the "Z" part where you add or subtract the time according to your time zone and your daylight saving. When you convert that to a variable of type Instant using the toInstant() method, the correct time will be calculated.