0

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
7
  • The toString() output of Date objects 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. Commented Jun 4, 2024 at 3:10
  • 1
    You wrote (in your question): the database value of this createDate (which is YYYY-MM-DD format) If you are referring to a column of a table in the database, then if the data type of that column is a DATE type, then it has no format. It is displayed using some format but it is not stored in that format. If you are using at least Java 8, the use the date-time API. Commented Jun 4, 2024 at 3:45
  • 1
    What database engine? And what exactly is the data type of your date column? Commented Jun 4, 2024 at 5:56
  • 3
    Please for your sanity don’t use Date and SimpleDateFormat. 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. Commented Jun 4, 2024 at 6:37
  • 1
    private Date createDate; No. private LocalDate createDate; is what you want with formatting done at presentation time (e.g. toString()) as per the answer posted Commented Jun 4, 2024 at 8:08

1 Answer 1

2

You can use java.time.LocalDate and java.time.format.DateTimeFormatter to do this.

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

LocalDate date = LocalDate.of(2024, 4, 5); // year, month, day

// set the date pattern
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy");

String formattedDate = date.format(formatter);

April 5, 2024

If you want other than default human language and cultural norms for localizing, pass a Locale to DateTimeFormatter#withLocale.

Sign up to request clarification or add additional context in comments.

5 Comments

Why the down-vote and Delete-vote on this Answer? Seems correct and wise. Except that SimpleDateFormat is not actually deprecated, though it should be.
@BasilBourque - my guess: because it might be answering an 'XY question' thus adding to OP confusion. Myself, I'm not sure if it's XY.
@BasilBourque perhaps because it was downvoted before it was edited to change from SimpleDateFormat.
Thanks for having faith in me guys! Yeah, I think SimpleDateFormat should be deprecated, maybe I just jumped to conclusions when the log said that the program used or overrode a deprecated API, which I assumed was SimpleDateFormat
An alternative is DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM), which will produce the desired output and will also be automatically correct for all the other locales supported by Java.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.