0

I need to convert dates in this format = "dd-M-yyyy kk:mm:ss z" to LocalDateTime as per server time. So far, I've tried every approach I could find here and on other blogs, but none helped. That's why I asked a new question.

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern(SCHEDULED_TIME_FORMAT);
    logger.info("Date Recieved : {}", scheduledDate);
    ZonedDateTime zonedDateTime = ZonedDateTime.parse(scheduledDate, formatter);
    ZonedDateTime zonedDateTimeOnMyServer = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault());
    LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
    logger.info("Scheule Time as per Server : {}", localDateTime);
    LocalDateTime scheduleTime = zonedDateTimeOnMyServer.toLocalDateTime();
    logger.info("Scheduled Time as per GMT : {}", scheduleTime);

Requirement: I want localDateTime to give me the time as per Timezone provided in the string (for example, IST) and I want scheduleTime to be the time as per server.

Here are the logs of an example:

2021-10-04 13:53:53.968  INFO 1  : Date Recieved : 04-10-2021 19:24:00 IST
2021-10-04 13:53:53.972  INFO 1  : Scheule Time as per Server : 2021-10-04T19:24
2021-10-04 13:53:53.972  INFO 1  : Scheduled Time as per GMT : 2021-10-04T19:24

Please help, or suggest alternate way to achieve the requirement. A change in format is also possible but not preferred.

Update - This worked after changing "IST" to "Asia/Kolkata"

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Deepanshu Rathi
  • 381
  • 1
  • 10
  • 1
    What *specifically* is wrong with the code you have posted? – Scott Hunter Oct 04 '21 at 12:30
  • 5
    Do you realize that LocalDateTime does not reflect a certain timezone? The JavaDoc states: "This class does not store or represent a time-zone.". So if you parse "... 18:00 IST" into `LocalDateTime` it will be "... 18:00" in any timezone, including UTC. Use `ZonedDateTime` instead. – Thomas Oct 04 '21 at 12:46
  • Thanks @Thomas Scott, You can see the logs, Not able to get the time as per server – Deepanshu Rathi Oct 04 '21 at 13:13
  • 2
    One thing to add here is that "IST" seems to be parsed to the Icelandic timezone even though `ZoneId.SHORT_IDS` lists it as "Asia/Kolkata". Try to pass "04-10-2021 18:00:00 Asia/Kolkata" to get the time in the Indian timezone. – Thomas Oct 04 '21 at 13:23
  • Thanks @Thomas, It worked, replaced IST with Asia/Kolkata – Deepanshu Rathi Oct 04 '21 at 14:54
  • Two, three, four and five letter time zone abbreviations are alarmingly often ambiguous. IST may be for Irish Standard Time, Irish Summer Time or Israel Standard Time. Don’t try to parse those. You don’t know what you get. *A change in format is also possible but not preferred.* I’m afraid you will need such a change. I’d consider the ISO 8601 inspired format that you get from `ZonedDateTime.toString()` and that the one-arg `ZonedDateTime.parse(CharSequence)` parses without any expliciti formatter. – Ole V.V. Oct 10 '21 at 11:19
  • @Thomas is right: you should not want to use `LocalDateTime` here. The *Local* in this and other class names means *without time zone or offset*. It’s not what you need. Stick to `ZonedDateTime`. – Ole V.V. Oct 10 '21 at 11:25

1 Answers1

4

toLocalDateTime does not convert between timezones, first, convert between timezones and then to local

String scheduledDate = "04-10-2021 18:00:00 IST";

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-M-yyyy kk:mm:ss z");

ZonedDateTime zonedDateTime = ZonedDateTime.parse(scheduledDate, formatter);

ZonedDateTime zonedDateTimeOnMyServer = zonedDateTime.withZoneSameInstant(ZoneId.systemDefault());

System.out.println("Original scheduled zoned: " + zonedDateTime);
System.out.println("Original scheduled local: " + zonedDateTime.toLocalDateTime());
System.out.println("On my server scheduled zoned: " + zonedDateTimeOnMyServer);
System.out.println("On my server scheduled local: " + zonedDateTimeOnMyServer.toLocalDateTime());

with output

Original scheduled zoned: 2021-10-04T18:00Z[Atlantic/Reykjavik]
Original scheduled local: 2021-10-04T18:00
On my server scheduled zoned: 2021-10-04T20:00+02:00[Europe/Madrid]
On my server scheduled local: 2021-10-04T20:00
josejuan
  • 9,338
  • 24
  • 31
  • Did not work @josejuan. Updating question and logs for your reference – Deepanshu Rathi Oct 04 '21 at 13:56
  • 1
    @DeepanshuRathi you should be very specific describing what you want (i.e. easilly the term "local" may be missinterpreted) providing some input examples and the expected output. (My output show the same input DateTime, first in the source timezone, second the local in the source timezone, third the same DateTime [same instant] in other timezone and fourth the local in that timezone) – josejuan Oct 04 '21 at 14:37