How to accept different formats of Date in Java?
Different date formats are used in various parts of the world; hence computer languages often offer a variety of date formats for programmers to work with.
The way of writing date is different in different regions of the world. For example, 31st Dec 2017 will be written in India as 31-12-2017 but the same thing will be written in the United States of America as 12-31-2017. The is the reason behind converting dates from one format to another.
In Java, to convert the date from one format to another, we can use the date and time API by importing the java.time package. There are numerous date and time classes in the package that helps in formatting the date.
Class | Description |
LocalDate | Represents a date (year, month, day (yyyy-MM-dd)) |
LocalTime | Represents a time (hour, minute, second, and nanoseconds (HH-mm-ss-ns)) |
LocalDateTime | Represents both a date and a time (yyyy-MM-dd-HH-mm-ss-ns) |
DateTimeFormatter | Formatter for displaying and parsing date-time objects |
Display Current Date
To display the current date, import the java.time.LocalDate class, and use its now() method:
Program
Filename: DateF.java
import java.time.LocalDate ; // import the LocalDate class
public class DateF {
public static void main(String[] args) {
LocalDate myObj = LocalDate.now() ; // Create a date object
System.out.println(myObj) ; // Display the current date
}
}
Output

Display Current Date and Time
To display the current date and time, import the java.time.LocalDateTime class, and use its now() method.
Filename: DateF.java
import java.time.LocalDateTime; // import the LocalDateTime class
public class DateF{
public static void main(String[] args) {
LocalDateTime myObj = LocalDateTime.now() ;
System.out.println(myObj) ;
}
}
Output

Formatting Date and Time
The "T" in the example above is used to separate the date from the time. You can use the DateTimeFormatter class with the ofPattern() method in the same package to format or parse date-time objects. The following example will remove both the "T" and nanoseconds from the date-time.
Filename: DateF.java
import java.time.LocalDateTime ; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter ; // Import the DateTimeFormatter class
public class DateF {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now() ;
System.out.println("Before formatting: " + myDateObj) ;
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss") ;
String formattedDate = myDateObj.format(myFormatObj) ;
System.out.println("After formatting: " + formattedDate) ;
}
}
Output

The ofPattern() method accepts all sorts of values if you want to display the date and time in a different format.
Format | Example |
yyyy/MM/dd | "1988/09/29" |
dd/MM/yyyy | "29/09/1988" |
dd-MMM-yyyy | "29-Sep-1988" |
E, MMM dd yyyy | "Thu, Sep 29 1988" |
Program
In the format: yyyy/MM/dd
Filename: DateF.java
import java.time.LocalDateTime ; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter ; // Import the DateTimeFormatter class
public class DateF {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now() ;
System.out.println("Before Formatting: " + myDateObj) ;
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss") ;
String formattedDate = myDateObj.format(myFormatObj) ;
System.out.println("After Formatting: " + formattedDate) ;
}
}
Output

Program
In the format: dd/MM/yyyy
Filename: DateF.java
import java.time.LocalDateTime ; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter ; // Import the DateTimeFormatter class
public class DateF{
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now() ;
System.out.println("Before Formatting: " + myDateObj) ;
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss") ;
String formattedDate = myDateObj.format(myFormatObj) ;
System.out.println("After Formatting: " + formattedDate) ;
}
}
Output

Program
In the format: dd-MMM-yyyy
Filename: DateF.java
import java.time.LocalDateTime ; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter ; // Import the DateTimeFormatter class
public class DateF {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now() ;
System.out.println("Before Formatting: " + myDateObj) ;
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("dd-MMM-yyyy HH:mm:ss") ;
String formattedDate = myDateObj.format(myFormatObj) ;
System.out.println("After Formatting: " + formattedDate) ;
}
}
Output

Program
In the format: E, MMM dd yyyy
Filename: DateF.java
import java.time.LocalDateTime ; // Import the LocalDateTime class
import java.time.format.DateTimeFormatter ; // Import the DateTimeFormatter class
public class DateF {
public static void main(String[] args) {
LocalDateTime myDateObj = LocalDateTime.now() ;
System.out.println("Before Formatting: " + myDateObj) ;
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("E, MMM dd yyyy HH:mm:ss") ;
String formattedDate = myDateObj.format(myFormatObj) ;
System.out.println("After Formatting: " + formattedDate) ;
}
}
Output

Program
Filename: SimpleDateFormatExample.java
import java.text.ParseException ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.Locale ;
public class SimpleDateFormatExample{
public static void main(String[] args) {
Date date = new Date() ;
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy") ;
String strDate = formatter.format(date) ;
System.out.println("Date Format with MM/dd/yyyy : "+strDate) ;
formatter = new SimpleDateFormat("dd-M-yyyy hh:mm:ss") ;
strDate = formatter.format(date) ;
System.out.println("Date Format with dd-M-yyyy hh:mm:ss : "+strDate) ;
formatter = new SimpleDateFormat("dd MMMM yyyy") ;
strDate = formatter.format(date) ;
System.out.println("Date Format with dd MMMM yyyy : "+strDate) ;
formatter = new SimpleDateFormat("dd MMMM yyyy zzzz") ;
strDate = formatter.format(date) ;
System.out.println("Date Format with dd MMMM yyyy zzzz : "+strDate) ;
formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z") ;
strDate = formatter.format(date) ;
System.out.println("Date Format with E, dd MMM yyyy HH:mm:ss z : "+strDate) ;
}
}
Output
