0

I'm using SimpleDateFormat in Java and for some reason it is not detecting the month. Here is my code:

     /**
     * Takes a date/time stamp which looks like yyyyMMDDhhmmss on converts it to
     * yyyy-MM-DDThh:mm:ssZ
     *
     * @param timestamp
     * @return
     */
    public static String ConvertDate(String timestamp) {
        if (timestamp == null || timestamp.length() < 14)
            return null;

        String timeString = null;

        System.out.println(timestamp);
        DateFormat outputFormat = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss'Z'");
        DateFormat inputFormat = new SimpleDateFormat("yyyyMMDDhhmmss");

        try{
            Date date = inputFormat.parse(timestamp);
            timeString = outputFormat.format(date);
        }
        catch(Exception e){}

        return timeString;
    }

Calling this method: ConvertDate("20190803122424") returns the following: 2019-01-03T12:24:24Z whereas I want to return: 2019-08-03T12:24:24Z

Is there something wrong with my output format?

Smi28
  • 57
  • 1
  • 11
  • 1
    Have you tried changing the "yyyyMMDDhhmmss" to "yyyyMMddHHmmss" (lowercase D, uppercase H)? – BBacon Nov 12 '19 at 18:15
  • It's worth noting that calling `setLenient(false)` causes `parse` to throw an exception in this case. – Ian McLaird Nov 12 '19 at 18:22
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `OffsetDateTime` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Nov 12 '19 at 19:13
  • Not only your input format, your output format pattern string too has a couple of instances of wrong case of pattern letters. Furthermore `'Z'` with quotes is bound to give you incorrect results. – Ole V.V. Nov 12 '19 at 19:14

2 Answers2

5

You are using the wrong date format string: DD (day in year) instead of dd (day in month). Change both SimpleDateFormat instance to use dd:

DateFormat inputFormat = new SimpleDateFormat("yyyyMMddhhmmss");
DateFormat outputFormat = new SimpleDateFormat("YYYY-MM-dd'T'hh:mm:ss'Z'");

Therefore you are getting the wrong result.

Robert
  • 39,162
  • 17
  • 99
  • 152
4

As everyone pointed your formatter yyyyMMDDhhmmss is wrong, so create DateTimeFormatter with valid format

DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

And by using java-8 date time API parse it into LocalDateTime and then to UTC format using ZonedDateTime

String dateString = "20190803122424";

LocalDateTime localDateTime = LocalDateTime.parse(dateString,inputFormat);

And then you can convert it into OffsetDateTime

 OffsetDateTime outputDateTime = localDateTime.atOffset(ZoneOffset.UTC);

In case if you particularly want ZonedDateTime

ZonedDateTime outputDateTime = localDateTime.atZone(ZoneOffset.UTC);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • Thanks i updated the approach to use `OffsetDateTime` @BasilBourque – Ryuzaki L Nov 12 '19 at 20:22
  • 2
    Thanks for the thought, but no need to credit me. I suggest replacing your `ZonedDateTime` line entirely with `OffsetDateTime`. Also, I suggest breaking your `LocalDateTime` object into its own line for clarity. So two lines: `LocalDateTime localDateTime = …` and `OffsetDateTime offsetDateTime = …`. – Basil Bourque Nov 12 '19 at 20:29