1

I have a UTC formatted DateTime String

and need this String to be converted to DATE Object without any Format change.

Currently, when I try to convert it to a date Object it is returned as GMT formatted Date object as depicted in the image attached below.

I have a limitation of using SimpleDateFormat as I have to support API<21 also.

String newDateString = "2022-06-27 08:27:00 UTC";
DateFormat format = new SimpleDateFormat(Constants.EnumDateFormats.DATE_FORMAT7);//"yyyy-MM-dd HH:mm:ss 'UTC'"
                format.setTimeZone(java.util.TimeZone.getTimeZone("UTC"));
                Date date = format.parse(newDateString);
                System.out.println(date);//printing GMT formatted Date object but I need Date object in UTC format like 2022-06-27 08:27:00 UTC

I have attached debugged code image image for better under standing.

3
  • 2
    You have a misunderstanding about Date objects: a Date object does not have a format by itself. It's only a timestamp value. There is no such thing as "a Date object without any format change". When you print a Date object, it's printed in a default format. The object does not know what the format was that you parsed the original string with. If you need to print a Date in a specific format, then you'll have to format it using an appropriate SimpleDateFormat. The Date object itself does not contain information about formatting. Commented Jun 27, 2022 at 8:49
  • @Jesper thanks for clearing the ambiguity. I would rather like to rephrase my issue. How can I get above UTC dateTime String as UTC formatted date Object(not a string)! can you provide me an example for clarification? Commented Jun 27, 2022 at 9:13
  • 2
    There is no such thing as an "UTC formatted Date object". A Date object does not have a format. Commented Jun 27, 2022 at 12:25

1 Answer 1

5

I have a UTC formatted DateTime String

No, you don’t.

UTC is the prime meridian commonly used for time-keeping. Time zones towards the east use an offset some number of hours-minutes-seconds ahead of UTC; those to the west, behind UTC.

UTC has nothing to do with text formats.

need this String to be converted to DATE Object without any Format change.

There are two Date classes in Java. You should specify which you intended.

And, both classes are terrible, flawed by poor design decisions. They were years ago supplanted by the modern java.time classes defined in JSR 310.

I have a limitation of using SimpleDateFormat as I have to support API<21 also.

Nope, no such limitation. The latest tooling brings most of the java.time functionality to earlier Android, via “API de-sugaring”.

To parse, use DateTimeFormatter to define a matching formatting pattern. Try the following, but I’ve not yet tested.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu-MM-d HH:mm:ss OOOO" ).withLocale( Locale.US ) ; 
OffsetDateTime odt = OffsetDateTime.parse( input , f ) ;

Tip: Educate the publisher of your data about the ISO 8601 standard for formatting textual representations of date-time values.


All of this has been covered many times already on Stack Overflow. Please search before posting here.

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

1 Comment

sir! thanks for that such a detailed answer, have cleared much of my misconceptions especially the 'API de-sugaring" concept was an awesome thing you taught. I'm very grateful.

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.