直到圣诞节应用程序Java值不会累计

时间:2014-12-07 00:53:26

标签: java

我正在自学Java,我正在研究这个应用程序,它将计算直到圣诞节的日子。今天和圣诞节的HOUR_OF_DAYMONTH对象的SECONDGregorianCalendarDate值都设置为零。通过调试,我可以看到天数差异的变量,以毫秒为单位,它给出的时间为1641599724毫秒,为18.99999天,但无论我尝试什么,它都不会达到19!

我已经尝试过Math.ceil方法尝试并将其向上舍入但我无法将其等同于19。

FIRST:DataUtils类存储/修改用户的输入

package chapter13datesstrings;


import java.util.*;

public class DateUtils {

static final int MILLS_IN_DAY = 24 * 60 * 60 * 1000;

public static Date getCurrentDate(){
    GregorianCalendar currentDate = new GregorianCalendar();
    currentDate.set(Calendar.HOUR_OF_DAY, 0);
    currentDate.set(Calendar.MINUTE, 0);
    currentDate.set(Calendar.SECOND, 0);
    return currentDate.getTime();              
}

public static Date createDate(int year, int month, int day){
    GregorianCalendar date = new GregorianCalendar(year, month, day);
    return date.getTime();
}

public static Date stripTime(Date date){
    GregorianCalendar noTimeDate = new GregorianCalendar();
    noTimeDate.setTime(date);
    noTimeDate.set(Calendar.HOUR, 0);
    noTimeDate.set(Calendar.MINUTE,0);
    noTimeDate.set(Calendar.SECOND, 0);
    return noTimeDate.getTime();
}

public static double daysDiff(Date date1, Date date2){

    date1 = stripTime(date1);
    date2 = stripTime(date2);

   long longDate1 = date1.getTime();
   long longDate2 = date2.getTime();

   long longDiff = longDate2 - longDate1;

   return (int) (Math.ceil(longDiff / MILLS_IN_DAY));
}    


}

SECOND:DateUtilExample类为DataUtils类提供输入

package chapter13datesstrings;

import java.util.*;
import java.text.*;

public class DateUtilExample {


public void thisIsCode(){

GregorianCalendar currentGC = new GregorianCalendar();
int currentYear = currentGC.get(Calendar.YEAR); //sets current year

Date currentDate = DateUtils.getCurrentDate(); //create current date object
Date christmas = DateUtils.createDate(currentYear, Calendar.DECEMBER, 25); //set christmas date
int daysToChristmas = DateUtils.daysDiff(currentDate, christmas); // days until christmas

DateFormat date = DateFormat.getDateInstance(Calendar.LONG);
String formattedToday = date.format(currentDate);

/**** Output Items *****/
System.out.println("Today is " + formattedToday);
System.out.println("Number of Days 'Till Xmas: " + daysToChristmas + " days");
}


}

MAIN METHOD CLASS

package chapter13datesstrings;


import java.util.Date;
import java.text.DateFormat;


public class Chapter13DatesStrings {

public static void theDates(){
    DateUtilExample dateUtilExample = new DateUtilExample();
    dateUtilExample.thisIsCode();
}

public static void main(String[] args) { 
 theDates();
}

}

2 个答案:

答案 0 :(得分:2)

在Java中分割两个整数时,结果会自动向下舍入("截断")。

表达式longDiff / MILLS_IN_DAY不等于18.99999,它等于18。 Java基本上计算18.99999然后抛出小数点之后的所有内容,之前你可以用它做任何事情。在此时,ceil的总结不会得到帮助,因为您最终只计算了ceil(18) 18

对此的一个解决方案是在分割数字之前将数字转换为doubledouble是浮点值,因此除以它们不会使结果向下舍入。要使用双打,请替换

longDiff / MILLS_IN_DAY

((double)longDiff) / ((double)MILLS_IN_DAY)

另一种解决方案可能是计算效率更高但更不优雅,只需在结果中添加1即可。这并不是严格等同的,因为当你在午夜的毫秒之内时,结果将比你想象的要多一天,但这基本上是不明显的。要采用这种方法,您将替换以下行:

return (int) (Math.ceil(longDiff / MILLS_IN_DAY));

return longDiff / MILLS_IN_DAY + 1;

答案 1 :(得分:1)

SelectricSimian所说的, 这可以使用java提供的Calendar API在几行中完成。 要简单地获取当前时间和给定日期之间的日差,您可以使用:

public static void main(String[] args) {
    System.out.println(getDaysUntil(Calendar.DECEMBER, 25) + " day(s).");
    // If the date is December 25th, this will output "365 day(s)"
    // If the date is December 24th, this will output "1 day(s)"
}

public static int getDaysUntil(int month, int day) {
    /**
     * First get a properly formatted calendar representing right now. This
     * should include leap years and local. With this calendar, we get the
     * day of the year.
     */
    Calendar calendar = Calendar.getInstance();
    int today = calendar.get(Calendar.DAY_OF_YEAR);

    /**
     * Now change the day and month of the current calendar to the given day
     * and month.
     */
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.DAY_OF_MONTH, day);
    int desiredDay = calendar.get(Calendar.DAY_OF_YEAR);

    /**
     * Then we just get the difference between now and then.
     */
    int difference = desiredDay - today;

    /**
     * If the desiredDay has passed already, or it's currently the
     * desiredDay, we need to recalculate the difference.
     */
    if (difference <= 0) {
        /**
         * We start by getting the days until the end of the year.
         */
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        int daysUntilEnd = calendar.get(Calendar.DAY_OF_YEAR) - today;

        /**
         * Then, move the calendar forward a year and get the day of year
         * for the desired day again. We recalculate the number of days just
         * in case next year is a leap year.
         */
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, day);
        desiredDay = calendar.get(Calendar.DAY_OF_YEAR);

        /**
         * Finally, just add daysUntilEnd and desiredDay to get the updated
         * difference.
         */
        difference = daysUntilEnd + desiredDay;
    }

    return difference;
}