OffsetTime now() method in Java with examples
- The now() method of OffsetTime class in Java obtains the current time from the system clock in the default time-zone.
Syntax :
public static OffsetTime now()
Parameter: This method does not accepts any parameter.
Return Value: It returns the current time using the system clock and default time-zone and not null.
Below programs illustrate the now() method:
Program 1 :
// Java program to demonstrate the now() method
import
java.time.OffsetTime;
public
class
GFG {
public
static
void
main(String[] args)
{
// Parses the time
OffsetTime time = OffsetTime.now();
// Prints the current time
System.out.println(
"Current time: "
+ time);
}
}
chevron_rightfilter_noneOutput:Current time: 02:58:01.700Z
- The now(clock) method of OffsetTime class in Java obtains the current time from the specified clock in the parameter.
Syntax :
public static OffsetTime now(Clock clock)
Parameter: This method accepts a single parameter clock which specifies the clock to use and not null.
Return Value: It returns the current time and not null.
Below programs illustrate the now(clock) method:
Program 1 :
// Java program to demonstrate the now(clock) method
import
java.time.OffsetTime;
import
java.time.Clock;
public
class
GFG {
public
static
void
main(String[] args)
{
// Parses the time
OffsetTime time = OffsetTime.now();
// Prints the current time
System.out.println(
"Current time: "
+ Clock.systemUTC());
}
}
chevron_rightfilter_noneOutput:Current time: SystemClock[Z]
- The now(zone) method of OffsetTime class in Java obtains the current time from the system clock in the specified time-zone in the parameter.
Syntax :
public static OffsetTime now(ZoneId zone)
Parameter: This method accepts a single parameter zone which specifies the zone ID to use, not null.
Return Value: It returns the current time and not null.
Below programs illustrate the now(clock) method:
Program 1 :
// Java program to demonstrate the now(clock) method
import
java.time.OffsetTime;
import
java.time.ZoneId;
public
class
GFG {
public
static
void
main(String[] args)
{
// Parses the time
OffsetTime time = OffsetTime.now();
// Prints the current time
System.out.println(
"Current time: "
+ ZoneId.systemDefault());
}
}
chevron_rightfilter_noneOutput:Current time: Etc/UTC
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#now-long-
Recommended Posts:
- OffsetTime with() Method in Java with Examples
- OffsetTime from() method in Java with examples
- OffsetTime get() method in Java with examples
- OffsetTime plus() method in Java with examples
- OffsetTime until() Method in Java with Examples
- OffsetTime minusMinutes() method in Java with examples
- OffsetTime getNano() method in Java with examples
- OffsetTime range() method in Java with Examples
- OffsetTime minusSeconds() method in Java with examples
- OffsetTime toString() method in Java with examples
- OffsetTime minusHours() method in Java with examples
- OffsetTime toLocalTime() method in Java with examples
- OffsetTime minusNanos() method in Java with examples
- OffsetTime minus() method in Java with examples
- OffsetTime atDate() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai