Java Data Type How to - Get current day, month and year in Java 8








Question

We would like to know how to get current day, month and year in Java 8.

Answer

import java.time.LocalDate;
import java.time.LocalDateTime;
/*from   ww  w  . jav  a  2  s .co  m*/
public class Main {
  public static void main(String[] argv) {
    LocalDate today = LocalDate.now();
    System.out.println(String.format("Year : %s Month : %s day : %s",
        today.getYear(), today.getMonthValue(), today.getDayOfMonth()));

    LocalDateTime time = LocalDateTime.now();
    System.out.println(String.format("Hour : %s Mins : %s Sec : %s",
        time.getHour(), time.getMinute(), time.getSecond()));

  }
}

The code above generates the following result.