I have a model class with 'Date' type field.
//Model.java
private Date createDate;
I am trying to convert the database value of this createDate (which is YYYY-MM-DD format) to MMM d, YYYY, i.e., I want the date to be displayed as April 5, 2024.
I am able to get this format in "String". But if I use 'Date' it prints as "Thurs Apr 5 0:00:00 EDT 2024" . How I can make the object to print ONLY "April 5, 2024"?
String string = "January 2, 2010";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);//Sat Jan 02 00:00:00 EDT 2010
toString()output ofDateobjects is what it is - you cannot modify it. Whatever makes you think you need to modify it - you don't. Anytime you have a Date object and it needs to be rendered anywhere, run it through a formatter. In other words, the answer to your question is: Impossible.DateandSimpleDateFormat. They were badly designed and troublesome and have been outdated for 10 years now. Use java.time, the modern Java date and time API, for your date work.private LocalDate createDate;is what you want with formatting done at presentation time (e.g.toString()) as per the answer posted