基于Java打工人下班倒计时源码

93 阅读1分钟
package com.cn.example.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeTest {

    public static long midTime;

    public static void main(String[] args) {
        Calendar instance = Calendar.getInstance();
        //获取年月日
        int month = Integer.parseInt(new SimpleDateFormat("MM").format(new Date())) - 1;
        int day = Integer.parseInt(new SimpleDateFormat("dd").format(new Date()));
        int year = Integer.parseInt(new SimpleDateFormat("YYYY").format(new Date()));
        //这里可以改时间
        instance.set(year, month, day, 17, 30, 00);
        Date date = new Date();
        midTime = (instance.getTimeInMillis() - date.getTime()) / 1000;

        time2();
    }

    /**
     * 设定时间戳,倒计时
     */
    private static void time2() {
        while (midTime > 0) {
            midTime--;
            long hh = midTime / 60 / 60 % 60;
            long mm = midTime / 60 % 60;
            long ss = midTime % 60;
            System.out.println("距离下班还有" + hh + "小时" + mm + "分钟" + ss + "秒");
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}