java text SimpleDateFormat class

java.text.SimpleDateFormat

The SimpleDateFormat is a concrete class fused or formatting and parsing dates in a locale-sensitive manner. It is a sub class of DateFormat class. It allows us to start by choosing any user-defined patterns for date-time formatting.

SimpleDateFormat format codes

CharacterDescriptionExample
GEra designatorAD
yYear in four digits2001
MMonth in yearJuly or 07
dDay in month10
hHour in A.M./P.M. (1~12)12
HHour in day (0~23)22
mMinute in hour30
sSecond in minute55
SMillisecond234
EDay in weekTuesday
DDay in year360
FDay of week in month2 (second Wed. in July)
wWeek in year40
WWeek in month1
aA.M./P.M. markerPM
kHour in day (1~24)24
KHour in A.M./P.M. (0~11)10
zTime zoneEastern Standard Time
Escape for textDelimiter
Single quote`

java.text.SimpleDateFormat class example

package com.w3spoint;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class SimpleDateFormatTest {
	public static void main(String args[]){
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
		String date = sdf.format(new Date()); 
		System.out.println(date);
	}
}

Output:

09-04-2018
Content Protection by DMCA.com
Please Share