代码之家  ›  专栏  ›  技术社区  ›  Vivek Dhiman

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

  •  1
  • Vivek Dhiman  · 技术社区  · 6 年前

    我有两个日期,分别是“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 回复  |  直到 6 年前
        1
  •  2
  •   Basil Bourque    6 年前

    Runnable

    定义作为一个团队需要完成的工作 Runnable .

    Scheduled ExecutorService 要每隔一分钟左右运行一次,请检查当前日期与目标日期。如果倒计时已增加,则更新GUI中的显示。如果没有,什么也不做,让 可运行 再跑一分钟。

    Oracle Tutorial on Executors .

    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.
    

    安排一次新的旅行会更有效率 可运行 每次,而不是自动重复,以便将延迟设置为直到下一个午夜的计算时间量。这样,遗嘱执行人就可以整天睡觉,而不是分秒必争。但是,如果用户的时钟更新到明显不同的当前时间,或者如果用户当前的默认时区发生变化(如果您依赖默认时区而不是明确设置默认时区),这种方法可能会失控。考虑到这一点 可运行

    LocalDate

    这个 LocalDate 类表示一个仅日期的值,该值不包含一天中的时间,也不包含 time zone offset-from-UTC .

    Paris France Montréal Québec .

    如果未指定时区,JVM将隐式应用其当前默认时区。这一违约可能会发生 change at any moment 在运行时(!),因此您的结果可能会有所不同。最好将所需/预期时区明确指定为参数。

    指定一个 proper time zone name Continent/Region 例如 America/Montreal , Africa/Casablanca Pacific/Auckland . 切勿使用2-4个字母的缩写,如 EST IST 真正的时区,没有标准化,甚至不是唯一的(!)。

    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() ) ;
        }
    }
    
        2
  •  1
  •   Sanjay    6 年前

    for (LocalDate date = startDate; date.isBefore(endDate); date = date.plusDays(1))
    {
        ...
    }
    
        3
  •  1
  •   Prasad Karunagoda    6 年前

    你可以用 java.util.Timer 这门课。

    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*/);
      }
    }
    
    推荐文章