Méthode Java Instant now()

La méthode now() de la classe Java Instant est utilisée pour obtenir l'instant actuel à partir de l'horloge système.

La méthode Java Instant now() se compose de 2 paramètres :

  • Méthode Java Instant now()
  • Méthode Java Instant now (horloge)

La méthode Instant now() interrogera l’horloge UTC du système pour obtenir l’instant actuel.

La méthode Instant now(Clock clock) interrogera le moment spécifié pour obtenir l'heure actuelle.

Syntaxe

 public Instant now() public Instant now(Clock clock)  

Paramètres

horloge - l'horloge à utiliser, non nulle.

Retour

L'instant courant, non nul.

L'instant actuel utilisant l'horloge système, non nul.

Méthode Java Instant now() Exemple

Exemple 1

 import java.time.Instant; public class InstantNowExample1 { public static void main(String[] args) { Instant instant = Instant.now(); System.out.println(instant.getEpochSecond()); } }  
Testez-le maintenant

Sortir:

La sortie sera comme ceci.

 1410134852  

Exemple 2

 import java.time.Instant; public class InstantNowExample2 { public static void main(String... args) { Instant i = Instant.now(); System.out.println(i); } }  
Testez-le maintenant

Sortir:

La sortie sera comme ceci.

 2017-05-01T20:57:32.709Z  

Méthode Java Instant now (horloge) Exemple

Exemple 3

 import java.time.Clock; import java.time.Instant; public class InstantNowExample3 { public static void main(String[] args) { Instant instant = Instant.now(Clock.systemUTC()); System.out.println(instant); } }  
Testez-le maintenant

Sortir:

La sortie sera comme ceci.

 2017-03-15T09:18:34.062Z  

Exemple 4

 import java.time.Clock; import java.time.Instant; import java.time.ZoneId; public class InstantNowExample4 { public static void main(String... args) { Instant i = Instant.now(Clock.system(ZoneId.of('America/Chicago'))); System.out.println(i); } }  
Testez-le maintenant

Sortir:

La sortie sera comme ceci.

 2017-05-01T20:57:34.818Z