Java 8 has new Date-Time API to work with dates and times. We should use the new Java 8 Date-Time API to format and parse date time value.
If we are writing new code related to dates and times, we should be using the new Date-Time API.
Format date and time using new Java 8 Date-Time API.This section is for you to work with legacy code that uses old Date and Calendar classes.
The Java library provides two classes to format dates:
DateFormat
class is an abstract class
and we can use the DateFormat
class to format dates with a predefined format.
Since it is abstract, so we cannot create an instance of DateFormat
class
using the new
operator.
We have to use one of its getXxxInstance()
methods to create new instance.
Xxx can be Date, DateTime, or Time.
To format a date time value we use the format()
method of
the DateFormat
class.
The formatted text from DateFormat
class
depends on two things:
The style of formatting determines how much datetime information is included in the formatted text
The locale determines which locale to use.
The DateFormat
class defines five styles as constants:
The DEFAULT
format is the same as MEDIUM
.
getInstance() uses SHORT
.
The following table shows the same date formatted in different styles for a US locale.
Style | Formatted Date |
---|---|
DEFAULT | Mar 27, 2014 |
SHORT | 3/27/14 |
MEDIUM | Mar 26, 2014 |
LONG | March 26, 2014 |
FULL | Sunday, November 2, 2014 |
The following code shows how to display dates in short and medium formats for locales as default, France, and Germany.
import java.text.DateFormat; import java.util.Date; import java.util.Locale; // w w w . ja v a2s .c o m public class Main { public static void main(String[] args) { Date today = new Date(); // Print date in the default locale format Locale defaultLocale = Locale.getDefault(); printLocaleDetails(defaultLocale); printDate(defaultLocale, today); // Print date in French format printLocaleDetails(Locale.FRANCE); printDate(Locale.FRANCE, today); // Print date in German format. We could also use Locale.GERMANY // instead of new Locale ("de", "DE"). Locale germanLocale = new Locale("de", "DE"); printLocaleDetails(germanLocale); printDate(germanLocale, today); } public static void printLocaleDetails(Locale locale) { String languageCode = locale.getLanguage(); String languageName = locale.getDisplayLanguage(); String countryCode = locale.getCountry(); String countryName = locale.getDisplayCountry(); // Print the locale info System.out.println("Language: " + languageName + "(" + languageCode + "); " + "Country: " + countryName + "(" + countryCode + ")"); } public static void printDate(Locale locale, Date date) { DateFormat formatter = DateFormat.getDateInstance(DateFormat.SHORT, locale); String formattedDate = formatter.format(date); System.out.println("SHORT: " + formattedDate); formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); formattedDate = formatter.format(date); System.out.println("MEDIUM: " + formattedDate+"\n"); } }
The code above generates the following result.
The java.util.Locale
class contains constants for common locales.
We can use the Locale.getDefault()
method to get the default Locale for your system.
To create custom date formats, we can use the SimpleDateFormat
class.
SimpleDateFormat class is locale-sensitive.
Its default constructor creates a formatter with default date format for default locale.
format()
method from the SimpleDateFormat
class does the date format.
To change the date format for subsequent formatting, you can use the applyPattern() method by passing the new date format as an argument.
import java.text.SimpleDateFormat; import java.util.Date; /* w w w . j av a 2 s . c o m*/ public class Main { public static void main(String[] args) { SimpleDateFormat simpleFormatter = new SimpleDateFormat("dd/MM/yyyy"); Date today = new Date(); String formattedDate = simpleFormatter.format(today); System.out.println("Today is (dd/MM/yyyy): " + formattedDate); simpleFormatter.applyPattern("MMMM dd, yyyy"); formattedDate = simpleFormatter.format(today); System.out.println("Today is (MMMM dd, yyyy): " + formattedDate); } }
The code above generates the following result.