-3

I want to change the date format from "20230523154325" to 2023-05-23T15:43:25+0000".
And I have this piece of code, but unfortunately the result is this: "2023-05-23T15:43:25+0300".
So I don't want to have +0300, I want to have +0000 at the end. So, it should be exactly the same for this part 2023-05-23T15:43:25, but instead of +0300 should be +0000. It's important to have this +0000 at the end. Any feedback will be appreciated!

public static void main(String[] args) {
    String oldDate = "20230523154325";
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    Date newDateFormat = null;

    try {
        newDateFormat = sdf.parse(oldDate);
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
    
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
    System.out.println("The date is " + sdf1.format(newDateFormat));
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
elvis
  • 956
  • 9
  • 33
  • 56
  • It's good to state *why* you need it there, because as it stands now, we can't tell which solution would work. For example, with the information you've given, you might as well do substring + "+0000", and it would get the task done as it stands now. With that being said, you can change time zones of SimpleDateFormat https://stackoverflow.com/questions/2891361/how-to-set-time-zone-of-a-java-util-date . That also means the time itself will change (by the 3 hour difference), so you'd need to account for that somehow. – Samuel Novelinka May 23 '23 at 12:17
  • 3
    First of all I strongly recommend you stop using `SimpleDateFormat` and `Date` immediately. The former is notoriously troublesome, both poorly designed and bad to work with and both long outdated. Use `LocalDateTime`, `DateTimeFormatter` and `ZoneOffset`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). – Ole V.V. May 23 '23 at 16:03
  • 1
    [UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time) is not a format. – Basil Bourque May 23 '23 at 16:43

2 Answers2

5

I want to change the date format from 20230523154325 to 2023-05-23T15:43:25+0000

You can do that with java.time if you are using Java 8 or higher.

Example Code

public static void main(String[] args) {
    // example input
    String oldDate = "20230523154325";
    // prepare a parser
    DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
    // parse the oldDate with the parser
    LocalDateTime ldt = LocalDateTime.parse(oldDate, parser);
    // print the result (NO OFFSET SO FAR!!!)
    System.out.println(ldt);
    // add an offset (UTC = +00:00 = Z)
    OffsetDateTime odt = ldt.atOffset(ZoneOffset.UTC);
    // print it
    System.out.println(odt);
    // prepare a formatter for the desired output
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssxxxx");
    // then print the datetime plus offset in the desired format
    System.out.println(odt.format(formatter));
}

Output

2023-05-23T15:43:25
2023-05-23T15:43:25Z
2023-05-23T15:43:25+0000

There are prebuilt ones for standard/common formats like DateTimeFormatter.ISO_OFFSET_DATE_TIME, which are worth a look. However, you have to create those two DateTimeFormatters manually in this case, because none of the prebuilt ones can parse your input format and none of them can produce your desired output.


Why did your attempt fail to produce the desired result?

Your attempt involves the zone/offset of your machine/jvm, which will even change the offset when the code is executed on another machine with a different zone/offset.

That's why you

  • should stop using java.util.Date and java.text.SimpleDateFormat whenever/wherever you can…
  • should switch to java.time because you have full control over zone/offset handling

Compact / Short Version

public static void main(String[] args) {
    String oldDate = "20230523154325";
    DateTimeFormatter parser = DateTimeFormatter.ofPattern("uuuuMMddHHmmss");
    DateTimeFormatter formatter = DateTimeFormatter
                                    .ofPattern("uuuu-MM-dd'T'HH:mm:ssxxxx");
    String desiredResult = LocalDateTime.parse(oldDate, parser)
                                        .atOffset(ZoneOffset.UTC)
                                        .format(formatter);
    System.out.println(desiredResult);
}

Output

2023-05-23T15:43:25+0000
deHaar
  • 17,687
  • 10
  • 38
  • 51
0

Don’t use SimpleDateFormat. SimpleDateFormat instances have a timezone, which causes misleading parsing results like what you have observed.

Given the simplicity of your particular use case, I would forget about date-time parsing entirely, and just use a StringBuilder to insert characters:

String oldDate = "20230523154325";
StringBuilder formattedDate = new StringBuilder(oldDate);
formattedDate.append("+0000");
formattedDate.insert(12, ':');
formattedDate.insert(10, ':');
formattedDate.insert(8, 'T');
formattedDate.insert(6, '-');
formattedDate.insert(4, '-');
System.out.println("The date is " + formattedDate);
VGR
  • 40,506
  • 4
  • 48
  • 63
  • That ought to work. It’s too hand-held for my taste and doesn’t give us the validation you get for free when using the date-time library (read: java.time). – Ole V.V. May 23 '23 at 19:00