0

I have a model where I need to accept only dates with the format "dd/MM/yyyy. For that I am using jackson JsonFormat .

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
 
import java.util.Date;
import java.util.UUID;
 
@Data
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class InventoryVolume {
 
  private UUID itemID;
 
  @JsonFormat(pattern = "dd/MM/yyyy")
  public Date inventoryStartDate;
 
  private UUID ItemSpecID;
 
}

Now I expect dates with format dd/MM/yyyy(max length 10) only to be accepted and rest all to throw an exception.

But looks like the only thing that is checked is "/" between dates.

All of these following formats are deserialized and converted to valid date!! Is this an expected behavior?

Year

"inventoryStartDate": "01/01/11ff"
"inventoryStartDate": "01/01/11fff"
"inventoryStartDate": "01/01/1111111111"
"inventoryStartDate": "01/01/11"

Month

"inventoryStartDate": "01/35/11"

Date

"inventoryStartDate": "023466234/12/11"

Simsons
  • 12,295
  • 42
  • 153
  • 269
  • By default it's using SimpleDateFormat which is lenient and I don't see any way to change that without rolling your own. – flip66 Jul 21 '20 at 06:20
  • https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#setLenient(boolean) – flip66 Jul 21 '20 at 06:20

0 Answers0