Java Date Time Methods








The method names in Java Date-Time API are made consistent between classes wherever possible.

For example, now method returns the date or time values of the current moment. from methods allow conversion from one class to another.

The following table lists the commonly used prefixes:

  • of
    static factory method
    factory method to creates an instance ands validate the input parameters
  • from
    static factory method converts the input parameters to an instance of the target class.
  • parse
    static factory method
    parses the input string to create an instance of the target class.
  • format
    instance method
    formats the temporal object to produce a string.
  • get
    instance method
    returns a part of the target object.
  • is
    instance method
    queries the target object.
  • with
    instance method
    returns a copy of the target object with one element changed.
  • plus
    instance method
    returns a copy of the object with an amount of time added.
  • minus
    instance method
    returns a copy of the object with an amount of time subtracted.
  • to
    instance method
    converts this object to another type.
  • at
    instance method
    Combines this object with another object.




of() Methods

Java Date-Time API ofXXX() methods are used create objects.

The following code shows how to create objects of the LocalDate class:

import java.time.LocalDate;
import java.time.Month;
/*  w  w  w  . j ava  2 s.c  o m*/
public class Main {
  public static void main(String[] args) {
    LocalDate localDate1  = LocalDate.of(2014, 5, 21); 
    System.out.println(localDate1);
    
    LocalDate localDate2  = LocalDate.of(2014, Month.MARCH, 4);
    System.out.println(localDate2);
    
    LocalDate localDate3  = LocalDate.ofEpochDay(2014);
    System.out.println(localDate3);
    
    LocalDate localDate4  = LocalDate.ofYearDay(2014, 39);
    System.out.println(localDate4);
  }
}

The code above generates the following result.





from() Methods

from() is a static factory method which is used to derive a datetime object from the specified argument.

Unlike of(), from() requires data conversion on the specified argument.

The following code shows how to derive a LocalDate from a LocalDateTime:

import java.time.LocalDate;
import java.time.LocalDateTime;
/*w  ww.  j  a v a 2s .c  o  m*/
public class Main {
  public static void main(String[] args) {
    LocalDateTime  localDateTime = LocalDateTime.of(2015, 6,  21,  13, 40);
    System.out.println(localDateTime);
    
    LocalDate localDate  = LocalDate.from(localDateTime);
    System.out.println(localDate);
  }
}

The code above generates the following result.

with() Methods

To change a field in a datetime object we can use a method with a prefix with.

withXXX() method returns a copy of an object with the specified field changed since most of the objects in Date Time API are immutable.

The following code shows how to get a LocalDate from another LocalDate with the year changed:

import java.time.LocalDate;
import java.time.Month;
//  w w  w .  ja va  2s  .  c  o  m
public class Main {
  public static void main(String[] args) {
    LocalDate localDate1  = LocalDate.of(2014, Month.MAY,  2);
    System.out.println(localDate1);
    
    LocalDate localDate2  = localDate1.withYear(2015);
    System.out.println(localDate2);
    
    LocalDate localDate3  = localDate1.withYear(2014).withMonth(7);
    System.out.println(localDate3);
    
  }
}

The code above generates the following result.

getXXX() Methods

getXXX() returns the specified element of the object.

The following code shows how to get year, month, and day from a LocalDate object:

import java.time.LocalDate;
import java.time.Month;
/* w  ww  .  ja  v  a  2 s  . co  m*/
public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.of(2014, 6, 21);
    int year = localDate.getYear();
    System.out.println(year);
    Month month = localDate.getMonth();
    System.out.println(month);

    int day = localDate.getDayOfMonth();
    System.out.println(day);

  }
}

The code above generates the following result.

toXXX() Methods

toXXX() converts an object to a related type.

The following code shows some examples of using toXXX() methods.

import java.time.LocalDate;
//from   www.j  a v a  2  s  . c om
public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.of(2014, 6, 21); 
    long days = localDate.toEpochDay(); 
    System.out.println(days);

  }
}

The code above generates the following result.

atXXX() Methods

atXXX() creates a new datetime object from an existing datetime object with additional information.

The following code use at method to add additional information to a date object.

import java.time.LocalDate;
import java.time.LocalDateTime;
//  w w w  . java2  s .c  o  m
public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21);
    System.out.println(localDate);

    LocalDateTime  localTime1 = localDate.atStartOfDay();
    System.out.println(localTime1);

    LocalDateTime  localTime2 = localDate.atTime(16, 21);
    System.out.println(localTime2);
  }
}

The code above generates the following result.

The following code shows how to use atXXX() methods which supports the builder pattern to build a local date:

import java.time.LocalDate;
import java.time.Year;

public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = Year.of(2014).atMonth(6).atDay(21);
    System.out.println(localDate);

  }
}

The code above generates the following result.

plusXXX() Methods

plusXXX() returns a copy of an object by adding a specified value.

The following code shows how to use plus method to add more time to the local date object.

import java.time.LocalDate;
/* w  ww .  j  ava  2 s .c  o m*/
public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21); 
    LocalDate localDate1  = localDate.plusDays(5);   
    System.out.println(localDate1);
    LocalDate localDate2  = localDate.plusMonths(3);
    System.out.println(localDate2);
    LocalDate localDate3  = localDate.plusWeeks(3);        
    System.out.println(localDate3);
  }
}

The code above generates the following result.

minusXXX() Methods

minusXXX() returns a copy of an object by subtracting a specified value.

The following code shows how to subtract time from local date object.

import java.time.LocalDate;
//from w w  w .  j a  v a2 s  .  c om
public class Main {
  public static void main(String[] args) {
    LocalDate localDate  = LocalDate.of(2014, 6, 21); 
    LocalDate localDate1  = localDate.minusMonths(5);   
    System.out.println(localDate1);
    LocalDate localDate2  = localDate.minusWeeks(3);
    System.out.println(localDate2);
  }
}

The code above generates the following result.

now() Method

now() method returns the current time for various class, for example LocalDate, LocalTime, LocalDateTime, ZonedDateTime.

The following code shows how to use now() method to return current date and time.

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
//from w ww .ja v a  2 s  .c  o m
public class Main {
  public static void main(String[] args) {
    LocalDate localDate = LocalDate.now(); 
    System.out.println(localDate);
    
    LocalTime  localTime  = LocalTime.now();
    System.out.println(localTime);
    
    LocalDateTime  dateTime  = LocalDateTime.now();
    System.out.println(dateTime);
    
    ZonedDateTime dateTimeWithZone  = ZonedDateTime.now();
    System.out.println(dateTimeWithZone);
  }
}

The code above generates the following result.