跳至主要內容

TimeUnit时间单元

wangdx大约 2 分钟

1、
public static void sleep(long millis) throws InterruptedException

2、
public static void sleep(long millis, int nanos) throws InterruptedException

3、
public final void wait(long timeoutMillis) throws InterruptedException

4、
package com.yootk;

import java.util.concurrent.TimeUnit;

public class YootkDemo { // 李兴华高薪就业编程训练营
    public static void main(String[] args) { // 沐言科技:www.yootk.com
        long hour = 1; // 当前的时间单元是表示小时的数据
        // 现在需要将一个小时的单元转变为秒
        long second = TimeUnit.SECONDS.convert(hour, TimeUnit.HOURS); // 由小时转为分钟
        System.out.println("小时转秒数:" + second);
    }
}


5、
package com.yootk;

import java.util.concurrent.TimeUnit;

public class YootkDemo { // 李兴华高薪就业编程训练营
    public static void main(String[] args) { // 沐言科技:www.yootk.com
        long hour = 1; // 当前的时间单元是表示小时的数据
        // 现在需要将一个小时的单元转变为秒
        long second = TimeUnit.MINUTES.convert(hour, TimeUnit.HOURS); // 由小时转为分钟
        System.out.println("小时转分钟:" + second);
    }
}


6、
package com.yootk;

import java.util.concurrent.TimeUnit;

public class YootkDemo { // 李兴华高薪就业编程训练营
    public static void main(String[] args) { // 沐言科技:www.yootk.com
        long day = 1; // 当前的时间单元是表示小时的数据
        // 现在需要将一个小时的单元转变为秒
        long second = TimeUnit.SECONDS.convert(day, TimeUnit.DAYS); // 由天转为秒
        System.out.println("一天的秒数:" + second);
    }
}


7、
package com.yootk;

import java.util.concurrent.TimeUnit;

public class YootkDemo { // 李兴华高薪就业编程训练营
    public static void main(String[] args) { // 沐言科技:www.yootk.com
        java.time.Duration duration = java.time.Duration.ofHours(2).plusHours(2); // 2小时 + 2小时
        // 以上给出的是两个小时的概念
        System.out.println("时间间隔:" + duration);
        long second = TimeUnit.SECONDS.convert(duration); // 直接实现了转换
        System.out.println("间隔的秒数:" + second);
    }
}


8、
package com.yootk;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class YootkDemo { // 李兴华高薪就业编程训练营
    public static void main(String[] args) { // 沐言科技:www.yootk.com
        long current = System.currentTimeMillis(); // 获取当前的时间戳
        long after = current + TimeUnit.MILLISECONDS.convert(180, TimeUnit.DAYS);
        System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(new Date(after)));
    }
}


9、
package com.yootk;

import java.util.concurrent.TimeUnit;

public class YootkDemo { // 李兴华高薪就业编程训练营
    public static void main(String[] args) { // 沐言科技:www.yootk.com
        for (int x = 0; x < 100; x++) {
            try {
                TimeUnit.SECONDS.sleep(2);// 根据秒来休眠
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("【x = " + x + "】沐言科技:www.yootk.com");
        }
    }
}

demo


上次编辑于: