0

I get informations on entity via jpa . the date 31/12/2018 dd/MM/YYYY became 31/12/2019. I must be wrong on a simpledateformat option.

LOG.info("date de création :" +entite.getCreatedt ());
SimpleDateFormat sdf =new SimpleDateFormat ( "dd/MM/YYYY" );
LOG.info("date de création formatée :" + sdf.format(entite.getCreatedt ()));

the createdt field from my entity is defined as above :

@Column(name = "CREATEDT")
@Temporal(TemporalType.DATE)
private Date createdt;

I get the result : Infos: date de création :Mon Dec 31 00:00:00 CET 2018 Infos: date de création formatée :31/12/2019

can you explain why the 2018 became 2019 I tried with

sdf = new SimpleDateFormat ( "dd/MM/YYYY", new Locale("fr", "FR") );

but the result was the same

thanks

willyodin
  • 1
  • 1
  • 5
    "Y" is the wrong letter for the year, use lower case "y" (upper case means "week-based-year") – Hulk Jan 09 '19 at 10:07
  • 3
    Possible duplicate of [Difference between year-of-era and week-based-year?](https://stackoverflow.com/questions/26431882/difference-between-year-of-era-and-week-based-year) (refers to [`DateTimeFormatter`](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html), but is also valid for [`SimpleDateFormat`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html)) – Hulk Jan 09 '19 at 10:08
  • 2
    Also see https://stackoverflow.com/questions/14755742/java-date-year-calculation-is-off-by-year-for-two-days – Hulk Jan 09 '19 at 10:17
  • 1
    Why are you still using `Date` and `SimpleDateFormat`, those troublesome and long outdated classes? A modern JPA implementation should handle `LocalDate` from [java.time, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) for you. Then use a `DateTimeFormatter` for formatting it. – Ole V.V. Jan 09 '19 at 10:32

0 Answers0