Java Localdate format() method with yyyy-MM-dd pattern
In this tutorial, we will cover Java LocalDate format method to convert LocalDate to String with default(yyyy-MM-dd) pattern, user-defined patterns, library defined patterns, and custom patterns provided by java library.
Here is the brief intro about what is LocalDate class in java.
LocalDate
- LocalDate class is immutable and thread-safe.
- This is a value-based class thus use of equals method is recommended for comparisons.
- LocalDate provides date format as YYYY-MM-dd.
- The LocalDate class does not have time or timezone data, So LocalDate is preferable for Birthday, National Holiday representation.
LocalDate format() method
Method Declaration:
public String format(DateTimeFormatter formatter)
This method formats this date using the specified formatter. This date will be passed to the formatter to produce the date to string type.
Specified by:
the format method is present in interface ChronoLocalDate.
Parameters:
formatter - the formatter to use, which should be not null.
Returns:
the formatted date string, which will be not null.
Throws:
DateTimeException - if an error occurs during printing.
LocalDate format() method Examples:
1. Default Pattern
If we use the LocalDate.now() method to create LocalDate Instance and we print it.thie will print date in default format which is yyyy-MM-dd.
If we use LocalDate.toString() method that will show us date object in string format with default pattern which is yyyy-MM-dd.
Example:
import java.time.LocalDate;
public class LocalDateFormatExample1 {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
System.out.println("Using now() : " + currentDate);// 2020-06-27
// Prints format in yyyy-MM-dd format by default.
// The output will be in the ISO-8601 format
// in string with pattern yyyy-MM-dd
System.out.println("Using toString() :" + currentDate.toString());
}
}
Output:Using now() : 2020-06-27
Using toString() :2020-06-27
2. Custom Pattern
To format the LocalDate instance to string in any other patterns, we must use LocalDate.format(DateTimeFormatter) method passing formatter value.
2.1 User-Defined Pattern Example
If we want to convert our date in our own specified format we can define our own pattern and use it.
Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class LocalDateFormatExample2 {
public static void main(String[] args) {
// Prints date in default format yyyy-MM-dd
LocalDate now = LocalDate.now();
System.out.println("Date with pattern yyyy-MM-dd : " + now);
// Prints date in MMM-dd-yyyy format
String formattedDate1 = now.format(DateTimeFormatter.ofPattern("MMM-dd-yyyy"));
System.out.println("Date with pattern MMM-dd-yyyy : " + formattedDate1); // prints Jun-27-2020
// Prints date in dd MMM yyyy format
String formattedDate2 = now.format(DateTimeFormatter.ofPattern("dd MMM yyyy"));
System.out.println("Date with pattern dd MMM yyyy : " + formattedDate2); // prints 27 Jun 2020
// Prints date in YYYY, dd MMM format with default locale
String formattedDate3 = now.format(DateTimeFormatter.ofPattern("yyyy, dd MMM", Locale.getDefault()));
System.out.println("Date with pattern yyyy, dd MMM with Locale Default : " + formattedDate3); // prints 2020, 27
}
}
Output:
Date with pattern yyyy-MM-dd : 2020-06-27
Date with pattern MMM-dd-yyyy : Jun-27-2020
Date with pattern dd MMM yyyy : 27 Jun 2020
Date with pattern yyyy, dd MMM with Locale Default : 2020, 27 Jun
2.2 Library Defined Pattern Example
Here in this DateTimeFormatter have Enum Constants which we can use if suits best for our use-case.
Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class LocalDateFormatExample3 {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
// The ISO date formatter that formats or parses a date without an
// offset, such as '20111203'
// Prints date in yyyymmdd format
String formattedDate1 = now.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println("BASIC_ISO_FORMAT : " + formattedDate1);
// The ISO date formatter that formats or parses a date with the
// offset if available, such as '2011-12-03' or '2011-12-03+01:00'.
// Prints date in default pattern yyyy-mm-dd format
String formattedDate2 = now.format(DateTimeFormatter.ISO_DATE);
System.out.println("ISO_DATE : " + formattedDate2);
// The ISO date formatter that formats or parses a date without an
// offset, such as '2011-12-03'
// Prints date in default pattern yyyy-mm-dd format
String formattedDate3 = now.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println("ISO_LOCAL_DATE : " + formattedDate3);
// The ISO date formatter that formats or parses the week-based date
// without an offset, such as '2012-W48-6'
// Prints date in year-weeknumber-monthnumber format
String formattedDate4 = now.format(DateTimeFormatter.ISO_WEEK_DATE);
System.out.println("ISO_WEEK_DATE : " + formattedDate4);
// The ISO date formatter that formats or parses the ordinal date
// without an offset, such as '2012-337'
// Prints date in year-dayofyear format
String formattedDate5 = now.format(DateTimeFormatter.ISO_ORDINAL_DATE);
System.out.println("ISO_ORDINAL_DATE : " + formattedDate5);
}
}
Output:
BASIC_ISO_FORMAT : 20200627
ISO_DATE : 2020-06-27
ISO_LOCAL_DATE : 2020-06-27
ISO_WEEK_DATE : 2020-W26-6
ISO_ORDINAL_DATE : 2020-179
2.3 Custom Defined Pattern Example
Example:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
public class LocalDateFormatExample4 {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
// Prints date as 6/27/20
String formattedDateShort = currentDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
System.out.println("SHORT Format : " + formattedDateShort);
// Prints date as Jun 27, 2020
String formattedDateMedium = currentDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
System.out.println("MEDIUM Format : " + formattedDateMedium);
// Prints date as June 27, 2020
String formattedDateLong = currentDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
System.out.println("LONG Format : " + formattedDateLong);
// Prints date as Saturday, June 27, 2020
String formattedDateFull = currentDate.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
System.out.println("FULL Format : " + formattedDateFull);
}
}
Output:
SHORT Format : 6/27/20
MEDIUM Format : Jun 27, 2020
LONG Format : June 27, 2020
FULL Format : Saturday, June 27, 2020
Conclusion
In this tutorial, we have covered how we can use the Java LocalDate format() method to convert LocalDate instance to String using the default(yyyy-MM-dd), user-defined, library defined and custom-defined patterns.
Happy Learning!!!
More Examples you may like
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.