SimpleDateFormat format() Method in Java with Examples
The format() Method of SimpleDateFormat class is used to format a given date into Date/Time string. Basically the method is used to convert this date and time into a particular format for say mm/dd/yyyy.
Syntax:
public final String format(Date date)
Parameters: The method takes one parameter date of Date object type and refers to the date whose string output is to be produced.
Return Value: The method returns Date or time in string format of mm/dd/yyyy.
Below programs illustrate the working of format() Method of SimpleDateFormat:
Example 1:
// Java code to illustrate format() method import java.text.*; import java.util.Calendar; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException { SimpleDateFormat SDFormat = new SimpleDateFormat("MM/dd/yyyy"); // Initializing the calender Object Calendar cal = Calendar.getInstance(); // Displaying the actual date System.out.println("The original Date: " + cal.getTime()); // Using format() method for conversion String curr_date = SDFormat.format(cal.getTime()); System.out.println("Formatted Date: " + curr_date); } } |
The original Date: Tue Jan 29 12:23:40 UTC 2019 Formatted Date: 01/29/2019
Example 2:
// Java code to illustrate format() method import java.text.*; import java.util.Calendar; public class SimpleDateFormat_Demo { public static void main(String[] args) throws InterruptedException { SimpleDateFormat SDFormat = new SimpleDateFormat(); // Initializing the calender Object Calendar cal = Calendar.getInstance(); // Displaying the actual date System.out.println("The original Date: " + cal.getTime()); // Using format() method for conversion String curr_date = SDFormat.format(cal.getTime()); System.out.println("Formatted Date: " + curr_date); } } |
The original Date: Tue Jan 29 12:29:32 UTC 2019 Formatted Date: 1/29/19 12:29 PM
Recommended Posts:
- SimpleDateFormat applyPattern() Method in Java with Examples
- SimpleDateFormat set2DigitYearStart() Method in Java with Examples
- SimpleDateFormat getDateFormatSymbols() Method in Java with Examples
- SimpleDateFormat toPattern() Method in Java with Examples
- SimpleDateFormat setDateFormatSymbols() Method in Java with Examples
- SimpleDateFormat hashCode() Method in Java with Examples
- SimpleDateFormat toLocalizedPattern() Method in Java with Examples
- SimpleDateFormat equals() Method in Java with Examples
- SimpleDateFormat get2DigitYearStart() Method in Java with Examples
- SimpleDateFormat parse() Method in Java with Examples
- SimpleDateFormat applyLocalizedPattern() Method in Java with Examples
- SimpleDateFormat clone() Method in Java with Examples
- Year format() method in Java with Examples
- ChronoLocalDateTime format() method in Java with Examples
- DateFormat format() Method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

