package datetime;

import java.time.LocalDateTime;

import java.time.ZoneOffset;

public class DateTime {

 

    public static void main(String[] args) {

        LocalDateTime dt = LocalDateTime.now();

        System.out.println(dt);

        System.out.println(dt.getMonth());

        System.out.println(dt.getDayOfWeek());

        dt = dt.plusDays(2);

        System.out.println(dt.getDayOfWeek());

        dt = dt.plusHours(5).plusMinutes(10);

        System.out.println(dt);

        System.out.println(dt.atOffset(ZoneOffset.ofHours(-11)));

        dt = dt.minusWeeks(20);

        System.out.println(dt);

        dt = dt.withDayOfMonth(4) .withYear(2009);

        System.out.println(dt);

    }

}

 

 

 

 

 

package datetime;

 

import java.time.Month;

import java.time.MonthDay;

import java.time.Year;

 

public class YearMonth {

    public static void main(String[] args) {

        Year y = Year.now();

        System.out.println(y.isLeap());

        System.out.println(y.isBefore(Year.of(2013)));

        System.out.println(y.isAfter(Year.parse("2013")));

       

        MonthDay md = MonthDay.of(Month.FEBRUARY, 29);

        System.out.println(md.atYear(2011));

        System.out.println(md.isValidYear(2012));

        System.out.println(md.isValidYear(2013));

    }

}

 

 

 

 

 

package datetime;

 

import java.time.Instant;

import java.time.temporal.ChronoUnit;

 

public class InstantExample {

    public static void main(String[] args) {

        Instant now = Instant.now();

        System.out.println("Epoch seconds = "+ now.getEpochSecond());

        System.out.println("Offset nanoseconds = "+ now.getNano());

        System.out.println(now.toString());

       

        now = now.minusSeconds(86400 );

        System.out.println(now.toString());

       

        System.out.println(now.until(Instant.now(), ChronoUnit.HOURS));

    }

}

 

 

 

 

 

package datetime;

 

import java.time.Duration;

import java.time.Instant;

 

public class Durations {

    public static void main(String[] args) {

        Instant i1 = Instant.now();

        try {

            Thread.sleep(1050L);

        } catch (InterruptedException ex) {

        }

        Instant i2 = Instant.now();

        Duration d = Duration.between(i1, i2);

        System.out.printf("%d seconds and %d nanoseconds elapsed\n",

 d.getSeconds(), d.getNano());

    }

}

 

 

 

 

 

 

package datetime;

 

import java.time.LocalDate;

import java.time.Month;

import java.time.Period;

 

public class Periods {

    public static void main(String[] args) {

        LocalDate ldt1 = LocalDate.of(2008, Month.JANUARY, 22);

        LocalDate ldt2 = LocalDate.of(2014, Month.MARCH, 12);

        Period p = Period.between(ldt1, ldt2);

        System.out.printf("%d years, %d months, %d days\n",

p.getYears(), p.getMonths(), p.getDays());   

    }

}