0

I have a simple date formatter with the following format:

private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat(DATE_FORMAT);
//accessExpiryDate is a Date object
DATE_FORMATTER.format(accessExpiryDate);

And it is formatting the date as yyyy-MM-dd. But I want to format the date such as "1 Jun 2019". day + first 3 letter of the month + year.

How can i achieve that? Is there a simple way/method/class or should i write my custom date formatter method?

abidinberkay
  • 1,857
  • 4
  • 36
  • 68
  • 4
    I suggest reading the [documentation for SimpleDateFormat](https://docs.oracle.com/javase/10/docs/api/java/text/SimpleDateFormat.html) - but also, if you can possibly use java.time instead of java.text/java.util, you'll have a better experience. – Jon Skeet Sep 16 '19 at 12:21
  • my date is a just Date class. – abidinberkay Sep 16 '19 at 12:29
  • Have you tried changing the DATE_FORMAT from "yyyy-MM-dd" to "d MMM YYYY"? – Nick Sep 16 '19 at 12:35
  • 1
    @abidinberkay: Yes, I'm suggesting that if you can possibly move to `java.time.LocalDateTime`, `java.time.Instant` etc you'll have a better experience. – Jon Skeet Sep 16 '19 at 12:58
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Sep 16 '19 at 16:47
  • Possible near-duplicate of [Format month of date to string of 3 first letters- Kotlin](https://stackoverflow.com/questions/56207152/format-month-of-date-to-string-of-3-first-letters-kotlin) – Ole V.V. Sep 16 '19 at 17:05

3 Answers3

6

java.time

Don’t use Date and SimpleDateFormat. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome. As Jon Skeet said, move to java.time, the modern Java date and time API, for a better experience.

    DateTimeFormatter dateFormatter
            = DateTimeFormatter.ofPattern("d MMM u", Locale.ENGLISH);
    LocalDate ld = LocalDate.of(2019, Month.JUNE, 1);
    System.out.println(ld.format(dateFormatter));

Output from this snippet is:

1 Jun 2019

A number of format pattern letters including M for month can yield either a number or a text depending on how many letters you put in the format pattern string (this is true for both DateTimeFormatter and the legacy SimpleDateFormat). So MM gives you two-digit month number, while MMM gives you a month abbreviation (often three letters, but could be longer or shorter in some languages).

If you are getting an old-fashioned Date object from a legacy API that you either cannot change or don’t want to upgrade just now, you may convert it like this:

    Date accessExpiryDate = getFromLegacyApi();
    LocalDate ld = accessExpiryDate.toInstant()
            .atZone(ZoneId.systemDefault())
            .toLocalDate();

The rest is as before. And now you’ve embarked on using the modern API and can migrate your code base in this direction at your own pace.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Similar thread: https://stackoverflow.com/questions/67089932/simpledateformat-format-month-september-jdk16 – SydMK Feb 22 '22 at 06:49
0

Since you have specified the date format as yyyy-MM-dd, it is formatted as that. Just fix your date format to the expected formatting.

e.g: d MMM yyyy

Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17
0

As already suggested by @JonSkeet, you should read the API documentation.
Anyway, the format should be in your case d MMM yyyy.

Robert Kock
  • 5,795
  • 1
  • 12
  • 20