Use TimeUnit, use timeunit
TimeUnit is a class in the java. util. concurrent package, indicating the time period of the given unit granularity.
Main functions
- Time granularity Conversion
- Latency
Common Granularity
TimeUnit. DAYS // day TimeUnit. HOURS // hour TimeUnit. MINUTES // minute TimeUnit. SECONDS // second TimeUnit. MILLISECONDS/millisecond
1. time granularity Conversion
Public long toMillis (long d) // convert to millisecond public long toSeconds (long d) // convert to second public long toMinutes (long d) // convert to minute public long toHours (long d) // convert to hour public long toDays (long d) // convert to day
Example
Package com. app; import java. util. concurrent. timeUnit; public class Test {public static void main (String [] args) {// 24 hours a day 1 represents 1 day: 1 day is converted to an hour System. out. println (TimeUnit. DAYS. toHours (1); // result: 24 // 3600 seconds for 1 hour System. out. println (TimeUnit. HOURS. toSeconds (1); // result 3600 // Convert 3 days to hour System. out. println (TimeUnit. HOURS. convert (3, TimeUnit. DAYS); // The result is: 72 }}
2. latency
Package com. app; public class Test2 {public static void main (String [] args) {new Thread (new Runnable () {@ Overridepublic void run () {try {Thread. sleep (5*1000); System. out. println ("delayed completion");} catch (InterruptedException e) {e. printStackTrace ();}}}). start ();;}}
Package com. app; import java. util. concurrent. timeUnit; public class Test2 {public static void main (String [] args) {new Thread (new Runnable () {@ Overridepublic void run () {try {TimeUnit. SECONDS. sleep (5); System. out. println ("latency 5 seconds, completed");} catch (InterruptedException e) {e. printStackTrace ();}}}). start ();;}}