DateFormat format() Method in Java with Examples
The format() Method of DateFormat class in Java 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 i.e., 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 DateFormat:
Example 1:
// Java code to illustrate format() method import java.text.*; import java.util.Calendar; public class DateFormat_Demo { public static void main(String[] args) { // Initializing the first formatter DateFormat DFormat = DateFormat.getDateInstance(); // 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 = DFormat.format(cal.getTime()); System.out.println("Formatted Date: " + curr_date); } } |
The original Date: Wed Mar 27 11:12:29 UTC 2019 Formatted Date: Mar 27, 2019
Example 2:
// Java code to illustrate format() method import java.text.*; import java.util.*; public class DateFormat_Demo { public static void main(String[] args) { // Initializing the first formatter DateFormat DFormat = DateFormat.getDateTimeInstance( DateFormat.LONG, DateFormat.LONG, Locale.getDefault()); // 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 = DFormat.format(cal.getTime()); System.out.println("Formatted Date: " + curr_date); } } |
The original Date: Wed Mar 27 11:12:37 UTC 2019 Formatted Date: March 27, 2019 11:12:37 AM UTC
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#format(java.util.Date)
Recommended Posts:
- DateFormat format(Date obj) method in Java
- DateFormat equals() Method in Java with Examples
- DateFormat setCalendar() Method in Java with Examples
- DateFormat getDateTimeInstance() Method in Java with Examples
- DateFormat hashCode() method in Java with Examples
- DateFormat getTimeZone() Method in Java with Examples
- DateFormat getCalendar() Method in Java with Examples
- DateFormat getAvailableLocales() Method in Java with Examples
- DateFormat clone() method in Java with Examples
- DateFormat isLenient() Method in Java with Examples
- DateFormat getDateInstance() Method in Java with Examples
- DateFormat getTimeInstance() Method in Java with Examples
- DateFormat setTimeZone() Method in Java with Examples
- DateFormat setLenient() Method in Java with Examples
- DateFormat parse(string , ParsePosition) 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.



