- 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.
Return Value: This method returns the localdate-time as derived from the input value.
Exceptions: This method throws DateTimeException if the value of any field is out of the above mentioned range or the day-of-month is invalid for the month-year.
Below programs illustrate the of(int year, int month, int dayOfMonth, int hour, int minute) method in Java:
Program 1:
// 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:DateTime: 2020-05-13T06:30
Program 2:
// 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.
Return Value: This method returns the localdate-time as computed from the input value.
Exceptions: This method throws DateTimeException if the value of any field is out of the above mentioned range or the day-of-month is invalid for the month-year.
Below programs illustrate the of(int year, int month, int dayOfMonth, int hour, int minute, int second) method in Java:
Program 1:
// 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:DateTime: 2020-05-13T06:30:45
Program 2:
// 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.
Return Value: This method returns the localdate-time.
Exceptions: This method throws DateTimeException if the value of any field is out of range or the day-of-month is invalid for the month-year.
Below programs illustrate the of(int year, int month, int dayOfMonth, int hour, int minute, int second, int nanoOfSecond) method in Java:
Program 1:
// 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:DateTime: 2020-05-13T06:30:45.000020
Program 2:
// 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:NanoOfSecond: 50000
References:
- 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)