The following list shows the formatting symbols we can use to create custom data format patterns.
G AD GGGG Anno Domini GGGGG A
u/uuu/uuuu 2014 uu 12 uuuuu 02014
y/yyy/yyyy 2014 yy 12 yyyyy 02014
D 150
M 5 MM 05 MMM Jul MMMM July
d 21 dd 01 ddd 001 ddd 029
Q 3 QQ 03 QQQ Q3 QQQQ 3rd quarter
Y 2014 YY 12 YYY/YYYY 2014
w 31
W 2
E 7 EE 07 EEE Sun EEEEE Sunday
F 1
a AM
h 2
K 3
k 7
H 7 HH 07
mm 21
ss 12
SSSSSSSSS 000006789
A 12014012
n 789
N 12014012001289
VV America/Chicago
z CDT
Z -0500 ZZ -0500 ZZZ -05:00 ZZZZ GMT-05:00
O GMT-5
X +09 XX +0930 XXX +09:30 XXX -05:00 XXXX +093045 XXXXX +08:30:45
xx -0500
mm 30 pppmm " 30"
'Hello' MMMM Hello July
'''Hello''' MMMM 'Hello' July
import java.time.LocalDate; import java.time.Month; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; import java.util.Locale; /*from www. java 2 s .com*/ public class Main { public static void main(String[] args) { LocalDate ld = LocalDate.of(2014, Month.JUNE, 30); format(ld, "M/d/yyyy"); format(ld, "MM/dd/yyyy"); format(ld, "MMM dd, yyyy"); format(ld, "MMMM dd, yyyy"); format(ld, "EEEE, MMMM dd, yyyy"); format(ld, "'Month' q 'in' QQQ"); } public static void format(Temporal co, String pattern) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); String str = fmt.format(co); System.out.println(pattern + ": " + str); } }
The code above generates the following result.
We can use DateTimeFormatter
class ofPattern()
method to create
a DateTimeFormatter
object with the specified format pattern and locale.
static DateTimeFormatter ofPattern(String pattern) static DateTimeFormatter ofPattern(String pattern, Locale locale)
The following code shows how to create two formatters to format a date in "Month day, Year" format in the default locale and in the German locale.
DateTimeFormatter fmt1 = DateTimeFormatter.ofPattern("MMMM dd, yyyy"); DateTimeFormatter fmt2 = DateTimeFormatter.ofPattern("MMMM dd, yyyy", Locale.GERMAN);
DateTimeFormatter
class withLocale()
method
returns a DateTimeFormatter object for the specified locale from the same pattern.
DateTimeFormatter fmt2 = fmt1.withLocale(Locale.GERMAN);
getLocale()
method from DateTimeFormatter
class
returns the locale for current formatter.
import java.time.LocalDate; import java.time.LocalTime; import java.time.Month; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.Temporal; import java.util.Locale; /*from www .j a va2s.co m*/ public class Main { public static void main(String[] args) { LocalTime lt = LocalTime.of(16, 30, 5, 78899); format(lt, "HH:mm:ss"); format(lt, "KK:mm:ss a"); format(lt, "[MM-dd-yyyy][' at' HH:mm:ss]"); ZoneId usCentral = ZoneId.of("America/Chicago"); ZonedDateTime zdt = ZonedDateTime.of(LocalDate.now(), lt, usCentral); format(zdt, "MM/dd/yyyy HH:mm:ssXXX"); format(zdt, "MM/dd/yyyy VV"); format(zdt, "[MM-dd-yyyy][' at' HH:mm:ss]"); } public static void format(Temporal co, String pattern) { DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern, Locale.US); String str = fmt.format(co); System.out.println(pattern + ": " + str); } }
The code above generates the following result.
When defining custom date time format
we can use symbols [
and ]
to mark an optional section.
A pattern enclosed within an optional section is output only if information is available for all its elements.
The following code shows how to use an optional format.
import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.Month; import java.time.format.DateTimeFormatter; /* w w w. j a v a2 s. c om*/ public class Main { public static void main(String[] args) { String pattern = "MM/dd/yyyy[ 'at' HH:mm:ss]"; DateTimeFormatter fmt = DateTimeFormatter.ofPattern(pattern); LocalDate ld = LocalDate.of(2014, Month.JUNE, 30); LocalTime lt = LocalTime.of(17, 30, 12); LocalDateTime ldt = LocalDateTime.of(ld, lt); String str1 = fmt.format(ld); System.out.println(str1); String str2 = fmt.format(ldt); System.out.println(str2); } }
The code above generates the following result.
We can create custom date time formatter from DateTimeFormatterBuilder
.
The following code builds a DateTimeFormatter
object to format a date in the format like "New Year in YEAR is on WEEK_DAY":
import java.time.LocalDate; import java.time.Month; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; import java.time.format.TextStyle; import java.time.temporal.ChronoField; /* w w w . java 2s . c om*/ public class Main { public static void main(String[] args) { DateTimeFormatter formatter = new DateTimeFormatterBuilder() .appendLiteral("New Year in ") .appendValue(ChronoField.YEAR) .appendLiteral(" is on ") .appendText(ChronoField.DAY_OF_WEEK,TextStyle.FULL_STANDALONE) .toFormatter(); LocalDate ld = LocalDate.of(2014, Month.JANUARY, 1); String str = ld.format(formatter); System.out.println(str); } }
The code above generates the following result.
We can create the same custom formatter using a pattern from DateTimeFormatterBuilder.
import java.time.LocalDate; import java.time.Month; import java.time.format.DateTimeFormatter; // w w w . j av a 2s .c om public class Main { public static void main(String[] args) { LocalDate ld = LocalDate.of(2014,Month.JANUARY,1); String pattern = "'New Year in' yyyy 'is on' EEEE"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern); String str = ld.format(formatter); System.out.println(str); } }
The code above generates the following result.