-1

I have this date object which when is printed to the console is "Mar 24 22:31:00 EET 2023", now I want to convert this date to the following format : yyyy-MM-dd'T'HH:mm:ssXXX what I have tried is the following:

    Date currentTimeStampInUtc =//the utc date retrieved
    final DateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
    String currentTimeFormattedWithTimezone = format.format(currentTimeStampInUtc);

but I get an exception "Cannot format given Object as a Date". Any suggestions how to forma the Date object to the given format?

Joel
  • 85
  • 4
  • 3
    Unless you have absolutely no alternative, please do not use `Date` and `SimpleDateFormat`. Please use the more modern and more robust `java.time` classes. [Background info](https://stackoverflow.com/q/32437550/12567365). [More background info](https://www.baeldung.com/migrating-to-java-8-date-time-api). – andrewJames Mar 24 '23 at 23:22
  • @andrewJames 100 % agree. And while one sometimes has no alternative to getting a `Date` from a legacy API that one cannot upgrade, the OP certainly has a better alternative to `SimpleDateFormat`, a notorious troublemaker of a class. Convert the old-fashioned `Date` to a modern `Instant` using its `toInstant` method and perform further conversions from there. – Ole V.V. Mar 25 '23 at 05:10
  • I cannot reproduce. Your code runs fine for me and produces `2023-03-24T22:31:00+02:00`. Please double-check whether you have in fact posted the code that produced the exception into your question. Better yet, create a [mre]. – Ole V.V. Mar 25 '23 at 05:19
  • The easy and modern way to get your desired string from an old-fashioned `Date` object would be `currentTimeStampInUtc .toInstant() .atZone(ZoneId.systemDefault()) .format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)`. It gives the same result as your code (may also print the milliseconds if they are non-zero, which for most purposes would be an advantage). – Ole V.V. Mar 25 '23 at 05:23
  • 1
    Does this answer your question? [Java : Cannot format given Object as a Date](https://stackoverflow.com/questions/10649782/java-cannot-format-given-object-as-a-date). [My answer there](https://stackoverflow.com/a/52405866/5772882) uses java.time as I recommend. Tip: paste your error message into your search engine. – Ole V.V. Mar 25 '23 at 05:28

1 Answers1

2

Here is how you could do it. I recommend using the newer and more robust classes from the java.time package.

String dateTime = "Mar 24 22:31:00 EET 2023";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("MMM dd HH:mm:ss z yyyy");
ZonedDateTime zdt = ZonedDateTime.parse(dateTime,dtf);
System.out.println(zdt);

Now print the parsed local date time in its default format, per the ISO 8601 standard.

System.out.println(zdt);

prints

2023-03-24T22:31+02:00[Europe/Bucharest]

Now format the output to force the appearance of seconds even when zero.

DateTimeFormatter odtf = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");
String output = zdt.format(odtf);
System.out.println(output);

prints

2023-03-24T22:31:00+0200

The only difference is the 00 seconds on the last output. Had the seconds been non-zero, the two outputs would have been the same.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • Hi good answer, but I don't see the timezone in the result of the ouput. If the time is 22;03 EET I want in the output to be like 2023-03-024T21:03:00+01:00 , because 01:00 is EET offset right. – Joel Mar 25 '23 at 10:01
  • 1
    That's because you didn't explicitly require it (per the specified format). I updated my question to to use the appropriate classes. – WJS Mar 25 '23 at 15:55
  • @Joel By the way, `EET` is not a [real time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). Real time zones have a name in `Continent/Region` format. Ex: `Asia/Beirut` & `Europe/Sofia`. – Basil Bourque Mar 26 '23 at 01:07