java.text.SimpleDateFormat

On this section we would be covering up SimpleDateformat class which is a concrete class that helps in formatting and parsing of dates. Let’s say for instance one of your business requirements is to transform a date format yyyy-mm-dd to mm/dd/yyyy, the SimpleDateFormat class along with its attributes will help you to accomplish this.

One of the fundamental use of SimpleDateFormat class is its ability to interpret a string date format into a Date object which means we would be able to manipulate this data to our liking. Like for instance we want to subtract one day on the input String date, no other way than to convert the string date into a Date object which is easy done using the parse method of SimpleDateFormat class.

From personal experience, I have used this Class a lot of time during my career as a developer. So I suggest for you to go through the examples that we have presented on this document and its subsections. Once you mastered the use of this method, your handling of date format would surely improved.

SimpleDateFormat Class Syntax

public class SimpleDateFormat
extends DateFormat

Integer Compatibility Version

Requires JDK 1.0

Integer Class Constructor

 

Syntax Description
SimpleDateFormat(String pattern) Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the default FORMAT locale
SimpleDateFormat(String pattern,Locale locale) Constructs a SimpleDateFormat using the given pattern and the default date format symbols for the given locale.
SimpleDateFormat(String pattern,DateFormatSymbols formatSymbols) Constructs a SimpleDateFormat using the given pattern and date format symbols.

 

Basic SimpleDateFormat Handling

There are three constructors available but most of the time you would only be dealing with below sample declaration:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MD-dd");

Wherein the method argument “yyyy-MM-dd” signifies the patter to be use by the formatter. The list of pattern accepted and its description can be found in the Java 8 Official Document for SimpleDateFormat class.

So what if we want to format the current date and time to the specified pattern declared on the constructor. We would be simply call the format class. lets take below for a sample program on how to do this.

package com.javatutorialhq.java.examples;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/*
 * This is a basic example of SimpleDateFormat
 */

public class SimpleDateFormatExample {

	public static void main(String[] args) {

		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
		Calendar cal = Calendar.getInstance();
		System.out.println("Current date:"+cal.getTime());
		String formattedDate = sdf.format(cal.getTime());
		System.out.println("Formatted date:" + formattedDate);

	}

}

If we run the above example the following will be the sample output:

Current date:Mon Mar 02 22:07:34 CST 2015
Formatted date:03/02/15

From the example above, we have declare a SimpleDateFormat object and initialize it with constructor value of MM/dd/yy. Then we have initialize the Calendar object. We use the calendar object to get the current time and we passed it to the format method of SimpleDateFormat. The return String of the format method is a formatted date which represents the format specified on the SimpleDateFormat constructor.

Method List Available for SimpleDateFormat Class

Method Return Method Description
void applyLocalizedPattern(String pattern) Applies the given localized pattern string to this date format.
void applyPattern(String pattern) Applies the given pattern string to this date format.
boolean equals(Object obj) Compares the given object with this SimpleDateFormat for equality.
Object clone() Creates a copy of this SimpleDateFormat.
Attributed Character Iterator formatToCharacterIterator(Object obj) Formats an Object producing an AttributedCharacterIterator.
StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition pos) Formats the given Date into a date/time string and appends the result to the given StringBuffer.
Date Format Symbols getDateFormatSymbols() Gets a copy of the date and time format symbols of this date format.
Date parse (String text, ParsePosition pos) Parses text from a string to produce a Date.
String toLocalizedPattern() Returns a localized pattern string describing this date format.
String toPattern() Returns a pattern string describing this date format.
Date get2DigitYearStart() Returns the beginning date of the 100-year period 2-digit years are interpreted as being within.
int hashCode() Returns the hash code value for this SimpleDateFormat object.
void set2DigitYearStart(Date startDate) Sets the 100-year period 2-digit years will be interpreted as being in to begin on the date the user specifies.
void setDateFormatSymbols (DateFormatSymbols newFormatSymbols) Sets the date and time format symbols of this date format.