Java - from( ) Methods


from() static factory method derives a datetime object from the specified argument.

from() method requires data conversion on the specified argument.

Using a from() method, you derive a new datetime object from the specified argument.

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


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

public class Main {
  public static void main(String[] args) {
    LocalDateTime ldt = LocalDateTime.of(2012, 5, 2, 15, 30); // 2012-05-02T15:30
    LocalDate ld = LocalDate.from(ldt); // 2012-05-02
    System.out.println(ldt);//from w w  w  .  java  2 s  . co m