gpt4 book ai didi

java - 错误计算 2 个日期与 joda 时间之间的差异天数?

转载 作者:行者123 更新时间:2023-11-29 06:19:32 25 4
gpt4 key购买 nike

早安

我开发了一个 Android 应用程序,我快疯了!几天后,我试着找出两个日期之间的天数。

我用 joda time 意识到了这一点 - 这似乎有效。

DatePicker datepicker = (DatePicker) findViewById(R.id.Picker);

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, datepicker.getYear());
cal.set(Calendar.MONTH, datepicker.getMonth());
cal.set(Calendar.DATE, datepicker.getDayOfMonth());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
long sqlDate = cal.getTimeInMillis();



Calendar calToday = Calendar.getInstance();
calToday.set(Calendar.YEAR, calToday.get(Calendar.YEAR));
calToday.set(Calendar.MONTH, calToday.get(Calendar.MONTH));
calToday.set(Calendar.DATE, calToday.get(Calendar.DATE));
calToday.set(Calendar.HOUR_OF_DAY, 0);
calToday.set(Calendar.MINUTE, 0);
calToday.set(Calendar.SECOND, 0);



Date date1 = new Date(
datePicker.getYear()-1900,
datePicker.getMonth(),
datePicker.getDayOfMonth()
);



Date today = new Date(
calToday.get(Calendar.YEAR)-1900,
calToday.get(Calendar.MONTH),
calToday.get(Calendar.DAY_OF_MONTH)
);



int days = Days.daysBetween(new DateTime(today), new DateTime(date1)).getDays();

我将长 sqlDate 保存在我的数据库中。这是日期选择器的日期,以毫秒为单位。

现在我想通过查询请求这个日期,并计算以毫秒为单位的日期与现在的日期之间的天数差。

我试试这个:

Calendar calToday = Calendar.getInstance();
calToday.set(Calendar.YEAR, calToday.get(Calendar.YEAR));
calToday.set(Calendar.MONTH, calToday.get(Calendar.MONTH));
calToday.set(Calendar.DATE, calToday.get(Calendar.DATE));
calToday.set(Calendar.HOUR_OF_DAY, 0);
calToday.set(Calendar.MINUTE, 0);
calToday.set(Calendar.SECOND, 0);
long now = calToday.getTimeInMillis();
long DiffDays = (sqlDate - now) / (24 * 60 * 60 * 1000);


Log.e("-->", ""+sqlDate );
Log.e("-->", ""+now);
Log.e("-->", ""+(sqlDate - now));
Log.e("-->", ""+DiffDays);
Log.e("-->", "====================");

记录结果:

12-02 07:19:09.931 26600-26600/? E/-->: 1451606400515
12-02 07:19:09.931 26600-26600/? E/-->: 1449014400938
12-02 07:19:09.931 26600-26600/? E/-->: 2591999577
12-02 07:19:09.931 26600-26600/? E/-->: 29
12-02 07:19:09.931 26600-26600/? E/-->: ====================
12-02 07:19:09.931 26600-26600/? E/-->: 1449187200127
12-02 07:19:09.931 26600-26600/? E/-->: 1449014400942
12-02 07:19:09.931 26600-26600/? E/-->: 172799185
12-02 07:19:09.931 26600-26600/? E/-->: 1
12-02 07:19:09.931 26600-26600/? E/-->: ====================

但这是错误的。例如:

这是 SqlDate 的结果 - 现在 = 2591999577(毫秒)

2591999577/(24 * 60 * 60 * 1000) = 29,9999(30 天)不是 29 天

为什么我得到一个小数结果 - 我每次都从 00:00:00 到 00:00:00 计算

我希望你能帮助我:'(

最佳答案

你没有将毫秒归零 :)

尝试:

    Calendar calToday = Calendar.getInstance();
calToday.set(Calendar.YEAR, calToday.get(Calendar.YEAR));
calToday.set(Calendar.MONTH, calToday.get(Calendar.MONTH));
calToday.set(Calendar.DATE, calToday.get(Calendar.DATE));
calToday.set(Calendar.HOUR_OF_DAY, 0);
calToday.set(Calendar.MINUTE, 0);
calToday.set(Calendar.SECOND, 0);
calToday.set(Calendar.MILLISECOND, 0); // this is important
long now = calToday.getTimeInMillis();
long DiffDays = (sqlDate - now) / (24 * 60 * 60 * 1000);
Log.e("-->", ""+sqlDate );
Log.e("-->", ""+now);
Log.e("-->", ""+(sqlDate - now));
Log.e("-->", ""+DiffDays);
Log.e("-->", "====================");

关于java - 错误计算 2 个日期与 joda 时间之间的差异天数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34036488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com