Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A reference for datetime format
Mostly about the java.time package, about time and date format issue.
Nothing Special, just for reference.
y = year (yy or yyyy)
M = month (MM)
d = day in month (dd)
h = hour (0-12) (hh)
H = hour (0-23) (HH)
m = minute in hour (mm)
s = seconds (ss)
S = milliseconds (SSS)
z = time zone text (e.g. Pacific Standard Time...)
Z = time zone, time offset (e.g. -0800)
yyyy-MM-dd (2009-12-31)
dd-MM-YYYY (31-12-2009)
E hh:mm a (Sun 1:18 PM)
E MMM yyyy (Sun Jul 2015)
E MMMM yyyy (Sun July 2015)
E MMMM yy (Sun July 15)
EEEE MMMM yyyy (Sunday July 2015)
yyyy-MM-dd HH:mm:ss (2009-12-31 23:59:59)
HH:mm:ss.SSS (23:59.59.999)
yyyy-MM-dd HH:mm:ss.SSS (2009-12-31 23:59:59.999)
yyyy-MM-dd HH:mm:ss.SSS Z (2009-12-31 23:59:59.999 +0100)
Example: (Java 8) Use the java.time package:
####Code Here
String str = "1996-08-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
Example: (java 8) import java.time.*;
####Problem Set
Get the current date, and format it.
####Code here
LocalDate date =;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd MMM yyyy");
String one = date.format(formatter);
System.out.println("Format Date: "+one);
Example:(Use the java.text.SimpleDateFormat package)
####Problem Set
How to format "2015-07-01 09:00:02" date to 9:00
####Code Here
String str = "2015-07-01 09:00:02";
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(originalString);
String newString = new SimpleDateFormat(H:mm).format(date);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment