Close

Java Date Time - LocalDate.format() Examples

Java Date Time Java Java API 


Class:

java.time.LocalDate

java.lang.Objectjava.lang.Objectjava.time.LocalDatejava.time.LocalDatejava.time.temporal.TemporalTemporaljava.time.temporal.TemporalAdjusterTemporalAdjusterjava.time.chrono.ChronoLocalDateChronoLocalDatejava.io.SerializableSerializableLogicBig

Method:

public String format(DateTimeFormatter formatter)

Returns the formatted string as specified by the provided DateTimeFormatter.

This method is defined as "default" method in ChronoLocalDate interface.


Examples:


package com.logicbig.example.localdate;

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

public class FormatExample {
public static void main (String[] args) {
LocalDate localDate = LocalDate.of(1990, Month.MAY, 20);

String s = localDate.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("BASIC_ISO_DATE: " + s);

s = localDate.format(DateTimeFormatter.ISO_DATE);
System.out.println("ISO_DATE: " + s);

s = localDate.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("ISO_LOCAL_DATE: " + s);

s = localDate.format(DateTimeFormatter.ISO_ORDINAL_DATE);
System.out.println("ISO_ORDINAL_DATE: " + s);

s = localDate.format(DateTimeFormatter.ISO_WEEK_DATE);
System.out.println("ISO_WEEK_DATE: " + s);
}
}

Output

BASIC_ISO_DATE: 19900520
ISO_DATE: 1990-05-20
ISO_LOCAL_DATE: 1990-05-20
ISO_ORDINAL_DATE: 1990-140
ISO_WEEK_DATE: 1990-W20-7




package com.logicbig.example.localdate;

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

public class FormatExample2 {
public static void main (String[] args) {

LocalDate localDate = LocalDate.of(1990, Month.MAY, 20);

String s = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println("FULL: " + s);

s = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
System.out.println("LONG: " + s);

s = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("MEDIUM: " + s);

s = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
System.out.println("SHORT: " + s);

s = localDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)
.withLocale(Locale.GERMAN));
System.out.println(s);
}
}

Output

FULL: Sunday, May 20, 1990
LONG: May 20, 1990
MEDIUM: May 20, 1990
SHORT: 5/20/90
Sonntag, 20. Mai 1990




package com.logicbig.example.localdate;

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

public class FormatExample3 {
public static void main (String[] args) {

LocalDate localDate = LocalDate.of(1990, Month.JULY, 20);

String s = localDate.format(DateTimeFormatter.ofPattern("YYYY/MM/d"));
System.out.println("YYYY/MM/dd: " + s);

s = localDate.format(DateTimeFormatter.ofPattern("D, MMMM"));
System.out.println("D, MMM: " + s);

s = localDate.format(DateTimeFormatter.ofPattern("D, M"));
System.out.println("D, M: " + s);

s = localDate.format(DateTimeFormatter.ofPattern("'Quarter/Year: 'Q/y"));
System.out.println(s);
}
}

Output

YYYY/MM/dd: 1990/07/20
D, MMM: 201, July
D, M: 201, 7
Quarter/Year: 3/1990




See Also