【发布时间】:2017-04-04 02:43:18
【问题描述】:
我有一个用于创建 24 小时倒计时的代码。
此代码检查“日期”文件是否存在,如果不存在,则创建一个,其中包含每天 24 小时的日期和时间。然后它获取当前时间并比较两者,以创建从当前日期到文档中日期的倒计时。
这使得即使代码“关闭”也可以保存计时器并检查它已经走了多远。唯一的问题是有时计时器会变成负数。就像我从一开始就运行代码一样,没有在星期一创建“日期”文件,就在午夜之前,让我们说星期一晚上十一点半。然后,如果我在当前日期已过午夜时停止代码并再次运行它,那么它实际上是星期二,但在它达到实际目标计时器之前仍然缺少 23 小时。如果是这种情况,倒计时中剩余的时间为负数。就像它会显示“-1 天 23 小时 60 分钟和 60 秒剩余”一样。但是,如果作为示例,它在周二午夜过后从头开始运行,然后在同一天 30 分钟后重新启动,则没有问题。
我希望你能理解问题是什么,通过文字表达有点困难。但我附上了我的整个代码,这正是我正在使用的代码,其中有这个问题。该代码对发生的每个动作都有 cmets,因此应该很容易理解。
static File dFileD = new File("date.txt");
static String date = "";
public static void main(String[] args) throws ParseException {
Timer tickTock = new Timer();
TimerTask tickTockTask = new TimerTask(){
public void run(){
try {
timFunRun(); //Timer calls method to start the countdown
} catch (ParseException e) {
e.printStackTrace();
}
}
};
tickTock.schedule(tickTockTask, 1000, 1000);
}
static void timFunRun() throws ParseException {
if (!dFileD.exists()){ //if it doesn't exist, first part
//Get current date and time
Calendar startDat = Calendar.getInstance();
System.out.println("Current date: " + startDat.getTime());
//Get that current date and time and then add 1 day
Calendar todAdd = Calendar.getInstance();
todAdd.add(Calendar.DATE, 1);
System.out.println("Date in 1 day: " + todAdd.getTime());
//Create a format for sending date to text file
SimpleDateFormat formDat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
String formatted = formDat.format(todAdd.getTime());
System.out.println("Formatted: " + formatted);
try{
PrintWriter dW = new PrintWriter("date.txt");
dW.println(formatted);
dW.close();
} catch (IOException e) {
}
System.out.println(formDat.parse(formatted));
} else { //if it does exist, second part
//Get current date and time
Calendar currentDeT = Calendar.getInstance();
System.out.println("Current date: " + currentDeT.getTime());
SimpleDateFormat formDat2 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
Date dateFroText = null; //Get the "goal" date
try {
Scanner dateRead = new Scanner(dFileD);
while (dateRead.hasNextLine()) {
date = dateRead.nextLine();
dateFroText = formDat2.parse(date);
System.out.println("Date from text new format: " + dateFroText);
}
dateRead.close();
} catch (Exception e) {
System.out.println("Error!");
}
if (dateFroText != null){ //method to compare the current date and the goal date
Calendar dateFromTxtCal = Calendar.getInstance();
dateFromTxtCal.setTime(dateFroText);
int yearDiff = dateFromTxtCal.get(Calendar.YEAR) - currentDeT.get(Calendar.YEAR);
int dayDiff = ((yearDiff*365) + dateFromTxtCal.get(Calendar.DAY_OF_YEAR)) - currentDeT.get(Calendar.DAY_OF_YEAR);
dayDiff--;
int hourDiffer = dateFromTxtCal.get(Calendar.HOUR_OF_DAY)+23 - currentDeT.get(Calendar.HOUR_OF_DAY);
int minuDiff = dateFromTxtCal.get(Calendar.MINUTE)+60 - currentDeT.get(Calendar.MINUTE);
int secoDiff = dateFromTxtCal.get(Calendar.SECOND)+60 - currentDeT.get(Calendar.SECOND);
System.out.println(dayDiff + " days " + hourDiffer + " hours " + minuDiff +" minutes " + secoDiff + "seconds remaining");
}
}
}
【问题讨论】:
-
首先使用 Java 8 或 JodaTime 中提供的较新的日期/时间 API,如果您使用的是早期版本的 Java,它们能够计算两个时间点之间的持续时间
-
好主意,@MadProgrammer。我不想破译问题中代码末尾附近发生的时间算术。看起来很复杂。只是,我听说如果你不能使用 Java 8 并且你愿意依赖第三方库,你应该更喜欢 Basil Bourque’s answer 中提到的 ThreeTen Backport 而不是 JodaTime。
-
@OleV.V.这不是一个坏建议,我更喜欢 JodaTime,因为它是一个更丰富的 API,但这就是我
标签: java date timer calendar countdown