How to format LocalDate to string?

JavaJsr310

Java Problem Overview


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

Java Solutions


Solution 1 - Java

SimpleDateFormat will not work if he is starting with LocalDate which is new in Java 8. From what I can see, you will have to use DateTimeFormatter, http://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html.

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 after the day and before the month, you might have to use "dd'.LLLL yyyy"

Solution 2 - Java

Could be short as:

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

Solution 3 - Java

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.

Solution 4 - Java

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

The above answer shows it for today

Solution 5 - Java

With the help of ProgrammersBlock posts 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));
    
}

Hopefully this will help someone, if anyone sees a better way of doing this task please add your input.

Solution 6 - Java

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! :)

Solution 7 - Java

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);

Cheers.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJaskoView Question on Stackoverflow
Solution 1 - JavaProgrammersBlockView Answer on Stackoverflow
Solution 2 - JavaZonView Answer on Stackoverflow
Solution 3 - JavaArvind Kumar AvinashView Answer on Stackoverflow
Solution 4 - Javakarthik rView Answer on Stackoverflow
Solution 5 - JavapeteView Answer on Stackoverflow
Solution 6 - JavaInoyView Answer on Stackoverflow
Solution 7 - JavanomView Answer on Stackoverflow