1

Im getting the following error when I try to convert the following string. Id like the Date to be in the format yyyy-MM-dd'T'HH:mm:ss:SSS but instead the Date seems to be coming out as Sun Mar 01 23:00:01 GMT 2020

    String FULL_ISO_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS";
    SimpleDateFormat formatter = new SimpleDateFormat(FULL_ISO_DATE_FORMAT);
    Date from = formatter.parse("2020-03-01T23:00:01.000");

Error

feign.FeignException: status 400 reading Controller#searchController(Date,Date,Integer,String); content:
{"status":"fail","data":{"errors":[{"type":"IllegalArgumentException","description":"Invalid value Sun Mar 01 23:00:01 GMT 2020 for filter from. Field needs to be in format: yyyy-MM-dd'T'HH:mm:ss.SSS"}]}]}

Any help would be appreciated. I need to use the Date object as the constructor Im querying is using the Date object. Ideally I'd like to use LocalDateTime but I cant.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • You say that you get the error when you "try to convert the following string," but you don't specify the string; you only show the parsing pattern and an error message. Judging from the error message, I would assume that the string you are trying to parse is actually `Sun Mar 01 23:00:01 GMT 2020`, and that obviously won't work. Look for a step earlier in your request processing where you convert a `Date` instance to a `String` with its `toString()` method. This might be an implicit call somewhere. – erickson Mar 16 '20 at 21:43
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead just use `LocalDateTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Mar 17 '20 at 06:03
  • You are asking the impossible. A `Date` hasn’t got (as in cannot have) a format. What you are seeing when you see `Sun Mar 01 23:00:01 GMT 2020` is the result of implicitly calling its `toString` method when printing it. – Ole V.V. Mar 17 '20 at 06:04

2 Answers2

1

Use the LocalDateTime from java-8 date-time API and stop using legacy Date classes

String FULL_ISO_DATE_FORMAT = DateTimeFormatter. ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime dateTime = LocalDateTime.now();
dateTime.format(FULL_ISO_DATE_FORMAT);
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • I have to use the Date object, the constructor is using Date. When I try to use the LocalDateTime it gives mismatch errors in the class –  Mar 16 '20 at 19:12
  • can you show more code @KahunaDub with pointing to exact problem – Ryuzaki L Mar 16 '20 at 19:14
  • @KahunaDub I've edited my answer to show how to convert a ```LocalDateTime``` to the old ```Date```. This way you will have your compatibility. – akuzminykh Mar 16 '20 at 20:09
1

Please don't use the old classes Date and SimpleDateFormat. Use the new java.time api that is much more robust and better designed.


You can do the same thing as follows:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS");
LocalDateTime date = LocalDateTime.parse("2020-03-01T23:00:01.000", formatter);

Keep in mind that you can convert it to Date for compatibility like so:

Date legacyDate = Date.from(date.atZone(ZoneId.systemDefault()).toInstant());
akuzminykh
  • 4,522
  • 4
  • 15
  • 36