0

I have a date coming from a device and I want to do the following in Java:

  1. I want to parse this date "2021-05-27T18:47:07+0530" to yyyy-mm-dd HH:MM:SS
  2. Get the Date's offset value so that I can get the timezone offset as +05:30 or whatever timezone it comes from.

For the first one I have done this and looks like it works, but any better smaller approach will be handy as well:


        String date = "2021-05-27T18:47:07+0530";
        String inputPattern = "yyyy-MM-dd'T'HH:mm:ss+SSSS";
        String outputPattern = "yyyy-MM-dd HH:mm:ss";
        LocalDateTime inputDate = null;
        String outputDate = null;
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern(inputPattern, Locale.ENGLISH);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern(outputPattern, Locale.ENGLISH);
        inputDate = LocalDateTime.parse(date, inputFormatter);
        outputDate = outputFormatter.format(inputDate);
        System.out.println("inputDate: " + inputDate);
        System.out.println("outputDate: " + outputDate);

The Ouput is:

inputDate: 2021-05-27T18:47:07.053
outputDate: 2021-05-27 18:47:07

I don't know how to get the offset value of timezone in this case.

There are many recommendations including using SimpleDateFormat and ZonedDateTime etc but should be the best answer for this considering the offset value can be dynamic i.e it can be +05:30,+09:00 etc.

Please help in this case.

RahulNans
  • 47
  • 8
  • even though is deprecated, you can use `Date::getTimezoneOffset` – Alberto Sinigaglia May 27 '21 at 20:05
  • Does this answer your question? [Get TimeZone offset value from TimeZone without TimeZone name](https://stackoverflow.com/questions/11399491/get-timezone-offset-value-from-timezone-without-timezone-name) – Alberto Sinigaglia May 27 '21 at 20:05
  • Hi @Berto99 there are too many answers to come to a conclusion and most the accepted answer is taking date as the current date not the date we are providing. So, that's why it doesn't really answer my question. – RahulNans May 28 '21 at 18:26

1 Answers1

2

Try it like this.

String dateTime = "2021-05-27T18:47:07+0530";
String inputPattern = "yyyy-MM-dd'T'HH:mm:ssZ";
String outputPattern = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ", 
    Locale.ENGLISH);
ZonedDateTime zdt = ZonedDateTime.parse(dateTime, dtf);
ZoneOffset tz = zdt.getOffset();
System.out.println(tz);
System.out.println(zdt.format(DateTimeFormatter.ofPattern(outputPattern,
        Locale.ENGLISH)));

Prints

+05:30
2021-05-27 18:47:07

The zoned date time could also be reprinted using the same format by which it was originally parsed.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • 1
    Good answer. Prefer `OffsetDateTime` over `ZonedDateTme`since the string contains an offset and no time zone (like Asia/Colombo or America/New_York). – Ole V.V. May 28 '21 at 06:04
  • It works very well in the case of let's say +0100 or +0900 but when using +0000 which is GMT it returns the value Z for the offset. As I understand is the Zulu date time and it would return Z instead of 00:00. Correct? – RahulNans May 28 '21 at 18:24
  • 1
    Yes. But check out this [link](https://stackoverflow.com/questions/49790409/how-to-make-zoneoffset-utc-return-0000-instead-of-z) which addresses a solution using an offset formatter. – WJS May 28 '21 at 18:48
  • I used this, since by date is dynamic, not instant.now(), so I converted to Instant object and `Instant result = Instant.from(dtf.parse(dateTime)); ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(result); System.out.println(offsetFormatter.format(offset)); ` will get me the same result everytime that is my timezone offset. So, I think the instant date needs to be corrected. – RahulNans May 29 '21 at 20:01
  • @RahulNans Great! Whatever meets your requirements. – WJS May 29 '21 at 20:03
  • @WJS I edited my comment, I think something needs to change, but the direction is right. – RahulNans May 29 '21 at 20:39
  • I couldn't get the offset value when date is +0000 which returns Z, the only thing we can do is use the ternary operator or some other if else to return +00:00. When I used the above method of zoneoffset , it kept returning me +05:30 which is my time zone. – RahulNans May 30 '21 at 17:47
  • @RahulNans You may print the offset directly from the `ZonedDateTime` using a separate formatter: `zdt.format(DateTimeFormatter.ofPattern("xxxxx"))` yields `+00:00` if offset is 0. Just remember to use lower case `xxxxx`. – Ole V.V. Jun 06 '21 at 05:14