圣诞节倒计时

时间:2013-11-28 02:20:02

标签: java long-integer countdown

我正试图在java中进行圣诞倒计时,但我似乎无法弄清楚为什么时间关闭。这比实际时间输出大约12小时。

这是我的代码:

long now = System.currentTimeMillis();
long christmas = 1387929600000L;
long untilChristmas = christmas - now;

DateFormat df = new SimpleDateFormat("dd-kk-mm-ss");
Date date = new Date(untilChristmas);

System.out.println(df.format(date));

1 个答案:

答案 0 :(得分:1)

您不应该对日期进行硬编码,您应该使用Calendar进行计算:

    long now = System.currentTimeMillis();
    Calendar christmas = Calendar.getInstance();
    christmas.set(Calendar.MONTH, Calendar.DECEMBER);
    christmas.set(Calendar.DAY_OF_MONTH, 25);
    long untilChristmas = christmas.getTimeInMillis() - now;

    DateFormat df = new SimpleDateFormat("dd-kk-mm-ss");
    Date date = new Date(untilChristmas);

    System.out.println(df.format(date));