我想获取一个 应用程序里的随时变量数据,应该怎么获取呢? 例如获取时钟数据
我这里有一个关于倒计时的实例,读者可以参考下:
/**
 * 输出2022年冬奥会倒计时(暂定冬奥会举办时间为2022年2月2日)
 */
public class OlympicWinterGames { //创建OlympicWinterGames类
  public static void main(String[] args) {
    System.out.println("——————————冬奥会倒计时——————————\n"); 
    Date date = new Date(); //实例化Date
    //创建SimpleDateFormat对象,制定目标格式
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    // 调用format方法,格式化时间,转换为指定方法
    String today = simpleDateFormat.format(date);
    //输出当前日期
    System.out.println("今天是" + today);
    //计算“自 1970 年 1 月 1 日 00:00:00 至当前时间所经过的毫秒数
    long time1 = date.getTime();
    //使用默认时区和语言环境获得一个日历calendar
    Calendar calendar = Calendar.getInstance();
    //设置日历calendar中的 YEAR、MONTH 和 DAY_OF_MONTH 的值
    calendar.set(2022, 2-1, 4);
    //计算“自 1970 年 1 月 1 日 00:00:00 至 2022 年 2 月 2 日所经过的毫秒数
    long time2 = calendar.getTimeInMillis();
    //计算2022 年 2 月 2 日距离当前时间的天数
    long day = (time2 - time1)/(24 * 60 * 60 * 1000);
    System.out.println("距离2022年“北京-张家口”冬奥会还有 " + day + " 天!");
  }
}