May 10, 2021 Java
Java 8 has a new Date-Time API to handle dates and times. /b10> We should use the new Java 8 Date-Time API to format and parse date time values.
If we are writing new code related to date and time, we should use the new Date-Time API.
Format the date and time using the new Java 8 Date Time API.
This section applies to old code that uses old dates and calendar classes.
The Java library provides two classes to format the date:
DateFormat
class is an abstract class and we can format the date in a predefined format using the
DateFormat
class.
Because it is abstract, we cannot create an
DateFormat
class
new
operator.
We have to use one of
getXxxInstance()
to create new instances.
Xxx can be a date, date time, or time.
To format the date time value, we use the
format()
method
DateFormat
class.
The formatted text of the DateFormat class depends on two things:
The style of the format determines how much date-time information is included in the formatted text
The language environment determines the locale to use.
Date Format
class defines five styles as constants:
DEFAULT
format is the
MEDIUM
GetInstance() uses
SHORT
The following table shows the same dates formatted in different styles for the U.S. locale.
Style | Format the 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 Chinese Simplified default dates for the locale in a different format, France and Germany.
import java.text.DateFormat; import java.util.Date; import java.util.Locale; 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 produces the following results.
java.util.Locale
class contains constants for common language environments.
We can use
Locale.getDefault()
to get the system's default locale.
To create a custom date format, we can use
SimpleDateFormat
class.
The SimpleDateFormat class is sensitive to the language environment.
Its default constructor creates a formatting program, and the default date format is the default locale.
The
format()
method in the
SimpleDateFormat
class executes the date format.
To change the date format for subsequent formatting, you can use the applePattern() method by passing the new date format as an argument.
import java.text.SimpleDateFormat; import java.util.Date; 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 produces the following results.