定时器倒计时为负

Timer countdown is negative

我有一个用于创建 24 小时倒计时的代码。

此代码检查 "date" 文件是否存在,如果不存在,则创建一个文件,其中包含 24 hourse/a 天的日期和时间。然后它获取当前时间并比较两者,以创建从当前日期到文档中日期的倒计时。

即使代码是 "turned off",这也可以保存计时器并检查它已经走了多远。唯一的问题是有时计时器会变为负值。就像我 运行 从一开始就没有创建 "date" 文件的代码一样,就在星期一午夜之前,让我们说星期一晚上十一点半。然后,如果我停止代码并在当前日期午夜过后再次 运行 它,那么它实际上是星期二,但在它达到实际目标计时器之前仍然缺少最多 23 小时。如果是这种情况,则倒计时剩余的时间为负数。就像它会显示“-1 天 23 小时 60 分 60 秒剩余”。但是,如果它是 运行 在星期二午夜过后从头开始,然后在同一天的 30 分钟后重新启动,则没有问题。

希望你能理解是什么问题,文字表达起来略显困难。但是我已经附上了我的全部代码,这正是我正在使用的代码,并且有这个问题。该代码对发生的每个动作都有注释,因此应该很容易理解。

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");
          }

      }

  }

避免遗留日期时间 classe

你太辛苦了。而且您使用的是麻烦的旧日期时间 classes 现在已被 java.time classes 淘汰和取代。

在 UTC 工作

此外,如果您只关心接下来的 24 小时,则不需要时区。只需使用 UTC。

Instant

Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds(最多九 (9) 位小数)。

Instant instantNow = Instant.now();

Duration

未附加到时间轴的时间跨度由 DurationPeriod classes 表示。

Duration duration = Duration.ofHours( 24 );

您可以执行日期时间数学运算,将 Duration 添加到 Instant。 java.time classes 使用不可变对象,因此此类操作的结果是一个新对象,其值基于原始对象。

Instant instantLater = instantNow.plus( duration );

ISO 8601

要将日期时间值(例如文本值)序列化,请使用标准 ISO 8601 格式。 java.time classes 在生成和解析字符串时默认使用 ISO 8601。

String output = instantNow.toString(); 

2017-04-03T03:18:48.183Z

计算剩余时间

要获得剩余时间,让java.time算一下。

Duration remaining = Duration.between( Instant.now() , instantLater );

要以标准 ISO 8601 格式报告,请调用 toStringformat for durationsPnYnMnDTnHnMnSP 标记开始(对于 Period),T 将任何年-月-日与时-分-秒分开。

String outputRemaining = remaining.toString();

PT23H34M22S

要生成更长的字符串,在Java9中为每个单位(小时、分钟、秒)调用to…Part方法。奇怪的是,java.time.Duration class 在Java 8 中省略了这些方法。您可以查看Java 9 的源代码来编写类似的代码。

或者更简单地说,操作标准字符串。删除PT。将 H 替换为 hours 等等。先做S,避免其他两个词中的复数s。不可否认,这是一种 hack,但它之所以有效,是因为字母的出现是 hours-minutes-seconds 的英文拼写。

String output = remaining.toString()
                         .replace( "PT" , "" )
                         .replace( "S" , " seconds " )
                         .replace( "H" , " hours " )
                         .replace( "M" , " minutes " ) ;

23 hours 34 minutes 22 seconds

ZonedDateTime

如果要在用户的 desired/expected 时区显示目标日期时间,请应用 ZoneId 以获得 ZonedDateTime 对象。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

要生成标准格式的字符串,请调用 toString。实际上 ZonedDateTime class 通过在方括号中附加时区名称来扩展标准。

要生成其他格式的字符串,请在 Stack Overflow 中搜索 DateTimeFormatter class.

的许多示例

转换为java.util.Date

Timer class has not yet been updated to work with the java.time types. So convert back to a Date object via new methods added to the old classes, in this case from.

java.util.Date date = java.util.Date.from( instantLater );

或者使用 Duration 对象的毫秒计数。

long milliseconds = duration.toMillis() ;

ScheduledExecutorService

仅供参考,TimerTimeTask classes 已被 Executors 框架取代。专门针对您的目的,ScheduledExecutorService。搜索 Stack Overflow 以获取许多示例和讨论。


关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

从哪里获得java.time classes?

  • Java SE 8 and SE 9 及更高版本
    • 内置。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and SE 7
  • Android
    • ThreeTenABP 项目专门为 Android 改编 ThreeTen-Backport(如上所述)。
    • 参见

ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.