![]() | |
|
Formatting and Parsing
In Java 8 formatting and parsing can be accomplished by using the format()
and
parse()
methods.
We format a given date using the desired pattern (can be predefined or customized):
// Current date and time LocalDateTime dateTime = LocalDateTime.now(); // Format as basic ISO date format String asBasicIsoDate = dateTime.format(DateTimeFormatter.BASIC_ISO_DATE); System.out.println("BASIC ISO DATE : " + asBasicIsoDate); // Format as ISO week date String asIsoWeekDate = dateTime.format(DateTimeFormatter.ISO_WEEK_DATE); System.out.println("ISO WEEK DATE : " + asIsoWeekDate); // Format ISO date time String asIsoDateTime = dateTime.format(DateTimeFormatter.ISO_DATE_TIME); System.out.println("ISO DATE TIME : " + asIsoDateTime); // Use a custom pattern: day / month / year String asCustomPattern = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy")); System.out.println("Custom pattern : " + asCustomPattern); // Use short Belarusian date/time formatting DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(new Locale("be")); String belarusDateTime = dateTime.format(formatter); System.out.println("Belarusian locale : " + belarusDateTime); // Use short US date/time formatting DateTimeFormatter formatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).withLocale(new Locale("en-US")); String usDateTime = dateTime.format(formatter1); System.out.println("US locale : " + usDateTime);
output:
BASIC ISO DATE : 20150718 ISO WEEK DATE : 2015-W29-6 ISO DATE TIME : 2015-07-18T12:20:25.773 Custom pattern : 18/07/2015 Belarusian locale : 18.7.15 12.20 US locale : 7/18/15 12:20 PM
We parse a String
into a date, or a time, or both:
// Parsing date strings LocalDate fromIsoDate = LocalDate.parse("2015-07-20"); System.out.println("From ISO date : " + fromIsoDate); LocalDate fromIsoWeekDate = LocalDate.parse("2015-W01-2", DateTimeFormatter.ISO_WEEK_DATE); System.out.println("From ISO week date : " + fromIsoWeekDate); LocalDate fromCustomPattern = LocalDate.parse("25.07.2015", DateTimeFormatter.ofPattern("dd.MM.yyyy")); System.out.println("From custom pattern : " + fromCustomPattern);
output:
From ISO date : 2015-07-20 From ISO week date : 2014-12-30 From custom pattern : 2015-07-25
Time zones
Working with time zones is simplified by the new API. The LocalDate
/LocalTime
classes
do not contain information about a time zone. If we want to work with a date/time in a certain time zone we
can use ZonedDateTime
or OffsetDateTime
.
The time zones are represented by the ZoneId
class. You can create a ZoneId
object
using the ZoneId.of(...)
. Here is an example:
ZoneId by = ZoneId.of("GMT+3");
The parameter passed to the of()
method is the ID of the time zone to create a ZoneId
for. In the example above the ID is "GMT+3
" (Belarus timezone) which is an offset from GMT (Greenwich)
time. You can find the GMT (or UTC) offset for the desired time zone and create an ID matching it by combining
"GMT" (or "UTC") with the offset (e.g. "+3" or "-5").
You can also use another type of time zone ID which consists of the name of the location where the time zone is active. Here is an example:
ZoneId berlin = ZoneId.of("Europe/Berlin");
You can create a ZonedDateTime
object in several ways. The first way is to call the now()
method of the ZonedDateTime
class. Here is an example of creating a ZonedDateTime
object
using the now()
method:
ZonedDateTime zdtNow = ZonedDateTime.now();
Another way to create a ZonedDateTime
object is to use the of(...)
method which can
create a ZonedDateTime
object from a concrete date and time. Here is an example of creating a
ZonedDateTime
object using the of(...)
method (assuming you are at
GMT+3 - Minsk, Belarus
):
ZoneId by = ZoneId.of("GMT+3"); ZoneId berlin = ZoneId.of("Europe/Berlin"); System.out.println("Minsk Zone : " + by); System.out.println("Berlin Zone : " + berlin); LocalDateTime ldt = LocalDateTime.now(); ZonedDateTime zdtNow = ZonedDateTime.now(); ZonedDateTime zdtBy = ZonedDateTime.of(ldt, by); ZonedDateTime zdtBerlin = ZonedDateTime.of(ldt, berlin); System.out.println("Minsk : " + zdtBy); System.out.println("Berlin : " + zdtBerlin); System.out.println("Difference with Berlin : " + zdtBy.until(zdtBerlin, ChronoUnit.HOURS)); System.out.println("Difference with here now : " + zdtBy.until(zdtNow, ChronoUnit.HOURS));
output:
Minsk Zone : GMT+03:00 Berlin Zone : Europe/Berlin Minsk : 2015-08-08T23:59:47.042+03:00[GMT+03:00] Berlin : 2015-08-08T23:59:47.042+02:00[Europe/Berlin] Difference with Berlin : 1 Difference with here now : 0
![]() ![]() ![]() |