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

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

问题描述

可能的重复:
计算两个 Java 日期实例之间的差异

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

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

在我的数据库中,它们存储为 DATE 数据类型,但在我的代码中它们是字符串.

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,这是一个比 <代码>日期/<代码>日历.听起来您应该使用 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天全站免登陆