JAVA中如重置时间_在Java中重置时间戳的时间部分

下面是一个使用Joda时间和支持时区的解决方案。

因此,您将获得当前日期和时间(到

currentDate

currentTime

)或者你通知的日期和时间

informedDate

informedTime

)在jvm中当前配置的时区中。

下面的代码还通知通知通知日期/时间是否在将来(变量

schedulable

)

请注意,Joda时间不支持

跳跃秒

. 所以,你可以偏离真实值26或27秒。这可能只有在未来50年才能解决,那时累积误差将接近1分钟,人们将开始关心它。

/**

* This class splits the current date/time (now!) and an informed date/time into their components:

*

*

schedulable: if the informed date/time is in the present (now!) or in future.

*

informedDate: the date (only) part of the informed date/time

*

informedTime: the time (only) part of the informed date/time

*

currentDate: the date (only) part of the current date/time (now!)

*

currentTime: the time (only) part of the current date/time (now!)

*

*/

public class ScheduleDateTime {

public final boolean schedulable;

public final long millis;

public final java.util.Date informedDate;

public final java.util.Date informedTime;

public final java.util.Date currentDate;

public final java.util.Date currentTime;

public ScheduleDateTime(long millis) {

final long now = System.currentTimeMillis();

this.schedulable = (millis > -1L) && (millis >= now);

final TimeZoneUtils tz = new TimeZoneUtils();

final java.util.Date dmillis = new java.util.Date( (millis > -1L) ? millis : now );

final java.time.ZonedDateTime zdtmillis = java.time.ZonedDateTime.ofInstant(dmillis.toInstant(), java.time.ZoneId.systemDefault());

final java.util.Date zdmillis = java.util.Date.from(tz.tzdate(zdtmillis));

final java.util.Date ztmillis = new java.util.Date(tz.tztime(zdtmillis));

final java.util.Date dnow = new java.util.Date(now);

final java.time.ZonedDateTime zdtnow = java.time.ZonedDateTime.ofInstant(dnow.toInstant(), java.time.ZoneId.systemDefault());

final java.util.Date zdnow = java.util.Date.from(tz.tzdate(zdtnow));

final java.util.Date ztnow = new java.util.Date(tz.tztime(zdtnow));

this.millis = millis;

this.informedDate = zdmillis;

this.informedTime = ztmillis;

this.currentDate = zdnow;

this.currentTime = ztnow;

}

}

public class TimeZoneUtils {

public java.time.Instant tzdate() {

final java.time.ZonedDateTime zdtime = java.time.ZonedDateTime.now();

return tzdate(zdtime);

}

public java.time.Instant tzdate(java.time.ZonedDateTime zdtime) {

final java.time.ZonedDateTime zddate = zdtime.truncatedTo(java.time.temporal.ChronoUnit.DAYS);

final java.time.Instant instant = zddate.toInstant();

return instant;

}

public long tztime() {

final java.time.ZonedDateTime zdtime = java.time.ZonedDateTime.now();

return tztime(zdtime);

}

public long tztime(java.time.ZonedDateTime zdtime) {

final java.time.ZonedDateTime zddate = zdtime.truncatedTo(java.time.temporal.ChronoUnit.DAYS);

final long millis = zddate.until(zdtime, java.time.temporal.ChronoUnit.MILLIS);

return millis;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值