287

I have a LocalDate variable called date. When I print it, it displays 1988-05-05. I need to convert this to be printed as 05.May 1988. How can I do this?

1
  • 3
    Take a look at the DateTimeFormatter class. It even has examples of how to convert to and from Strings. Commented Jan 27, 2015 at 18:24

8 Answers 8

492

SimpleDateFormat will not work if he/she is starting with LocalDate, which is new in Java 8. From what I can see, you will have to use DateTimeFormatter.

LocalDate localDate = LocalDate.now(); // For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);

That should print 05 May 1988. To get the period (AKA full stop) after the day and before the month, you might have to use "dd'.LLLL yyyy".

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

1 Comment

Thank you very much,this helps,but it didn't work with 'LLLL' to show name of the month but with 'MMMM' i found this strange since even in the documentation it says otherwise.But once again thank you for helping me.
117

It could be short as:

LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));

Comments

19

java.time

Unfortunately, all existing answers have missed a crucial thing, Locale.

A date-time parsing/formatting type (e.g., DateTimeFormatter of the modern API or SimpleDateFormat of the legacy API) is Locale-sensitive. The symbols used in its pattern print the text based on the Locale used with them. In absence of a Locale, it uses the default Locale of the JVM. Check this answer to learn more about it.

The text in the expected output, 05.May 1988 is in English and thus, the existing solutions will produce the expected result only as a result of mere coincidence (when the default Locale of the JVM an English Locale).

Solution using java.time, the modern date-time API*:

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

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.of(1988, 5, 5);
        final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd.MMMM uuuu", Locale.ENGLISH);
        String output = dtf.format(date);
        System.out.println(output);
    }
}

Output:

05.May 1988

Here, you can use yyyy instead of uuuu, but I prefer u to y.

Learn more about the modern date-time API from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Comments

8

Use:

System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd.MMMM yyyy")));

This shows it for today.

1 Comment

ofPatterns needs android O
1

With the help of ProgrammersBlock's post, I came up with this.

My needs were slightly different. I needed to take a string and return it as a LocalDate object. I was handed code that was using the older Calendar and SimpleDateFormat. I wanted to make it a little more current. This is what I came up with.

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

void ExampleFormatDate() {

    LocalDate formattedDate = null;  // Declare LocalDate variable to
                                     // receive the formatted date.
    DateTimeFormatter dateTimeFormatter;  // Declare date formatter
    String rawDate = "2000-01-01";  // Test string that holds a
                                    // date to format and parse.

    dateTimeFormatter = DateTimeFormatter.ISO_LOCAL_DATE;

    // formattedDate.parse(String string) wraps the
    // String.format(String string, DateTimeFormatter format) method.
    //
    // First, the rawDate string is formatted according
    // to DateTimeFormatter. Second, that formatted
    // string is parsed into the LocalDate formattedDate object.
    //
    formattedDate = formattedDate.parse(String.format(rawDate, dateTimeFormatter));
}

1 Comment

I see a better way: just LocalDate.parse(rawDate) ! String.format is being used in a total wrong form here, actually just returning the first string; the DateTimeFormatter is not being used at all; parse is a static method of LocalDate; formattedDate is not a formatted date, not even a String (as requested in question)
0

Note that this works correctly in JDK 15 and newer

Most Latin-based scripts (languages), like English, French, etc., use Western Arabic digits for their numbers. But, some locales like fa (for Farsi/Persian) use different Unicode characters for their digits and numerals.

To get proper numbers and decimal styles of the locale, use DateTimeFormatter::localizedBy):

// To override/force the digits to Latin, update the locale to this:
// var locale = Locale.forLanguageTag("fa-u-nu-latn"); // Fa with Latin numbers
var locale = Locale.forLanguageTag("fa");
var myDate = LocalDateTime.parse("2025-06-28T00:00:00");
var result = DateTimeFormatter

        // OR DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
        // OR DateTimeFormatter.ISO_LOCAL_DATE_TIME
        // OR ...
        .ofPattern("yyyy-MM-dd")

        // To get proper numbering system and localized digits (on JDK 15+),
        // it is required to add this .localizedBy(locale) to the formatter instance.
        // Adding a call to .withLocale(locale) or using the locale in the creation of the
        // formatter instance (like in previous .ofPattern("...", locale)) does not work.
        // See https://www.oracle.com/java/technologies/javase/15-relnote-issues.html#JDK-8244245
        .localizedBy(locale)

        // Can again override the digits with either of these calls:
        // .withDecimalStyle(DecimalStyle.of(Locale.ENGLISH))
        // .withDecimalStyle(DecimalStyle.of(Locale.forLanguageTag("fa-u-nu-latn")))

        .format(myDate); // ۲۰۲۵-۰۶-۲۸

For Java 14 and older, use this code:

// ...
    .localizedBy(locale)
    .withDecimalStyle(DecimalStyle.of(locale))
    .format(myDate)

See How can I localize numbers with DateTimeFormatter?.

Comments

-5

There is a built-in way to format LocalDate in Joda library

import org.joda.time.LocalDate;

LocalDate localDate = LocalDate.now();
String dateFormat = "MM/dd/yyyy";
localDate.toString(dateFormat);

In case you don't have it already - add this to the build.gradle:

implementation 'joda-time:joda-time:2.9.5'

Happy coding! :)

7 Comments

Not according to the javadoc.
you want me to prove that LocalDate.toString(String pattern) does NOT exist? Please provide a reference to that method if it does exist - I haven't been able to find it in any javadocs. As for a solution, this question already has that.
Joda Time is not "built-in". OP seems to be using java.time.LocalDate, not joda.time
We are using org.joda.time.LocalDate for ages, OP may be using it as well.
Yes, this is the way for joda time. Thank you @Inoy
|
-19

A pretty nice way to do this is to use SimpleDateFormat. I'll show you how:

SimpleDateFormat sdf = new SimpleDateFormat("d MMMM YYYY");
Date d = new Date();
sdf.format(d);

I see that you have the date in a variable:

sdf.format(variable_name);

1 Comment

OP wants to print the content of a LocalDate instance in a specific format. How could SimpleDateFormat help him here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.