下班计时器

该程序使用Java编写,实现每秒实时更新并显示距离下班剩余的秒数以及今天工作时间已过去的比例。通过当前时间与设定的下班时间(18:00:00,即64800秒)比较,计算剩余秒数和完成比例,帮助用户跟踪工作进度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

功能

计算距离下班还有多少秒,今天的工作时间已经过去的比例
每秒刷新一次

代码

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

public class Calculate {

    public static void main(String[] args) {
        Calculate calculate = new Calculate();
        final long timeInterval = 1000;
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                while (true) {
                    calculate.getTime();
                    try {
                        Thread.sleep(timeInterval);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();
    }

    public void getTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        Date currentTime = new Date();
        String current = simpleDateFormat.format(currentTime);
        //String targetTime = "18:00:00";
        int targetTime = 64800;
        String[] split = current.split(":");
        String currentHour = split[0];
        String currentMinute = split[1];
        String currentSecond = split[2];
        int i1 = Integer.parseInt(currentHour);
        int i2 = Integer.parseInt(currentMinute);
        int i3 = Integer.parseInt(currentSecond);
        int sumCurrent = i1 * 60 * 60 + i2 * 60 + i3;
        int dif = targetTime - sumCurrent;
        double dif2 = dif;
        double ratio = (32400-dif2)/32400;
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println("还剩"+dif+"秒下班,今天工作时间已经过了"+df.format(ratio*100)+"%");
    }
}

运行效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

skye111321

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值