Use TimeUnit, use timeunit

Source: Internet
Author: User

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
  • General statement
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 ();;}}

  

  • TimeUnit statement
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 ();;}}

  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.