Java8- java.time.* API

package com.me.pack;package com.me.pack;
import java.time.Instant;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.Month;import java.time.MonthDay;import java.time.OffsetDateTime;import java.time.Period;import java.time.YearMonth;import java.time.ZoneId;import java.time.ZoneOffset;import java.time.ZonedDateTime;import java.time.format.DateTimeFormatter;import java.time.format.DateTimeParseException;
public class Java8Date { public static void main(String[] args) { // java.time.* all are thread safe and immutable // import java.time.
LocalDate localDate = null, todaydate;
localDate = LocalDate.now(); System.out.println(localDate + “—” + localDate.getYear() + “—” + localDate.getMonth() + “–” + localDate.getDayOfMonth());
todaydate = LocalDate.of(2017, 7, 26); System.out.println(todaydate);
if (localDate.equals(todaydate)) { System.out.println(“equal”); }
MonthDay monthDay = MonthDay.of(todaydate.getMonth(), todaydate.getDayOfMonth()); MonthDay currentMonthDay = MonthDay.from(todaydate); if (monthDay.equals(currentMonthDay)) { System.out.println(“equal”); }
LocalTime localTime = LocalTime.now(); System.out.println(localTime);
localDate = localDate.plusDays(5); System.out.println(localDate);
LocalDate localDateNow = LocalDate.now(); LocalDate localDateYesterday = LocalDate.of(2017, 7, 27); if (localDateYesterday.isAfter(localDateNow)) { System.out.println(“It’s tomorrow”); } LocalDate localDateTomorrow = localDateNow.minusDays(1); if (localDateNow.isBefore(localDateYesterday)) { System.out.println(“It’s yesterday”); }
ZoneId zoneId = ZoneId.of(“America/New_York”); LocalDateTime localDateTime = LocalDateTime.now(); ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId); System.out.println(zonedDateTime);
YearMonth yearMonth = YearMonth.now(); YearMonth expirationYearMonth = YearMonth.of(2090, 3); System.out.println(expirationYearMonth);
if (localDateNow.isLeapYear()) { System.out.println(“Leap Year”); } else { System.out.println(“Not Leap year”); }
Period period = Period.between(localDateTomorrow, localDateNow); System.out.println(period.getDays());
Instant currentStamp = Instant.now(); System.out.println(currentStamp);
LocalDateTime datetime = LocalDateTime.of(2014, Month.JANUARY, 14, 19, 30); ZoneOffset offset = ZoneOffset.of(“+05:30”); OffsetDateTime date = OffsetDateTime.of(datetime, offset); System.out.println(“Date and Time with timezone offset in Java : ” + date);
// String stringDate = “20140912”; // LocalDate dateFormat = LocalDate.parse(stringDate, // DateTimeFormatter.ISO_OFFSET_DATE); // System.out.println(“++”, tringDate, dateFormat);
String goodFriday = “Apr 18 2014”; try { DateTimeFormatter formatter = DateTimeFormatter.ofPattern(“MMM dd yyyy”); LocalDate holiday = LocalDate.parse(goodFriday, formatter); System.out.printf(“Successfully parsed String %s, date is %s%n”, goodFriday, holiday); } catch (DateTimeParseException ex) { System.out.printf(“%s is not parsable!%n”, goodFriday); ex.printStackTrace(); }
}}

 

2017-07-26—2017—JULY–26
2017-07-26
equal
equal
08:43:07.913
2017-07-31
It’s tomorrow
It’s yesterday
2017-07-26T08:43:07.914-04:00[America/New_York]
2090-03
Not Leap year
1
2017-07-26T12:43:07.918Z
Date and Time with timezone offset in Java : 2014-01-14T19:30+05:30
Successfully parsed String Apr 18 2014, date is 2014-04-18
Picked up _JAVA_OPTIONS: -Xmx512M

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s