X
    Categories: Java Core

Java 8 Date Time API

Java 8 introduced a new Date-Time API which purpose is to cover drawbacks of the old date-time API.

The previous date-time api was not thread safe and the replacement for the new date-time API is that it does not have any setter methods. Another drawback that is fixed by the new API is the poor design. The old API had less direct methods for date operations. And yet another drawback of the old API was that programmers had to write a lot of code to deal with timezone issues.

Not only does the new API solve all these problems, but it also introduces 2 important classes that are in java.time package:

  • Local – no complexity of timezone handling
  • Zoned – more complex date-time API that deals with various timezones



Local time API should be used when time zones are not required

An example of Local Date-Time API which uses the main method calls

import java.time.*; 
import java.time.format.DateTimeFormatter; 
   
public class Main { 
  
    public static void LocalDateTimeAPI() { 

        LocalDate date = LocalDate.now(); 
        LocalTime time = LocalTime.now(); 
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter format = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        String formatedTime = now.format(format);      

        Month month = now.getMonth(); 
        int day = now.getDayOfMonth(); 
        int seconds = now.getSecond(); 

        System.out.println("Current date: " + date); 
        System.out.println("Current time: " + time);          
        System.out.println("Current date and time: " + now);         
        System.out.println("in foramatted manner " + formatedTime); 
        System.out.println("Month: " + month + "\nDay: " + day + "\nSeconds: " + seconds);
    } 

    public static void main(String[] args) { 
        LocalDateTimeAPI(); 
    } 
}

Output:

Current date: 2019-07-20
Current time: 14:10:58.492
Current date and time: 2019-07-20T14:10:58.492
in foramatted manner 20-07-2019 14:10:58
Month: JULY
Day: 20
Seconds: 58

Zoned Date-Time API example

import java.time.ZonedDateTime;
import java.time.ZoneId;

public class ZonedTime {

    public void testZonedDateTime() {
        ZoneId zone = ZoneId.of("Europe/Paris");
        System.out.println("ZoneId: " + zone);
            
        ZoneId currentZone = ZoneId.systemDefault();
        System.out.println("CurrentZone: " + currentZone);
    }

   public static void main(String args[]) {
        ZonedTime zonedTimeExample = new ZonedTime();
        zonedTimeExample.testZonedDateTime();
   }
}

Output

ZoneId: Europe/Paris
CurrentZone: Europe/London

As you can see, Zoned Date-Time API can give you an access of a specific time zone and can also give you your time zone or the system’s default time zone.

Chrono Units Example

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class ChronoUnits {

    public void chromoUnits() {
        LocalDate today = LocalDate.now();
        System.out.println("Current date: " + today);
          
        LocalDate week = today.plus(1, ChronoUnit.WEEKS);
        System.out.println("1 week from now: " + week);
          
        LocalDate month = today.plus(1, ChronoUnit.MONTHS);
        System.out.println("1 month from now: " + month);

        LocalDate year = today.plus(1, ChronoUnit.YEARS);
        System.out.println("1 year from now: " + year);
          
        LocalDate decade = today.plus(1, ChronoUnit.DECADES);
        System.out.println("1 decade from now: " + decade);
     }

   public static void main(String args[]) {
        ChronoUnits ChronoUnitsExample = new ChronoUnits();
        ChronoUnitsExample.chromoUnits();
   }
}

Output:

Current date: 2019-07-20
1 week from now: 2019-07-27
1 month from now: 2019-08-20
1 year from now: 2020-07-20
1 decade from now: 2029-07-20

Period and Duration

Period deals with date based amount of time and duratioin deals with time based amount of time.

import java.time.temporal.ChronoUnit;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Duration;
import java.time.Period;

public class PeriodDuration {
    public void testDuration() {
        LocalTime currentTime = LocalTime.now();
        Duration offtime5 = Duration.ofHours(5);
          
        LocalTime timeOff5Hours = currentTime.plus(offtime5);
        Duration duration = Duration.between(currentTime, timeOff5Hours);
          
        System.out.println("Duration: " + duration);
    }

    public void testPeriod() {
        LocalDate currentDate = LocalDate.now();
        System.out.println("Current date: " + currentDate);

        LocalDate nextMonth = currentDate.plus(1, ChronoUnit.MONTHS);
        System.out.println("Next month: " + nextMonth);

        Period timePeriod = Period.between(nextMonth, currentDate);
        System.out.println("Period: " + timePeriod);
    }
      
     

   public static void main(String args[]) {
        PeriodDuration periodDuration = new PeriodDuration();
        periodDuration.testPeriod();
        periodDuration.testDuration();
   }
}

Output

Current date: 2019-07-20
Next month: 2019-08-20
Period: P-1M
Duration: PT-19H

 

0 0 votes
Article Rating
ym_coding:
Related Post