Java Date format

Here we will learn how to convertĀ a date string into date object. Java Date format can be used here.

Simple Date Format Option

Example : 24 July2012 4:00:23 PM

Note*:: Take special care of small and caps letter.

LetterComponentExamples Value
GEra designator (AD/BC)AD
yYear2012 or 12
YWeek year2012; 12
MMonth in yearJuly; Jul; 07
wWeek in year27
WWeek in month4
DDay in year189
dDay in month24
FDay of week in month2
EDay name in weekTuesday; Tue
uDay number of week (1 = Monday, …, 7 = Sunday)2
aAm/pm marker(AM/PM)PM
HHour in day (0-23)16
kHour in day (1-24)17
KHour in am/pm (0-11)4
hHour in am/pm (1-12)12
mMinute in hour30
sSecond in minute55
SMillisecond978
zTime zonePacific Standard Time; PST; GMT-08:00
ZTime zone-0800
XTime zone-08; -0800; -08:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * Here we will learn how to convert String to Date.
 */

public class StringToDate {

	public static void main(String args[]) throws ParseException {

		String d1 = "24-DEC-2010 00:19:39";
		String d2 = "DEC 02 00:19:39 2012";
		String d3 = "01/01/2012";

		SimpleDateFormat s1 = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
		SimpleDateFormat s2 = new SimpleDateFormat("MMM dd HH:mm:ss yyyy");
		SimpleDateFormat s3 = new SimpleDateFormat("dd/MM/yyyy");

		Date date1 = s1.parse(d1);
		Date date2 = s2.parse(d2);
		Date date3 = s3.parse(d3);

		System.out.println(s1.format(date1));
		System.out.println(s2.format(date2));
		System.out.println(s3.format(date3));
	}

}

Out put will be

24-Dec-2010 00:19:39
Dec 02 00:19:39 2012
01/01/2012
By | 2012-07-16T19:33:44+00:00 July 16th, 2012|Code Base|1 Comment

About the Author:

One Comment

  1. Anju Sinha April 9, 2014 at 12:06 pm - Reply

    I am a beginner for string and i know only core java,I find difficulty in understanding this..plz help me

Leave A Comment