Java,计算两个日期之间的天数 [英] Java, Calculate the number of days between two dates

查看:120
本文介绍了Java,计算两个日期之间的天数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

计算两个Java日期实例之间的差异

在Java中,我想计算两个日期之间的天数。

In Java, I want to calculate the number of days between two dates.

在我的数据库中,它们被存储为 DATE datatype,但在我的代码中,它们是字符串。

In my database they are stored as a DATE datatype, but in my code they are strings.

我想计算这两个字符串之间的天数。

I want to calculate the no of days between those two strings.

推荐答案

首先,您应该只将作为字符串处理。大多数情况下,您应该使用数据类型实际描述您正在使用的数据。

Well to start with, you should only deal with them as strings when you have to. Most of the time you should work with them in a data type which actually describes the data you're working with.

我建议您使用 Joda Time ,这是一个比更好的API日期 / 日历。听起来你应该使用 在这种情况下,LocalDate 键入。然后,您可以使用:

I would recommend that you use Joda Time, which is a much better API than Date/Calendar. It sounds like you should use the LocalDate type in this case. You can then use:

int days = Days.daysBetween(date1, date2).getDays();

这篇关于Java,计算两个日期之间的天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆