1

I am using:

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@JsonFormat( pattern = "MM-dd-yyyy" )
private LocalDate start;

but it accept 02-30-2019 and convert to 02-28-2019 Automatically. But i want restrict that date.

I also used:

@DateTimeFormat(iso = java.time.format.DateTimeFormatter.ISO_DATE)
@JsonFormat( pattern = "MM-dd-yyyy" )
private LocalDate start;

but it give compile time error: Attribute value must be constant.

ankit here:

I have same issue and also used u in place of y but not help:

@FutureOrPresent
@DateTimeFormat( iso = DateTimeFormat.ISO.DATE,pattern = "MM-dd-uuuu")
@JsonFormat( pattern = "MM-dd-uuuu" )
private LocalDate start;

i want to restrict at time of parsing. It accept 02-31-2019 and convert automatically to 02-28-2019. refer: https://stackoverflow.com/a/41104034/6097074

Now 09/08/2018

If i am using: private LocalDate start;//without using DateTimeFormat and JsonFormat annotation

this work fine if i use: yyyy-MM-dd format date in json i.e. 2014-01-01. But i need to parse to MM-dd-yyyy.

Kind help to solve this, Thanks.

ankit
  • 2,591
  • 2
  • 29
  • 54
Abhinay Kumar
  • 101
  • 1
  • 8
  • Can you please share some code or even try to make your question a bit more understandable? It's not clear what you're trying to achieve. – akortex Sep 01 '18 at 14:54
  • @Aris_Kortex Please view question again. i editted – ankit Sep 04 '18 at 06:37
  • @ankit An invalid date (like 2019-02-30) cannot be parsed with `DateTimeFormatter.ISO_DATE`. Maybe your problem is rather in the JSON-layer. – Meno Hochschild Sep 05 '18 at 14:51
  • @MenoHochschild you are right, An invalid date (like 2019-02-30) cannot be parsed with `DateTimeFormatter.ISO_DATE`. But i am not able to use `DateTimeFormatter.ISO_DATE`. Please read question carefully. Thanks – ankit Sep 06 '18 at 05:40
  • @ankit Are you using Joda-Time instead when specifying the annotation `@DateTimeFormat`? That is not the same. – Meno Hochschild Sep 06 '18 at 05:49
  • No we are not using joda in any case. – ankit Sep 06 '18 at 05:59
  • @MenoHochschild please reply? – ankit Sep 07 '18 at 14:04
  • @ankit Hm, looking at the [spring source code](https://github.com/spring-projects/spring-framework/blob/8aa6e5bfea2c7314deaa1b432554e9e914b09ee7/spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeFormatterFactory.java#L180) does not seem to support your statement that Spring uses a non-strict parser, but I am not in the position to test it. – Meno Hochschild Sep 07 '18 at 14:52
  • @MenoHochschild Please check this: https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/format/annotation/DateTimeFormat.java – Abhinay Kumar Sep 07 '18 at 14:58
  • @ankit I can't understand it. Please help – Abhinay Kumar Sep 07 '18 at 14:59
  • @MenoHochschild yes as per your link springframwork code ude ristrict but !!. Please refer abhinay link if you understand this. Thanks – ankit Sep 07 '18 at 15:04
  • @ankit I have read the link and can only say: Verify that Spring 4 and Java 8+ are used.That means check if JSR-310 (=java.time-API) is really used under the hood (configuration issue?). Another question is: Which annotation will be used if setting the value of field "start"? If it is not DateTimeFormat but JSonFormat then the JSonFormat might use a non-strict parser. Have you checked it (for example by leaving out the annotation for JSon)? – Meno Hochschild Sep 08 '18 at 02:59
  • @MenoHochschild i am using Spring boot 2.0.4.RELEASE and yes i also think that this is problem by `com.fasterxml.jackson.annotation.@JsonFormat` annotation. But without using it i am getting `Parsing error: cannot Deserialize`. – ankit Sep 08 '18 at 09:15
  • @MenoHochschild yes JSR-310 is used – ankit Sep 08 '18 at 09:23
  • @MenoHochschild Please check edited question. – ankit Sep 08 '18 at 10:45

1 Answers1

5

I got solution for your problem:

remove below line

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
@JsonFormat( pattern = "MM-dd-yyyy" )

and just use:

private LocalDate start;

and send date in yyyy-MM-dd or yyyy-MM-ddTHH:mm:ss (default date format for Date parsing in spring with ResolverStyle.Strict) date format.

AKA
  • 618
  • 1
  • 5
  • 12