Java 8 Date Time api

In this article, we will discuss the new Date Time api introduced in JDK 8.
 
Java 8 Local Date Time

Java 8 Date Time api

 
A new Joda Time api was introduced in JDK 8 with the package java.time

Here are some important classes in java.time package :
 

LocalDate

The LocalDate class represents a date in year-month-day form and is useful for representing a date without time.

 

LocalTime

LocalTime class represents time of the day.

 

LocalDateTime

LocalDateTime class handles both date and time

 

Using LocalDate and LocalTime

 
LocalDate, LocalTime or LocalDateTime classes can not be instantiated with new operator.

To get the current Date and Time, we can use the static now() method of LocalDate and LocalTime classes.

The following program uses LocalDate and LocalTime classes to display system date and time.

package com.topjavatutorial.java8examples;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class DateTimeDemo {

  public static void main(String[] args) {

    LocalDate date = LocalDate.now();
    System.out.println(date);
    
    LocalTime time = LocalTime.now();
    System.out.println(time);
    
    LocalDateTime dateTime = LocalDateTime.now();
    System.out.println(dateTime);
  }

}


 

Output

 
2016-03-27
11:36:39.495
2016-03-27T11:36:39.495

 

Formatting Date and Time

 
The classes LocalDate, LocalTime and LocalDateTime provide a format() method

We can use this to specify a different format for the date and time.

The syntax for format() method is :


String format(DateTimeFormatter fmt)

 
To obtain a DateTimeFormatter, we can use following factory methods :


static DateTimeFormatter ofLocalizedDate(FormatStyle style)

static DateTimeFormatter ofLocalizedTime(FormatStyle style)

static DateTimeFormatter ofLocalizedDateTime(FormatStyle style)

 
FormatStyle is an enumeration that defines following constants :

FULL

LONG

MEDIUM

SHORT
 
Here is an example that uses DateTimeFormatter to show current date and time using different format styles.

package com.topjavatutorial.java8examples;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

public class DateTimeFormatDemo {

  public static void main(String[] args) {

    LocalDate date = LocalDate.now();
    System.out.println(date.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)));
    
    LocalTime time = LocalTime.now();
    System.out.println(time.format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)));
    
    LocalDateTime dateTime = LocalDateTime.now();
    System.out.println(dateTime.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)));
  }

}


 

Output

 
Sunday, March 27, 2016
11:37 AM
Mar 27, 2016 11:37:36 AM

 

Obtaining Date from specified String

 
LocalDate class contains a parse method using which we can get an instance of LocalDate from a text string using a specific formatter.



public static LocalDate parse(CharSequence text)
                              
public static LocalDate parse(CharSequence text,
                              DateTimeFormatter formatter)

We can similarly use parse() method in Time and LocalDateTime classes for parsing time or date time.
 
The parse() method retuns a DateTimeParseException if text can not be parsed due to incorrect formatting.

package com.topjavatutorial.java8examples;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormatDemo {

  public static void main(String[] args) {

    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("MM/dd/yyyy");
    LocalDate date = LocalDate.parse("01/01/2016", dateFormatter);
    System.out.println("Parsed date is "+ date);
    
    DateTimeFormatter dateTimeformatter = DateTimeFormatter.ofPattern("MMMM dd yyyy hh':'mm a");
    LocalDateTime dateTime = LocalDateTime.parse("June 01 2015 12:01 AM",dateTimeformatter);
    System.out.println("Parsed datetime is "+ dateTime);
  }

}


 

Output

 

Parsed date is 2016-01-01
Parsed datetime is 2015-06-01T00:01

 
You may want to read more about parsing a String to LocalDate or LocalDateTime in this article :

Convert String to LocalDate / LocalDateTime in Java 8
 

Date Time Calculations

 
LocalDate and LocalTime classes have built-in calculation methods.

package com.topjavatutorial.java8examples;

import java.time.LocalDate;
import java.time.LocalTime;

public class DateTimeCalculationDemo {

  public static void main(String[] args) {

    LocalDate date = LocalDate.now();
    System.out.println("Today = " + date);
    System.out.println("Today + 7 days = " + date.plusDays(7));
    System.out.println("Today + 2 weeks = " + date.plusWeeks(2));
    
    LocalTime time = LocalTime.now();
    System.out.println("\nCurrent time = " + time);
    System.out.println("Current Time +  2 hours = " + time.plusHours(2));
    System.out.println("Current Time + 15 mins = " + time.plusMinutes(15));
  }

}


 

Output

 
Today = 2016-03-27
Today + 7 days = 2016-04-03
Today + 2 weeks = 2016-04-10

Current time = 13:36:57.074
Current Time + 2 hours = 15:36:57.074
Current Time + 15 mins = 13:51:57.074

 
 

You may also like reading

Java 8 new features

Top 10 Tricky Java Puzzles

Top 10 Recursion Coding Interview Questions

Top Java Tutorial Articles : March 2016

 
 

© 2016 – 2017, https:. All rights reserved. On republishing this post, you must provide link to original post

Leave a Reply.. code can be added in <code> </code> tags