Java Simple Date formatting and converting Examples

We will see date formatting and the class used to format dates using date patterns in this blog.

DateFormat

A DateFormat class is used only for formatting (converting a date to a string).

The DateFormat class provides methods for creating DateFormat objects, such as getDateInstance() and getTimeInstance().

DateFormat's getDateInstance() method requires two input parameters. The first parameter specifies the DateFormat to use, and the second parameter specifies the locale.

DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.ENGLISH);
String date = dateFormat.format(new Date());
System.out.println(date);

Using getTimeInstance()

In order to perform a time format, we need an instance of time. A time instance will be obtained by using the getTimeInstance() method.

DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.DEFAULT, Locale.ENGLISH);
String time = timeFormat.format(new Date());
System.out.println(time);

SimpleDateFormat

A DateFormat class is used only for formatting (converting a date to a string).

If you want a class to support both formatting (Date to String conversion) and parsing (String to Data conversion), then the SimpleDateFormat class can be used.

A date can be parsed by creating an instance of SimpleDateFormat and then using format().

Let us look at an example of formatting a date using SimpleDateFormat. 

String[] dateformats = new String[] {
  "dd-MM-yyyy",
  "dd-MMM-yyyy",
  "HH:mm a",
  "hh:mm:ss",
  "hh:mm:ss:SSS",
  "dd-MM-yyyy-hh:mm:ss:SSS",
  "dd-MM-yyyy-hh:mm:ss:SSS:Z",
  "dd-MM-yyyy-hh:mm:ss:SSS:z"
};

for (String pattern: dateformats) {
  SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
  Date date = new Date();
  System.out.println(dateFormat.format(date));
}

Output for above program.


Pattrn dd-MM-yyyy - 13-11-2022
Pattrn dd-MMM-yyyy - 13-Nov-2022
Pattrn HH:mm a - 12:00 pm
Pattrn hh:mm:ss - 12:00:52
Pattrn hh:mm:ss:SSS - 12:00:52:012
Pattrn dd-MM-yyyy-hh:mm:ss:SSS - 13-11-2022-12:00:52:013
Pattrn dd-MM-yyyy-hh:mm:ss:SSS:Z - 13-11-2022-12:00:52:013:+0530
Pattrn dd-MM-yyyy-hh:mm:ss:SSS:z - 13-11-2022-12:00:52:013:IST

Here is a list of symbols that can be used in date formatting.

Symbol Description Example
G Era designator AD
y Year 2018(yyyy),18(yy)
M Month in year Jul(MMM), 07(MM)
w Results in week in year 16
W Results in week in month 3
D gives the day count of the year 266
d Day of the month 9
F Day of the week in month 4
E Day name in the week Tuesday, Tue
u Day number of week 1 represents Monday, 2 represents Tuesday and so on 2
a AM or PM marker AM
H Hour in the day (0-23) 12
k Hour in the day (1-24) 23
K Hour in am/pm for the 12-hour format(0–11) 0
h Hour in am/pm for 12 hour format (1-12) 12
m Minute in the hour 59
s Second in the minute 35
S Millisecond in the minute 978
z Timezone Pacific Standard Time; PST; GMT-08:00
Z Timezone offset in hours (RFC pattern) -0800
X Timezone offset in ISO format -0800,-08:00

Creating a SimpleDateFormat for a particular locale  

This locale specifies the user's region and language in order to display localized date and time information. For example, the way the date is written in China and Japan will be different.

We can get the default locale, which is used for date formatting using the Locale.getDefault() method.

The SimpleDateFormat constructor takes Locale as the second parameter when creating the class, so when we format the date using SimpleDateFormat it will use the Locale we have provided

Locale locale = Locale.getDefault();
System.out.println("Default Locale " + locale);

SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE-MM-yyyy", Locale.CHINA);
SimpleDateFormat dateFormat1 = new SimpleDateFormat("EEEE-MM-yyyy", Locale.JAPAN);
SimpleDateFormat dateFormat2 = new SimpleDateFormat("EEEE-MM-yyyy", Locale.ENGLISH);

Date date = new Date();

System.out.println(dateFormat.format(date));
System.out.println(dateFormat1.format(date));
System.out.println(dateFormat2.format(date));
      

Output for above program.


Default Locale en_IN
星期日-11-2022
日曜日-11-2022
Sunday-11-2022


DateFormatSymbols

The DateFormatSymbols class has localized date and time formatting information like the days of the week and months.

DateFormatSymbols don't have to be instantiated directly.By calling one of its factory methods, a DateFormat instance is automatically created.

By calling the SimpleDateFormat.getDateFormatSymbols() method, you can retrieve a dateformatSymbols object.

Custom Weekend

Let's see how we can define custom names for days using DateFormatSymbols.

By default, Sunday is the starting date, and the first value in the array is always ignored, so we can just give an empty string.

The custom Weekdays variable is an array that contains our custom day names, and we pass this variable to dateFormatSymbols.setWeekdays().

SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE-MM-yyyy", Locale.ENGLISH);
DateFormatSymbols dateFormatSymbols = dateFormat.getDateFormatSymbols();

System.out.println("Default days " + Arrays.toString(dateFormatSymbols.getShortWeekdays()));

String[] customWeekdays = new String[] {
  "",
  "Stop Sunday",
  "WorstDay Monday",
  "TeaDay Tuesday",
  "Wed Wednesday",
  "Tourday Thursday",
  "Fun Friday",
  "Sand Saturday",
};

dateFormatSymbols.setWeekdays(customWeekdays);
dateFormat.setDateFormatSymbols(dateFormatSymbols);

Date date = new Date();
System.out.println(dateFormat.format(date));
Output for above program.

Default days [, Sun, Mon, Tue, Wed, Thu, Fri, Sat]
Stop Sunday-11-2022

Post a Comment

Previous Post Next Post

Recent Posts

Facebook