如何在Java中对日期进行算术运算?

发布于 2024-11-17 19:35:36 字数 355 浏览 2 评论 0原文

可能的重复:
如何计算java中的时差?

实际上我想减去两个Java 中的日期。我尝试了很多但无法成功。所以请任何人帮助我。

Date dtStartDate=pCycMan.getStartDate();

Date dtEndDate=pCycMan.getEndDate();

如何减去这两个日期?

Possible Duplicate:
How to calculate time difference in java?

Actually I want to subtract two dates in Java. I tried a lot but can't succeed. So anyone please help me.

Date dtStartDate=pCycMan.getStartDate();

Date dtEndDate=pCycMan.getEndDate();

How can I subtract these two Dates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一江春梦 2024-11-24 19:35:36
long msDiff = dtEndDate.getTime() - dtStartDate.getTime()

msDiff 现在保存两个日期之间的毫秒数差异。请随意使用除法和取模运算符来转换为其他单位。

long msDiff = dtEndDate.getTime() - dtStartDate.getTime()

msDiff now holds the number of milliseconds difference between the two dates. Feel free to use the division and mod operators to convert to other units.

从来不烧饼 2024-11-24 19:35:36

如何减去这两个日期?

您可以像这样得到以毫秒为单位的差异:

dtEndDate.getTime() - dtStartDate.getTime()

(getTime 返回自 1970 年 1 月 1 日以来的毫秒数, 00:00:00 GMT。)

但是,对于这种类型的日期算术,Joda 时间库,它具有适当的持续时间类(Duration)可能是更好的选择。

how can i subtract these two Dates?

You can get the difference in milliseconds like this:

dtEndDate.getTime() - dtStartDate.getTime()

(getTime returns the number of milliseconds since January 1, 1970, 00:00:00 GMT.)

However, for this type of date arithmetics the Joda time library, which has proper classes for time durations (Duration) is probably a better option.

依 靠 2024-11-24 19:35:36

要减去日期,您可以将它们作为 longs getTime() 获取,然后减去返回的值。

To substract to dates you can get them both as longs getTime() and subtract the values that are returned.

罪#恶を代价 2024-11-24 19:35:36

根据这些日期创建两个 Calendar 对象然后使用 add() 方法(也可用于对日期进行细分)。

Create two Calendar objects from those Dates and then use the add() method (which can be used to subtrade dates too).

无戏配角 2024-11-24 19:35:36

有一个在java中处理日期的框架......
http://joda-time.sourceforge.net/userguide.html
看一下,我相信它可以帮助你

There is a framework to work with the date in java...
http://joda-time.sourceforge.net/userguide.html
take a look, i'm sure that it can help you

等风来 2024-11-24 19:35:36
long diffInMillis = dtEndDate.getTimeInMillis() -  dtStartDate.getTimeInMillis();
long diffInMillis = dtEndDate.getTimeInMillis() -  dtStartDate.getTimeInMillis();
半城柳色半声笛 2024-11-24 19:35:36

这取决于你想要什么。要获得以毫秒为单位的差异,请说dtStartDate.getTime() - dtEndDate.getTime()

要获得天、小时等的差异,请使用 Calendar 类(after、before、compareTo、roll 等方法可能会有所帮助)。

It depends what do you want. To get difference in milliseconds say dtStartDate.getTime() - dtEndDate.getTime().

To get difference in days, hours etc use Calendar class (methods like after, before, compareTo, roll may help).

贪恋 2024-11-24 19:35:36

使用 Calendar 类。

用法:

Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
int date = c.get(Calendar.YEAR); //int represents the year of the date
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // int represents dayOfWeek as defined in link.

Use the Calendar class.

Usage:

Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
int date = c.get(Calendar.YEAR); //int represents the year of the date
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK); // int represents dayOfWeek as defined in link.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文