0

How can I reformat this date time even I'm putting this annotation in my java code :

 @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date dateFinContrat;

and in my html file :

<td th:text="${#dates.format(row.dateNaissance, 'dd/MM/yyyy')}"></td> 

what i want is to remove the hh:mm:ss in my web page. it shows like this :

this is how it looks like

  • I strongly recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Aug 27 '22 at 06:56
  • In your HTML code you are showing how you are formatting *date de naissance* (birthday) to `dd/MM/yyyy`, and in your image it shows nicely as `07/07/2000`. So what your question is is not perfectly clear to me? – Ole V.V. Aug 27 '22 at 06:58
  • @OleV.V. yes it shows correctly the dd//mm//yyyy but i dont want to show the hh:mm:ss –  Aug 27 '22 at 09:12
  • You have got two date fields. If I am not mistaken you are showing us how you are formatting *Date de naissance*, and that fields *is* showing without 00:00:00. You are not showing us how you may be formatting the *Date Fin Contrat*, so we can’t really help you with what may be going wrong there. Voting to close as needing clarity. – Ole V.V. Aug 27 '22 at 09:17

2 Answers2

0

2050-07-07 00:00:00.0 As you can see , there's a space before that 00:00:00.

2050-07-07 00:00:00.0

So first try to split that string with 'Space'.

Vitamin
  • 21
  • 4
0

I would recommend formatting the date in either the service or the UI. Also try using LocalDate and LocalDateTime class which are latest class from java.utils.

  1. Annotation Based

If REST service with JSON response, Please use the following to format the date based on the pattern:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")

or

@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
  1. Using DateTimeFormatter or SimpleDateFormat Utility Class

SimpleDateFormat Code Snippet:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
String formattedDate = formatter.format(date); 

DateTimeFormatter Code Snipped:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
formatter.formate(date); 

Please reference: @DateTimeFormat(pattern = "yyyy-MM-dd hh:mm:ss") is Not Working

Dhruv Patel
  • 346
  • 2
  • 7