Format date/time
java.text.SimpleDateFormat
The subwritten code formats the current date:
SimpleDateFormat sd = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); String dateString = sd.format(new Date()); System.out.println(dateString); // And back: Date date = sd.parse(dateString);
Possible parameters/codes:
Code | Meaning | Example |
G | era as text | AD |
y | year | 2011 |
M | month in a year | 7 |
MM | month in a year with 0 for January | 6 |
MMM | month in a year short | Jun |
MMMM | month in a year long | June |
d | day in a month | 26 |
h | hour (1–12) | 9 |
H | hour per day (0–23) | 18 |
m | minute of an hour | 12 |
s | second of a minute | 42 |
S | millisecond | 264 |
E | day of a week | Fr |
EEEE | day of a week long | Friday |
D | day in a year | 321 |
F | day of a week in a month | 2 |
w | week in a year | 24 |
W | week in a month | 2 |
a | AM- or PM-text | AM |
k | hour per day (1–24) | 24 |
K | hour (0–11) | 11 |
z | timezone | GMT+02:00 |
' | enclosure for a text | Hello |
'' | single highcomma | ' |