Convert LocalDate to String in Java

In this example, we will demonstrate how to convert LocalDate to String in Java with an example.

LocalDate represents a year-month-day in the ISO calendar and is useful for representing a date without a time. You might use a LocalDate to track a significant event, such as birth date or wedding date.

Read more about LocalDate class with an example at https://www.javaguides.net/2018/08/java-8-localdate-class-api-guide.html.

Convert LocalDate to String in Java Example

LocalDate class provides below API to convert from LocalDate to String in Java.
  • String format(DateTimeFormatter formatter) - Formats this date using the specified formatter.
package com.ramesh.java8.datetime.api; import java.time.LocalDate; import java.time.format.DateTimeFormatter; /**  * Program to demonstrate LocalDate Class APIs.  * @author javaguides.net  *  */ public class LocalDateExamples { public static void main(String[] args) { convertLocalDatetoString(); } private static void convertLocalDatetoString() { // ISO Date LocalDate localDate = LocalDate.now(); DateTimeFormatter dateFormatter = DateTimeFormatter.ISO_LOCAL_DATE; System.out.println(localDate.format(dateFormatter)); // yyyy/MM/dd pattern DateTimeFormatter dateFormatter1 = DateTimeFormatter.ofPattern("yyyy/MM/dd"); System.out.println(localDate.format(dateFormatter1)); // MMMM dd, yyyy pattern DateTimeFormatter dateFormatter2 = DateTimeFormatter.ofPattern("MMMM dd, yyyy"); System.out.println(localDate.format(dateFormatter2)); // dd-MMM-yyyy pattern DateTimeFormatter dateFormatter3 = DateTimeFormatter.ofPattern("dd-MMM-yyyy"); System.out.println(localDate.format(dateFormatter3)); // dd-LL-yyyy pattern DateTimeFormatter dateFormatter4 = DateTimeFormatter.ofPattern("dd-LL-yyyy"); System.out.println(localDate.format(dateFormatter4)); } }
Output:
2018-08-10 2018/08/10 August 10, 2018 10-Aug-2018 10-08-2018

Reference



Comments