功能
计算距离下班还有多少秒,今天的工作时间已经过去的比例
每秒刷新一次
代码
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)+"%");
}
}