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
for better under standing.
Dateobjects: aDateobject does not have a format by itself. It's only a timestamp value. There is no such thing as "aDateobject without any format change". When you print aDateobject, 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 aDatein a specific format, then you'll have to format it using an appropriateSimpleDateFormat. TheDateobject itself does not contain information about formatting.Dateobject". ADateobject does not have a format.