- The of(int year, int month, int dayOfMonth, int hour, int minute) method of LocalDateTime class in Java is used to create an instance of LocalDateTime from the input year, month, day, hour and minute. The instance of LocalDateTime represents the date along with time. The parameters year, month and day are used to determine the date and the parameters hour and minute are used to determine the time. The second and nano-second will be set to zero by default.
Syntax:
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute)Parameters: This method accepts five parameters.- year - It is of integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
- month - It is of integer type and represents the month of the year. It varies from 1(JANUARY) to 12(DECEMBER).
- dayOfMonth - It is of integer type and represents the day of the month. It varies from 1 to 31.
- hour - It is of integer type and represents the hour of the day. It varies from 0 to 23.
- minute - It is of integer type and represents the minute of the hour. It varies from 0 to 59.
Java // Java program to demonstrate // LocalDateTime.of(int year, int month, // int dayOfMonth, int hour, int minute) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create LocalDateTime object LocalDateTime localdatetime = LocalDateTime.of( 2020, 5, 13, 6, 30); // print full date and time System.out.println("DateTime: " + localdatetime); } }
Output:Program 2:DateTime: 2020-05-13T06:30
Java // Java program to demonstrate // LocalDateTime.of(int year, int month, // int dayOfMonth, int hour, int minute) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create LocalDateTime object LocalDateTime localdatetime = LocalDateTime.of( 2019, 5, 13, 6, 30); // print year only System.out.println( "Year: " + localdatetime.getYear()); } }
Output:Year: 2019
- The of(int year, int month, int dayOfMonth, int hour, int minute, int second) method of LocalDateTime class in Java is used to create an instance of LocalDateTime from the input year, month, day, hour, minute and second. The parameters year, month and day are used to determine the date and the parameters hour, minute and second are used to determine the time. This method is used where nanosecond is not required as the value of nanoSecond will be set to zero by default.
Syntax:
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second)Parameters: This method accepts six parameters:- year - It is of integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
- month - It is of integer type and represents the month of the year. It varies from 1(JANUARY) to 12(DECEMBER).
- dayOfMonth - It is of integer type and represents the day of the month. It varies from 1 to 31.
- hour - It is of integer type and represents the hour of the day. It varies from 0 to 23.
- minute - It is of integer type and represents the minute of the hour. It varies from 0 to 59
- second - It is of integer type and represents the second of the minute. It varies from 0 to 59.
Java // Java program to demonstrate // LocalDateTime.of(int year, int month, // int dayOfMonth, int hour, int minute, // int second) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // Create LocalDateTime object LocalDateTime localdatetime = LocalDateTime.of( 2020, 5, 13, 6, 30, 45); // print full date and time System.out.println("DateTime: " + localdatetime); } }
Output:Program 2:DateTime: 2020-05-13T06:30:45
Java // Java program to demonstrate // LocalDateTime.of(int year, int month, // int dayOfMonth, int hour, int minute, // int second) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create LocalDateTime object LocalDateTime localdatetime = LocalDateTime.of( 2019, 5, 13, 6, 30, 45); // print dayofmonth only System.out.println( "DayOfMonth: " + localdatetime.getDayOfMonth()); } }
Output:DayOfMonth: 13
- The of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoSecond) method of LocalDateTime class in Java is used to create an instance of LocalDateTime from the input year, month, day, hour, minute, second and nanosecond. The instance of LocalDateTime represents the date along with time. The parameters year, month and day are used for date and parameters hour, minute, second and nanosecond are used for time.
Syntax:
public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond)Parameters: This method accepts seven parameters:- year - It is of integer type and represents the year. It varies from MIN_YEAR to MAX_YEAR.
- month - It is of integer type and represents the month of the year. It varies from 1(JANUARY) to 12(DECEMBER).
- dayOfMonth - It is of integer type and represents the day of the month. It varies from 1 to 31.
- hour - It is of integer type and represents the hour of the day. It varies from 0 to 23.
- minute - It is of integer type and represents the minute of the hour. It varies from 0 to 59.
- second - It is of integer type and represents the second of the minute. It varies from 0 to 59.
- nanoOfSecond - It is of integer type and represents the nano second. It varies from 0 to 999999999.
Java // Java program to demonstrate // LocalDateTime.of(int year, int month, // int dayOfMonth, int hour, int minute, // int second, int nanoOfSecond) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create LocalDateTime object LocalDateTime localdatetime = LocalDateTime.of(2020, 5, 13, 6, 30, 45, 20000); // print full date and time System.out.println("DateTime: " + localdatetime); } }
Output:Program 2:DateTime: 2020-05-13T06:30:45.000020
Java // Java program to demonstrate // LocalDateTime.of(int year, int month, // int dayOfMonth, int hour, int minute, // int second, int nanoOfSecond) method import java.time.*; import java.time.temporal.*; public class GFG { public static void main(String[] args) { // create LocalDateTime object LocalDateTime localdatetime = LocalDateTime.of( 2019, 5, 13, 6, 30, 45, 50000); // print nanoOfSecond only System.out.println( "NanoOfSecond: " + localdatetime.getNano()); } }
Output:
References:
NanoOfSecond: 50000
- https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#of(int, int, int, int, int)
- https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#of(int, int, int, int, int, int)
- https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#of(int, int, int, int, int, int, int)