java.time.LocalDateTime.of() Method Example


Advertisements

Description

The java.time.LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) method obtains an instance of LocalDateTime from year, month, day, hour, minute, second and nanosecond.

Declaration

Following is the declaration for java.time.LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) method.

public static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)

Parameters

  • year − the year to represent, from MIN_YEAR to MAX_YEAR

  • month − the month-of-year to represent

  • dayOfMonth − the day-of-month to represent, from 1 to 31

  • hour − the hour-of-day to represent, from 0 to 23

  • minute − the minute-of-hour to represent, from 0 to 59

  • second − the second-of-minute to represent, from 0 to 59

  • nanoOfSecond − the nano-of-second to represent, from 0 to 999,999,999

Return Value

the local date-time, not null.

Exceptions

DateTimeException − if the value of any field is out of range, or if the day-of-month is invalid for the month-year.

Example

The following example shows the usage of java.time.LocalDateTime.of(int year, Month month, int dayOfMonth, int hour, int minute, int second) method.

package com.tutorialspoint;

import java.time.LocalDateTime;
import java.time.Month;

public class LocalDateTimeDemo {
   public static void main(String[] args) {
 
      LocalDateTime date = LocalDateTime.of(2017,Month.FEBRUARY,3,6,30,40,50000);
      System.out.println(date);  
   }
}

Let us compile and run the above program, this will produce the following result −

2017-02-03T06:30:40.000050
Advertisements