Java Date Time - Java Clock Period








Clock

Clock class represents the real-world clock.

We can access to the current instant, date, and time in a time zone from Clock class.

We can create a clock for the system default time zone as follows.

Clock clock = Clock.systemDefaultZone();

To create a clock for a specified time zone.

ZoneId  z = ZoneId.of("America/Los_Angeles"); 
Clock  clock2 = Clock.system(z);

To get the current instant, date, and time from a clock, use the now(Clock c) method of the datetime related classes.

import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
//from  w w w  . j  ava  2  s. c om
public class Main {
  public static void main(String[] args) {
    Clock clock = Clock.systemDefaultZone();

    Instant instant1 = clock.instant();
    System.out.println(instant1);

    Instant instant2 = Instant.now(clock);
    System.out.println(instant2);

    LocalDate localDate = LocalDate.now(clock);
    System.out.println(localDate);

    ZonedDateTime zoneDateTime = ZonedDateTime.now(clock);
    System.out.println(zoneDateTime);
  }
}

The code above generates the following result.





Period

A period is a span of time in term of years, months, and days.

Negative periods are supported.

A duration is also a span of time measured in terms of seconds and nanoseconds.

A duration represents an exact number of nanoseconds which is for machine. A period is more for human.

1 day, 2 months, 3 days, 4 months and 5 days are all examples of periods. 2 month period may mean different number of days depending on different months.

We can use the following methods to create a Period.

static Period of(int years,int months, int days)
static Period ofDays(int days)
static Period ofMonths(int months)
static Period ofWeeks(int weeks)
static Period ofYears(int years)

The following code shows how to create Period.

import java.time.Period;
//from w  w  w.  ja  va2s. co  m
public class Main {
  public static void main(String[] args) {
    Period p1 = Period.of(2, 3, 5); // 2 years, 3 months, and 5 days
    Period p2 = Period.ofDays(2);  // 2 days
    Period p3 = Period.ofMonths(-3); // -3 months
    Period p4 = Period.ofWeeks(3); // 3 weeks 
    System.out.println(p1);
    System.out.println(p2);
    System.out.println(p3);
    System.out.println(p4);

  }
}

The code above generates the following result.





Example 2

Additions, subtractions, multiplications, and negation operations are supported by Period.

The division operation performs an integer division, for example, 7 divided by 3 is 2.

The following code shows how to use the operations on periods.

import java.time.Period;
/* w w  w  .jav  a  2s  .co  m*/
public class Main {
  public static void main(String[] args) {
    Period p1  = Period.ofDays(15);
    System.out.println(p1);
    Period p2  = p1.plusDays(12);
    System.out.println(p2);
    Period p3  = p1.minusDays(12);
    System.out.println(p3);
    Period p4  = p1.negated();
    System.out.println(p4);
    Period p5  = p1.multipliedBy(3);
    System.out.println(p5);
  }
}

The code above generates the following result.

Example 3

Period plus() adds one period to another period.

Period minus() subtracts one period from another period.

Period normalized() method normalizes the years and months. The method ensures that the month value stays within 0 to 11. A period of "2 years and 16 months" is normalized to "3 years and 4 months."

import java.time.Period;
// w w  w. j av  a  2 s .  c  o  m
public class Main {

  public static void main(String[] args) {
    Period p1  = Period.of(2, 3, 5); 
    Period p2  = Period.of(1, 15,   28);
    System.out.println(p1); 
    System.out.println(p2);
    System.out.println(p1.minus(p2));
    System.out.println(p1.plus(p2));
    System.out.println(p1.plus(p2).normalized()); 
  }
}

The code above generates the following result.

Period Between

The Date-Time API provides methods to compute the elapsed period between two dates and times.

We can use the between() method on one of the constants in the ChronoUnit enum.

ChronoUnit enum between() method takes two datetime objects and returns a long. If the second argument occurs before the first one, it returns a negative amount.

The returned amount is the complete number of units between two dates and times. For example, calling HOURS.between() on 06:00 and 09:30 the return value is 3, not 3.5. while MINUTES.between on 06:00 and 09:30 returns 210.

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
//w w w  .  j  a  va 2 s  . co m
public class Main {

  public static void main(String[] args) {
    LocalDate ld1  = LocalDate.of(2014, Month.JANUARY,  7); 
    LocalDate ld2  = LocalDate.of(2014, Month.MAY,  21); 
    long  days  = ChronoUnit.DAYS.between(ld1, ld2);
    System.out.println(days);
    
    LocalTime  lt1 = LocalTime.of(6, 0); 
    LocalTime  lt2 = LocalTime.of(9, 30); 
    long  hours   = ChronoUnit.HOURS.between(lt1, lt2);
    System.out.println(hours);
    long  minutes = ChronoUnit.MINUTES.between(lt1,   lt2);
    System.out.println(minutes);
  }
}

The code above generates the following result.

Period Util

The Date-Time API provides methods to compute the elapsed period between two dates and times.

We can use the until(end_date_or_time, time_unit) method on one of the datetime-related classes, for example, LocalDate, LocalTime, LocalDateTime, ZonedDateTime, etc.

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.time.temporal.ChronoUnit;
/*  w  w  w. j  ava  2 s  .  c  o m*/
public class Main {

  public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 7);
    LocalDate ld2 = LocalDate.of(2014, Month.MAY, 18);

    LocalTime lt1 = LocalTime.of(7, 0);
    LocalTime lt2 = LocalTime.of(9, 30);

    long days = ld1.until(ld2, ChronoUnit.DAYS);
    System.out.println(days);
    long hours = lt1.until(lt2, ChronoUnit.HOURS);
    System.out.println(hours);
    long minutes = lt1.until(lt2, ChronoUnit.MINUTES);
    System.out.println(minutes);
  }
}

The code above generates the following result.