用java代码如何实现2个timestamp类型的时间如何计算相隔多少天呢

怒放de生命2010 2015-10-22 10:10:18
2个timestamp类型的时间如何计算相隔多少天呢
我想根据这个天数作为长度来循环。讲每天的记录插入数据库,求大神指导
...全文
1207 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
JPF1024 2015-10-23
  • 打赏
  • 举报
回复
官方API,java.util.Calendar类已经提供了相应的实现.

add

public abstract void add(int field,
                         int amount)
根据日历的规则,为给定的日历字段添加或减去指定的时间量。例如,要从当前日历时间减去 5 天,可以通过调用以下方法做到这一点:
add(Calendar.DAY_OF_MONTH, -5)。

参数:
field - 日历字段。
amount - 为字段添加的日期或时间量。
另请参见:
roll(int,int), set(int,int)
roll

public abstract void roll(int field,
                          boolean up)
在给定的时间字段上添加或减去(上/下)单个时间单元,不更改更大的字段。例如,要将当前日期向上滚动一天,可以通过调用以下方法做到这一点:
roll(Calendar.DATE, true)。在 year 或 Calendar.YEAR 字段上滚动时,将在从 1 到调用 getMaximum(Calendar.YEAR) 的返回值之间的范围内滚动 year 值。在 month 或 Calendar.MONTH 字段上滚动时,其他字段(如 date)可能发生冲突,因此需要更改它们。例如,在日期 01/31/96 上滚动 month 将产生 02/29/96 的日期。在 hour-in-day 或 Calendar.HOUR_OF_DAY 字段上滚动时,小时值会在 0 到 23 之间的范围内滚动,它是基于 0 的。

参数:
field - 时间字段。
up - 指示指定时间字段的值是向上滚动还是向下滚动。如果向上滚动,则使用 true,否则使用 false。
另请参见:
add(int,int), set(int,int)
roll

public void roll(int field,
                 int amount)
向指定日历字段添加指定(有符号的)时间量,不更改更大的字段。负的时间量意味着向下滚动。
注:Calendar 上的此默认实现只是重复地调用滚动一个单元的 roll() 版本。这可能并非总是正确的。例如,如果 DAY_OF_MONTH 字段为 31,则在 February 的范围内滚动会将它设置为 28。此函数的 GregorianCalendar 版本会小心地处理这个问题。其他子类还应该提供此函数的重写版本,以正确实现该功能。

参数:
field - 日历字段。
amount - 要添加到日历 field 中的有符号时间量。
从以下版本开始:
1.2
另请参见:
roll(int,boolean), add(int,int), set(int,int)

Java 1.8 里面新增的类同样可以实现:

//添加
public LocalDateTime plus(long amountToAdd,
                          TemporalUnit unit)
Returns a copy of this date-time with the specified amount added.
This returns a LocalDateTime, based on this one, with the amount in terms of the unit added. If it is not possible to add the amount, because the unit is not supported or for some other reason, an exception is thrown.

If the field is a ChronoUnit then the addition is implemented here. Date units are added as per LocalDate.plus(long, TemporalUnit). Time units are added as per LocalTime.plus(long, TemporalUnit) with any overflow in days added equivalent to using plusDays(long).

If the field is not a ChronoUnit, then the result of this method is obtained by invoking TemporalUnit.addTo(Temporal, long) passing this as the argument. In this case, the unit determines whether and how to perform the addition.

This instance is immutable and unaffected by this method call.

Specified by:
plus in interface ChronoLocalDateTime<LocalDate>
Specified by:
plus in interface Temporal
Parameters:
amountToAdd - the amount of the unit to add to the result, may be negative
unit - the unit of the amount to add, not null
Returns:
a LocalDateTime based on this date-time with the specified amount added, not null
Throws:
DateTimeException - if the addition cannot be made
UnsupportedTemporalTypeException - if the unit is not supported
ArithmeticException - if numeric overflow occurs

//减法
public LocalDateTime minus(long amountToSubtract,
                           TemporalUnit unit)
Returns a copy of this date-time with the specified amount subtracted.
This returns a LocalDateTime, based on this one, with the amount in terms of the unit subtracted. If it is not possible to subtract the amount, because the unit is not supported or for some other reason, an exception is thrown.

This method is equivalent to plus(long, TemporalUnit) with the amount negated. See that method for a full description of how addition, and thus subtraction, works.

This instance is immutable and unaffected by this method call.

Specified by:
minus in interface ChronoLocalDateTime<LocalDate>
Specified by:
minus in interface Temporal
Parameters:
amountToSubtract - the amount of the unit to subtract from the result, may be negative
unit - the unit of the amount to subtract, not null
Returns:
a LocalDateTime based on this date-time with the specified amount subtracted, not null
Throws:
DateTimeException - if the subtraction cannot be made
UnsupportedTemporalTypeException - if the unit is not supported
ArithmeticException - if numeric overflow occurs
skgary 2015-10-22
  • 打赏
  • 举报
回复
引用 楼主 baidu_21349635 的回复:
2个timestamp类型的时间如何计算相隔多少天呢 我想根据这个天数作为长度来循环。讲每天的记录插入数据库,求大神指导
一天=86400秒,86400 000毫秒 两个timestamp的差值除一下就好了。
无心流泪 2015-10-22
  • 打赏
  • 举报
回复
引用 1 楼 skgary 的回复:
[quote=引用 楼主 baidu_21349635 的回复:] 2个timestamp类型的时间如何计算相隔多少天呢 我想根据这个天数作为长度来循环。讲每天的记录插入数据库,求大神指导
一天=86400秒,86400 000毫秒 两个timestamp的差值除一下就好了。[/quote]赞一个

62,616

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧