text

Java SimpleDateFormat and DateFormat Example

In this example we will show how to use java.text.SimpleDateFormat class so as to format date into text or parse text into date. SimpleDateFormat extends the java.text.DateFormat class which is an abstract class for date/time formatting subclasses and provides many class methods for obtaining default date/time formatters based on any given locale. We will see the usage of the class DateFormat, too.

1. Example of SimpleDateFormat

Create a java class named SimpleDateFormatExample.java with the following code:
SimpleDateFormatExample.java

package com.javacodegeeks.corejava.text;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SimpleDateFormatExample {
	public static void main(String[] args) {

		Date curDate = new Date();

		SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

		String DateToStr = format.format(curDate);
		System.out.println(DateToStr);

		format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
		DateToStr = format.format(curDate);
		System.out.println(DateToStr);

		format = new SimpleDateFormat("dd MMMM yyyy zzzz", Locale.ENGLISH);
		DateToStr = format.format(curDate);
		System.out.println(DateToStr);

		format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
		DateToStr = format.format(curDate);
		System.out.println(DateToStr);

		try {
			Date strToDate = format.parse(DateToStr);
			System.out.println(strToDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}

Let’s explain the different formats of SimpleDateFormat class in the above code. Firstly, we create a Date object which is initialized with the current date and time. Then, we create different date formatters with different patterns, such as:

  • yyyy/MM/dd, which shows the date in the form of year/month/day. As we can observe, the pattern for the year has 4 letters, which means that the full form of the year will be used (e.g. 2014). Otherwise a short or abbreviated form is used if available.
  • dd-M-yyyy hh:mm:ss, which shows the date in the form of date-month-year (the month will be shown in the abbreviated form, as it has only one letter and not two as in the previous case) and futhermore, it shows the time (hour, minutes and seconds) while the hour is in am/pm format.
  • dd MMMM yyyy zzzz, which shows the date and the timezone in full format. We can observe that we also defined the locale of the date/time: Locale.ENGLISH.
  • E, dd MMM yyyy HH:mm:ss z, which shows the date, the day name of the week and the time (we can see that the hour is in capital, which means that the hour’s values here are between 0 – 23, as we use the 24-hour clock).

Using all those formatters, we format dates as strings. Finally, we show a reverse example, where we parse a string into date.

For a detailed explanation of the different existing patterns you can visit the the java doc SimpleDateFormat.

If we run the above code, we will have the following results:

  • Output:
2014/05/11
11-5-2014 11:11:51
11 May 2014 Eastern European Summer Time
Sun, 11 May 2014 23:11:51 EEST
Sun May 11 23:11:51 EEST 2014

2. Example of DateFormat

Create a java class named DateFormatExample.java with the following code:

DateFormatExample.java

package com.javacodegeeks.corejava.text;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

public class DateFormatExample {
	public static void main(String[] args) {

		Date curDate = new Date();

		System.out.println(curDate.toString());

		String DateToStr = DateFormat.getInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance().format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.SHORT)
				.format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.MEDIUM).format(
				curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getTimeInstance(DateFormat.LONG).format(curDate);
		System.out.println(DateToStr);

		DateToStr = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
				DateFormat.SHORT).format(curDate);
		System.out.println(DateToStr);

		try {
			Date strToDate = DateFormat.getDateInstance()
					.parse("July 17, 1989");
			System.out.println(strToDate.toString());
		} catch (ParseException e) {
			e.printStackTrace();
		}

	}
}

Let’s explain the above code. Firstly, we create a Date object which is initialized with the current date and time. Then we use the default DateFormat by using the method getInstance() and the default time, date and date-time DateFormats by using the methods getTimeInstance(), getDateInstance(), getDateTimeInstance(), respectively. Then, we can control the length of the time format by using styles such as LONG and MEDIUM in the default Time DateFormat. Also, we define the date and time format in the default date-time DateFormat. Finally, we show an example of parsing a string in date, using the default date DateFormat.

If we run the above code, we will have the following results:

  • Output:
Sun May 11 23:37:54 EEST 2014
5/11/14 11:37 PM
11:37:54 PM
May 11, 2014
May 11, 2014 11:37:54 PM
11:37 PM
11:37:54 PM
11:37:54 PM EEST
May 11, 2014 11:37 PM
Mon Jul 17 00:00:00 EEST 1989

3. Download the source code

This was an example of how to use the classes DateFormat and SimpleDateFormat.
You can download the source code of this example from here: DateFormatExample.zip

Konstantina Dimtsa

Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Vikas
Vikas
5 years ago

Hi I was getting an exception in the last try and catch black. PLease update the code and provide a link
Thanks

Back to top button