Java 8 Date Time

This article introduces Java 8 Date Time API with many example lines of code. So by reading each line, we can understand how to work with this feature.

A. CONCEPT

I. Local Date – Time
1. LocalDate
		// 2016-12-18
		LocalDate currentDate = LocalDate.now();
		// 2016-12-19
		LocalDate specDay = LocalDate.of(2016, Month.DECEMBER, 19);
		// 2016-12-20
		LocalDate parseDay = LocalDate.parse("2016-12-20");
		// 2016-12-30
		LocalDate day365 = LocalDate.ofYearDay(2016, 365);
		// 353th day of the Year
		int dayOfYear = currentDate.getDayOfYear();
		// 366
		int lengthOfYear = currentDate.lengthOfYear();
		// true
		boolean isLeapYear = currentDate.isLeapYear();
		// SUNDAY
		DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
2. LocalTime
		// 16:49:18.175 for example
		LocalTime currentTime = LocalTime.now();
		// it depends on currentTime >> 16:49:18.175 -> 60558
		int secondOfDay = currentTime.toSecondOfDay();
		// 12:34
		LocalTime specHM = LocalTime.of(12, 34);
		// 12:34:56
		LocalTime specHMS = LocalTime.of(12, 34, 56);
		// 01:23:45
		LocalTime parseHMS = LocalTime.parse("01:23:45");
		// 12345th second of a day is [03:25:45]
		LocalTime secondsOfDay = LocalTime.ofSecondOfDay(12345);
3. LocalDateTime
		// 2016-12-18T16:53:22.584
		LocalDateTime currentDateTime = LocalDateTime.now();
		// 2016
		int year = currentDateTime.getYear();
		// DECEMBER
		Month month = currentDateTime.getMonth();
		// OCTOBER
		Month firstMonthOfQuarter = month.firstMonthOfQuarter();
		// 18
		int day = currentDateTime.getDayOfMonth();
		// 16
		int hour = currentDateTime.getHour();
		// 53
		int minute = currentDateTime.getMinute();
		// 22
		int seconds = currentDateTime.getSecond();
		// 2016-12-24T12:34
		LocalDateTime XMas = LocalDateTime.of(2016, Month.DECEMBER, 24, 12, 34);
II. Plus and Minus Date – Time
		// tomorrow
		LocalDate tomorrow = LocalDate.now().plusDays(1);
		// before now 1h30
		LocalDateTime dateTimeBefore = LocalDateTime.now().minusHours(1).minusMinutes(30);

		/* Chrono Units Enum */
		// next week from current date
		LocalDate nextWeek = currentDate.plus(1, ChronoUnit.WEEKS);
		// next centuries from current date: 2116-12-18
		LocalDate nextDecade = currentDate.plus(1, ChronoUnit.CENTURIES);
III. TemporalAdjusters
		// nextTuesday: 2016-12-20
		LocalDate nextTuesday = currentDate.with(TemporalAdjusters.next(DayOfWeek.TUESDAY));
		// firstDayOfMonth: 2016-12-01
		LocalDate firstDayOfMonth = currentDate.with(TemporalAdjusters.firstDayOfMonth());
IV. Period & Duration
		LocalDate certainDay = currentDate.plus(8, ChronoUnit.MONTHS).plus(1, ChronoUnit.YEARS);
		// P1Y8M
		Period period = Period.between(currentDate, certainDay);
		// 8 months
		int months = period.getMonths();
		// 1 year
		int years = period.getYears();

		Duration twelveHours = Duration.ofHours(12);
		LocalTime timeAfter = currentTime.plus(twelveHours);
		// PT-12H
		Duration duration = Duration.between(currentTime, timeAfter);
V. TimeZone
		// 2016-12-18T12:34
		LocalDateTime localdateTime = LocalDateTime.of(2016, 12, 18, 12, 34);
		// Asia/Bangkok
		ZoneId currentZone = ZoneId.systemDefault();
		// Europe/London
		ZoneId londonZone = ZoneId.of("Europe/London");
		// 2016-12-18T12:34+07:00[Asia/Bangkok]
		ZonedDateTime hereDateTime = ZonedDateTime.of(localdateTime, currentZone);
		// 2016-12-18T05:34Z[Europe/London]
		ZonedDateTime londonDateTime = hereDateTime.withZoneSameInstant(londonZone);
		// +07:00
		ZoneOffset offset = hereDateTime.getOffset();
		// a collection of all available zones
		// [Asia/Aden, America/Cuiaba, Etc/GMT+9, Etc/GMT+8,... ]
		Set allZoneIds = ZoneId.getAvailableZoneIds();
		// +05:00
		ZoneOffset customOffset = ZoneOffset.of("+05:00");
		// 2016-12-18T12:34+05:00
		OffsetDateTime plusFive = OffsetDateTime.of(localdateTime, customOffset);
VI. Formatting and Parsing
		// 2017-11-28T12:34
		LocalDateTime newDateTime = LocalDateTime.of(2017, Month.NOVEMBER, 28, 12, 34);
		// 20171128
		String basicIsoDate = newDateTime.format(DateTimeFormatter.BASIC_ISO_DATE);
		// 2017-11-28T12:34:00
		String isoDateTime = newDateTime.format(DateTimeFormatter.ISO_DATE_TIME);
		// 28/11/2017
		String customDateTime = newDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
		// 2017-01-01
		LocalDate date1 = LocalDate.parse("2017-01-01");
		// 2016-12-24
		LocalDate date2 = LocalDate.parse("20161224", DateTimeFormatter.BASIC_ISO_DATE);
		// 2016-12-23
		LocalDate date3 = LocalDate.parse("2016/23/12", DateTimeFormatter.ofPattern("yyyy/dd/MM"));

B. SOURCE CODE

Java8DateTime



By grokonez | December 18, 2016.

Last updated on June 4, 2017.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*