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.

3
  • even though is deprecated, you can use Date::getTimezoneOffset Commented May 27, 2021 at 20:05
  • Does this answer your question? Get TimeZone offset value from TimeZone without TimeZone name Commented May 27, 2021 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. Commented May 28, 2021 at 18:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Good answer. Prefer OffsetDateTime over ZonedDateTmesince the string contains an offset and no time zone (like Asia/Colombo or America/New_York).
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?
Yes. But check out this link which addresses a solution using an offset formatter.
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 Great! Whatever meets your requirements.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.