Java DateFormat class is an abstract class. This class provides various methods to format the date and time. In this guide, we will see the several examples of DateFormat class and later I will list down the methods and fields of this class. There is another class SimpleDateFormat that is used for the same purpose of formatting date and time.
1. DateFormat Example: Formatting the Date and Time using SHORT, MEDIUM and LONG Fields
import java.text.DateFormat; import java.util.Date; class Example{ public static void main(String args[]){ Date currentDate = new Date(); System.out.println("Current date is: "+currentDate); String dateShort = DateFormat.getDateInstance(DateFormat.SHORT).format(currentDate); System.out.println("Formatting the Date using DateFormat.SHORT: "+dateShort); String dateMedium = DateFormat.getDateInstance(DateFormat.MEDIUM).format(currentDate); System.out.println("Formatting the Date using DateFormat.MEDIUM: "+dateMedium); String dateLong = DateFormat.getDateInstance(DateFormat.LONG).format(currentDate); System.out.println("Formatting the Date using DateFormat.LONG: "+dateLong); String timeShort = DateFormat.getTimeInstance(DateFormat.SHORT).format(currentDate); System.out.println("Formatting the Time using DateFormat.SHORT: "+timeShort); String timeMedium = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(currentDate); System.out.println("Formatting the Time using DateFormat.MEDIUM: "+timeMedium); String timeLong = DateFormat.getTimeInstance(DateFormat.LONG).format(currentDate); System.out.println("Formatting the Time using DateFormat.LONG: "+timeLong); } }
Output:
Current date is: Thu Oct 19 20:54:44 IST 2017 Formatting the Date using DateFormat.SHORT: 19/10/17 Formatting the Date using DateFormat.MEDIUM: 19 Oct, 2017 Formatting the Date using DateFormat.LONG: 19 October, 2017 Formatting the Time using DateFormat.SHORT: 8:54 PM Formatting the Time using DateFormat.MEDIUM: 8:54:44 PM Formatting the Time using DateFormat.LONG: 8:54:44 PM IST
2. DateFormat Example: Converting the Date to String
In this example, we are using the format() method of DateFormat class to convert the date to a String. You can refer this guide: Java – Date to String for more examples on Date to String conversion.
import java.text.DateFormat; import java.util.Date; public class Example { public static void main(String[] args) { Date date = new Date(); System.out.println("Date is: "+date); String dateString = DateFormat.getDateInstance().format(date); System.out.println("Converting Date to String: "+dateString); } }
Output:
Date is: Thu Oct 19 21:04:16 IST 2017 Converting Date to String: 19 Oct, 2017
3. DateFormat Example: String to Date conversion using parse() method
In this example, we have given a date in String format and we are converting that String into a Date instance using the parse() method of DateFormat class. You can also refer this guide: Java – String to Date conversion for more examples.
import java.text.DateFormat; import java.util.Date; public class Example { public static void main(String[] args)throws Exception { //Date in String format String dateString = "11 Oct, 2017"; //Converting the String to date using DateFormat Date date = DateFormat.getDateInstance().parse(dateString); //Displaying the Date System.out.println("Date: "+date); } }
Output:
Date: Wed Oct 11 00:00:00 IST 2017
Methods of DateFormat class
We have seen the examples of few methods of DateFormat class. Lets see the complete list of methods available in this class.
1. final String format(Date date)
: We have already seen the example of this method above. This method converts the Date instance to a String.
2. Date parse(String source) throws ParseException
: This method converts the String to a Date instance as we have seen in the above example.
3. static final DateFormat getDateTimeInstance(int dateStyle,int timeStyle)
: Returns the date and time after formatting them based on the specified date and time styles. If you are wondering what are the styles, the SHORT, MEDIUM and LONG fields we have seen in the above examples are the styles that we can use on both date and time.
4. static final DateFormat getDateTimeInstance(int dateStyle, int timeStyle, Locale locale)
: Same as above, here we can also specify the Locale along with date and time styles.
5. static final DateFormat getInstance()
: This method returns the formatted date and time using the SHORT style for both date and time.
6. static final DateFormat getDateInstance()
: Returns the formatted date for the default Locale.
7. static final DateFormat getDateInstance(int style)
: Same as above but here we can specify the style for the date.
8. static final DateFormat getDateInstance(int style, Locale locale)
: Returns the formatted date for the specified style and Locale.
9. static final DateFormat getDateTimeInstance()
: Returns date and time after formatting them using default style and default Locale.
10. static final DateFormat getTimeInstance()
: Returns the formatted time for the default Locale.
11. static final DateFormat getTimeInstance(int style)
: Same as above but here we can specify the style for the time.
12. static final DateFormat getTimeInstance(int style, Locale locale)
: Returns the formatted time for the specified style and Locale.
13. static Locale[] getAvailableLocales()
: This method returns the array of available locales.
14. Calendar getCalendar()
: Returns the Calendar instance for the current instance of DateFormat.
15. NumberFormat getNumberFormat()
: Returns the NumberFormat instance for the current instance of DateFormat.
16. TimeZone getTimeZone()
: Returns the timeZone for the current DateFormat instance.
Leave a Reply