Hey there!
Welcome to ClearUrDoubt.com.
In this post, we will look at formatting dates in Java 8.
Using DateTimeFormatter, which was introduced in Java 8, we can format the date strings in a simple way.
Let’s look at a sample program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.clearurdoubt; import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateFormattingDemo { public static void main(String[] args) { LocalDate currentDate = LocalDate.now(); System.out.println("Current Date:: " + currentDate); System.out.println(); System.out.println("Current Date in \"YYYYMMDD\" format:: " + DateTimeFormatter.ofPattern("yyyyMMdd").format(currentDate)); System.out.println(); System.out.println("Current Date in \"YYYYMM\" format:: " + DateTimeFormatter.ofPattern("yyyyMM").format(currentDate)); System.out.println(); System.out.println("Current Date in \"YYYY_MM_DD\" format:: " + DateTimeFormatter.ofPattern("yyyy_MM_dd").format(currentDate)); } } |
Output:
Happy Learning :).
Please leave a reply in case of any queries.