DateFormat format(Date obj) method in Java
The format() method of DateFormat class in Java is used to format a given Date object into a string representing Date/Time.
Syntax:
String format(Date date)
Parameters: This method accepts a single parameter date which is a Date object.
Return Value: This method returns a String which is the formatted date-time value of the Date object passed as a parameter to the method.
Below programs illustrate the above method:
Program 1:
// Java program to illustrate the // format() method import java.text.DateFormat; import java.util.Date; public class GFG { public static void main(String[] args) { // Create a Date instance Date currentDate = new Date(); // Create a DateFormat instance and format // current Date String dateToStr = DateFormat.getInstance() .format(currentDate); System.out.println("Formatted Date String: " + dateToStr); } } |
Formatted Date String: 2/27/19 3:31 PM
Program 2:
// Java program to illustrate the // format() method import java.text.DateFormat; import java.util.*; public class GFG { public static void main(String[] args) { // Create a Date instance Date date = new Date(2323223232L); // Create a DateFormat instance and format // the specified Date String dateToStr = DateFormat.getInstance() .format(date); System.out.println("Formatted Date String: " + dateToStr); } } |
Formatted Date String: 1/27/70 9:20 PM
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#format(java.util.Date)\
Recommended Posts:
- DateFormat getTimeZone() Method in Java with Examples
- DateFormat isLenient() Method in Java with Examples
- DateFormat setLenient() Method in Java with Examples
- DateFormat getCalendar() Method in Java with Examples
- DateFormat format() Method in Java with Examples
- DateFormat getTimeInstance() Method in Java with Examples
- DateFormat equals() Method in Java with Examples
- DateFormat hashCode() method in Java with Examples
- DateFormat clone() method in Java with Examples
- DateFormat getDateTimeInstance() Method in Java with Examples
- DateFormat setCalendar() Method in Java with Examples
- DateFormat setTimeZone() Method in Java with Examples
- DateFormat getAvailableLocales() Method in Java with Examples
- DateFormat getDateInstance() 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.



