Java Instant Now() Method Example In Java [2021]
Java Instant Now() method is overloaded method and has 2 variants
- public static Instant now()
- public static Instant now(Clock clock)
Instant now() directly gives the Instant instance with the default system clock.
Instant now(Clock clock) gives us the liberty to change Clock and provide our own customized clock.
Contents
- Java Instant Now
- 1. Java Instant now() Example
- 2. Java Instant now(Clock clock) Example
- 3. Java Instant now in UTC Timezone
- 4. Java Instant now with custom Timezone
- 5. Java Instant now Example with custom Format
- 6. Java Instant now Minus One Day Example
- 7. Java Instant now to NanoSeconds Example
- 8. Java Instant now to Date Example
- Conclusion
- References
Java Instant Now
Now we are going to see Instant class now methods examples and other Instant class examples.
1. Java Instant now() Example
This method Instant.now() gets the current instant instance from the system clock.
import java.time.Instant;
public class JavaInstantNow {
public static void main(String[] args) {
// java instant now example
Instant instant = Instant.now();
System.out.println("Instant now() : " + instant);
}
}
Output:
Instant now() : 2020-07-18T16:30:01.118Z
2. Java Instant now(Clock clock) Example
This method Instant.now(Clock clock) gets the current instant from the clock specified by user.
import java.time.Clock;
import java.time.Instant;
public class JavaInstantNowClock {
public static void main(String[] args) {
// java instant now using clock
Instant instant =
Instant.now(Clock.systemDefaultZone());
System.out.println("Java Instant now Clock : "
+ instant);
}
}
Output:
Java Instant now Clock : 2020-07-18T16:31:52.748Z
3. Java Instant now in UTC Timezone
This example shows us how we can get Instant instance in UTC Timezone.
import java.time.Clock;
import java.time.Instant;
public class JavaInstantNowUTC {
public static void main(String[] args) {
// Java Instant Now UTC
Instant instant = Instant.now(Clock.systemUTC());
System.out.println("Java Instant now UTC : " + instant);
}
}
Output:
Java Instant now UTC : 2020-07-18T16:32:31.665Z
4. Java Instant now with custom Timezone
This example shows how we can get Instant instance in custom timezone specified by user. In this below example we get output in US/Eastern timezone.
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class JavaInstantNowTimezone {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println("Instant : " + instant);
//Java Instant now with custome Timezone
ZonedDateTime time =
instant.atZone(ZoneId.of("US/Eastern"));
System.out.println("Java Instant now Timezone : "
+ time);
}
}
Output:
Instant : 2020-07-18T16:33:31.672Z
Java Instant now Timezone : 2020-07-18T12:33:31.672-04:00[US/Eastern]
5. Java Instant now Example with custom Format
In this example, we use custom format with Java Instant now instance to get instant in the format specified by the user. In the example below we are using yyyy-MM-dd HH:mm:ss format.
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class JavaInstantNowFormat {
public static void main(String[] args) {
Instant instant = Instant.now();
// Java Instant now with format example
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault());
System.out.println("Java Instant Now Format : "
+ formatter.format(instant));
}
}
Output:
Java Instant Now Format : 2020-07-18 22:03:59
6. Java Instant now Minus One Day Example
This example shows how we can get Minus one Day values from current Instant generated using Java Instant now method.
import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class JavaInstantNowMinusOneDay {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println("Instant : " + instant);
//Java Instant now Minus One Day
Instant instantMinusOneDay =
instant.minus(1, ChronoUnit.DAYS);
System.out.println("Java Instant Now Minus One Day : "
+ instantMinusOneDay);
}
}
Output:
Instant : 2020-07-18T16:34:42.145Z
Java Instant Now Minus One Day : 2020-07-17T16:34:42.145Z
7. Java Instant now to NanoSeconds Example
This example show how to convert Java Instant now instance to nanoseconds using method instant.getNano().
import java.time.Instant;
public class JavaInstantNowNanoSeconds {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println("Instant : " + instant);
//Java Instant now Nanoseconds
int nanoseconds = instant.getNano();
System.out.println("Java Instant now to Nanoseconds : "
+ nanoseconds);
}
}
Output:
Instant : 2020-07-18T16:35:47.386Z
Java Instant now to Nanoseconds : 386000000
8. Java Instant now to Date Example
This example shows how to convert Java Instant now instance to Date using Date.from(Instant instant) method.
import java.time.Instant;
import java.util.Date;
public class JavaInstantNowToDate {
public static void main(String[] args) {
Instant instant = Instant.now();
System.out.println("Instant : " + instant);
// Java Instant now to Date
Date date = Date.from(instant);
System.out.println("Java Instant now to Date : " + date);
}
}
Output:
Instant : 2020-07-18T16:36:19.769Z
Java Instant now to Date : Sat Jul 18 22:06:19 IST 2020
Conclusion
In this tutorial, we have covered Java Instant Now method examples using Instant.now(), Instant.now(Clock clock) methods with other examples to Convert Instant to Date, Changing Java Instant now to custom formatted form and custom timezone, how to convert Java Instant now instance to Date.
Thanks for reading this tutorial so far. If you like this tutorial then please share it with your friends and colleagues. If you have any questions, doubts, suggestions, or feedback then please drop a comment and I'll try to answer your question.
Happy Learning!!!
More Examples you may like
Comments
Post a Comment
If you have any doubts, Please let me know.