为了账号安全,请及时绑定邮箱和手机立即绑定

从特定时间更新日期的每日计数器

从特定时间更新日期的每日计数器

慕哥9229398 2022-05-25 09:50:57
我有两个日期,说“2018-01-01”开始日期和“2018-01-31”结束日期我想以某种方式制定我的逻辑,每天我的开始日期都会增加,直到达到 2018-02-28。下面是我试过的代码片段。如何解决此问题,因为开始日期应每天更改。public class myclass {    public static void main(String a[]) {        try {            String dt = "2008-01-01";  // Start date            String dt1 = "2008-01-31";  // End date            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");            Calendar c = Calendar.getInstance();            c.setTime(sdf.parse(dt));            c.add(Calendar.DATE, 1);  // number of days to add            dt = sdf.format(c.getTime());  //            System.out.println(dt);        }catch (Exception ex) {            ex.printStackTrace();        }    }}PS:此执行是实时的,在调度程序中每天运行并检查给定日期的结局日期。还剩多少天。谢谢
查看完整描述

3 回答

?
萧十郎

TA贡献1815条经验 获得超13个赞

Runnable

将您需要完成的工作定义为Runnable.


然后使用 aScheduled ExecutorService每分钟左右运行一次,检查当前日期与目标日期。如果倒计时已增加,请更新 GUI 中的显示。如果没有,什么也不做,Runnable再过一分钟再运行。


请参阅有关执行程序的 Oracle 教程。


ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();  // Use a single thread, as we need this to run sequentially, not in parallel.

ses.scheduleWithFixedDelay( r , 0L , 1L , TimeUnit.MINUTES ); // Pass the `Runnable`, an initial delay, a count to wait between runs, and the unit of time for those two other number arguments.

Runnable每次安排一个新的而不是自动重复会更有效,因此将延迟设置为计算出的时间量,直到下一个午夜。这样,执行者会整天睡觉,而不是每分钟都在运行。但是,如果用户的时钟更新到显着不同的当前时间,或者用户当前的默认时区发生变化(如果您依赖于默认时区而不是明确设置),那么这种方法可能会出现问题。鉴于这Runnable需要做的事情很少(只需检查当前日期和计算剩余天数),没有实际理由不让它每分钟或每两分钟运行一次(但您对用户的最新更新的最小容忍度是多长时间 - 您的业务政策应用程序的管理)。


LocalDate

该类LocalDate表示没有时间、没有时区或从 UTC 偏移的仅日期值。


时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,法国巴黎午夜过后几分钟是新的一天,而魁北克蒙特利尔仍然是“昨天” 。


如果未指定时区,JVM 会隐式应用其当前默认时区。该默认值可能会在运行时(!)期间随时更改,因此您的结果可能会有所不同。最好将您想要/预期的时区明确指定为参数。


以、或等格式指定适当的时区名称。永远不要使用 2-4 个字母的缩写,例如或因为它们不是真正的时区,不是标准化的,甚至不是唯一的(!)。Continent/RegionAmerica/MontrealAfrica/CasablancaPacific/AucklandESTIST


ZoneId z = ZoneId.of( "Asia/Kolkata" );  // Or ZoneId.systemDefault() to rely on the JVM’s current default time zone.

LocalDate today = LocalDate.now( z );

例子

这是完整的例子。有关更多信息,请搜索堆栈溢出。的使用ScheduledExecutorService已经被介绍过很多次了。


package work.basil.example;


import java.time.Instant;

import java.time.LocalDate;

import java.time.Month;

import java.time.ZoneId;

import java.time.temporal.ChronoUnit;

import java.util.Objects;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;


public class DailyCountdown implements Runnable {

    private LocalDate dueDate;

    private Long daysRemaining;


    public DailyCountdown ( LocalDate dueDate ) {

        this.dueDate = dueDate;

    }


    @Override

    public void run () {

        try {

            System.out.println( "DEBUG - Running the DailyCountdown::run method at " + Instant.now() );

            ZoneId z = ZoneId.of( "America/Montreal" );  // Or ZoneId.systemDefault() to rely on the JVM’s current default time zone.

            LocalDate today = LocalDate.now( z );

            Long count = ChronoUnit.DAYS.between( today , this.dueDate );

            if ( Objects.isNull( this.daysRemaining ) ) {

                this.daysRemaining = ( count - 1 );

            }

            if ( this.daysRemaining.equals( count ) ) {

                // Do nothing.

            } else {

                // … Schedule on another thread for the GUI to update with the new number.

                this.daysRemaining = count;

            }

        } catch ( Exception e ) {

            // Log this unexpected exception, and notify sysadmin.

            // Any uncaught exception reaching the scheduled executor service would have caused it to silently halt any further scheduling.

        }

    }


    public static void main ( String[] args ) {

        // Put this code where ever appropriate, when setting up your GUI after the app launches.

        Runnable r = new DailyCountdown( LocalDate.of( 2018 , Month.FEBRUARY , 15 ) );

        ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();

        ses.scheduleWithFixedDelay( r , 0L , 1L , TimeUnit.MINUTES );


        // Be sure to gracefully shutdown the ScheduledExecutorService when your program is stopping. Otherwise, the executor may continue running indefinitely on the background thread.

        try {

            Thread.sleep( TimeUnit.MINUTES.toMillis( 7 ) ); // Sleep 7 minutes to let the background thread do its thing.

        } catch ( InterruptedException e ) {

            System.out.println( "The `main` thread was woken early from sleep." );

        }

        ses.shutdown();

        System.out.println( "App is exiting at " + Instant.now() ) ;

    }

}


查看完整回答
反对 回复 2022-05-25
?
白衣染霜花

TA贡献1796条经验 获得超10个赞

您可以java.util.Timer为此使用类。


在下面的程序中,我采取了不同的方法。我没有使用开始日期。我只需每天获取当前日期,然后对照目标日期(在本例中为“2019-02-10”)检查它。看看这是否适合您的要求。


(FIVE_SECONDS_IN_MILLISECONDS用于测试程序。)


import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Timer;

import java.util.TimerTask;


public class Scheduler {

  private static final long ONE_DAY_IN_MILLISECONDS = 1000 * 60 * 60 * 24;

  private static final long FIVE_SECONDS_IN_MILLISECONDS = 1000 * 5;


  public static void main(String[] args) {

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");


    TimerTask timerTask = new TimerTask() {

      @Override

      public void run() {

        Calendar c = Calendar.getInstance();

        String dateString = sdf.format(c.getTime());

        System.out.println(dateString);

        if (dateString.equals("2019-02-10")) {

          System.out.println("Date reached!");

        }

      }

    };


    Timer timer = new Timer();

    timer.schedule(timerTask, 0, ONE_DAY_IN_MILLISECONDS/*FIVE_SECONDS_IN_MILLISECONDS*/);

  }

}


查看完整回答
反对 回复 2022-05-25
?
婷婷同学_

TA贡献1844条经验 获得超8个赞

Java 8的time-API,你可以轻松实现。


for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1))

{

    ...

}


查看完整回答
反对 回复 2022-05-25
  • 3 回答
  • 0 关注
  • 124 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号