-1

I wrote a method to parse a date. When I'm passing the following date as an argument 2019-03-05 06:15:00, I'm getting a java.text.ParseException: Unparseable date: "2019-03-05 06:15:00".

What could be wrong with the logic?

public String convertDate(String val) throws ParseException {
    return "cast('" + new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").format(
            new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").parse(val)) + "' as ts)";
}
samba
  • 2,821
  • 6
  • 30
  • 85
  • I recommend you don’t use `SimpleDateFormat`. That class is notoriously troublesome and long outdated. 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. Mar 19 '19 at 12:10
  • Also don’t use `'Z'` in your format pattern string to parse or print `Z` as a literal. `Z` (pronounced *Zulu*) means UTC and needs to be parsed or printed as such, or you will get wrong results. – Ole V.V. Mar 19 '19 at 12:22
  • What are you *really* trying to obtain? It seems like you are dynamically generating an SQL expression or similar. For the vast majority of purposes that would be more complicated than what you really need. See [XY problem](https://en.wikipedia.org/wiki/XY_problem). – Ole V.V. Mar 19 '19 at 12:46

4 Answers4

1

You are trying to parse string 2019-03-05 06:15:00 using pattern yyyy-MM-dd'T'HH:mm'Z' that obviously does not fit.

Mikhail Vladimirov
  • 13,572
  • 1
  • 38
  • 40
1

Either change the method to following

public String convertDate(String val) throws ParseException {
        return "cast('" + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(
                new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").parse(val)) + "' as ts)";
}

or pass

2019-03-05T06:15Z
Rakesh
  • 4,004
  • 2
  • 19
  • 31
1

The Z represents the zone offset. Per documentation

Z zone-offset offset-Z +0000; -0800; -08:00;

So your String should be, for example

2019-03-05T06:15+0100

Your patterns doesn't seem to reflect what you want to accomplish. Z isn't just a simple letter.

yyyy-MM-dd'T'HH:mmZ

Anyway, avoid using SimpleDateFormat, use DateTimeFormatter instead, which is part of the new Time API for Java 8.

final String val = "2019-03-05T06:15+0100";
final TemporalAccessor parse = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mmZ").parse(val);
final String format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(parse);

By inputting

2019-03-05T06:15+01:00

you could even just use

final TemporalAccessor parsed = DateTimeFormatter.ISO_DATE_TIME.parse(val);
LppEdd
  • 20,274
  • 11
  • 84
  • 139
1

Well, nothing is wrong with your code except for this SS and change the order too.

s second-of-minute number 55

S fraction-of-second fraction 978

   public static String convertDate(String val) throws ParseException {
    return "cast('" + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'").format(
            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(val)) + "' as ts)";
}
MS90
  • 1,219
  • 1
  • 8
  • 17
  • I presume T is for Tag( day ), and Z is for Zeit ( time ) ? – MS90 Mar 19 '19 at 11:51
  • No, @MS90. T is for time (denotes where the time of day starts) and Z is the military name for “Zulu time zone” or UTC or UTC offset zero. – Ole V.V. Mar 19 '19 at 12:19
  • 1
    The answer is here: [ISO 8601 on Wikipedia](https://en.wikipedia.org/wiki/ISO_8601) ([Z here](https://en.wikipedia.org/wiki/ISO_8601#Coordinated_Universal_Time_(UTC)) and [T here](https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations)). But please wait for samba to confirm, of course. – Ole V.V. Mar 19 '19 at 12:39