gpt4 book ai didi

java - 减去两个 java.sql.timestamp 给出错误的年份

转载 作者:行者123 更新时间:2023-12-01 12:39:24 24 4
gpt4 key购买 nike

我正在尝试从sql获取时间戳,需要计算两个时间戳之间的差异。

//startDate = 2014-07-10 16:07:00.0
//endDate = 2014-07-11 04:07:00.0
//END_DATE = 2014-07-18 08:07:00.0

private Timestamp calculateWflTime(String vehicleNum, Timestamp startDate,
Timestamp endDate) {
Timestamp t1 = new Timestamp (END_DATE.getTime()); //2014-07-18 08:07:00.0
Timestamp t2 = new Timestamp (startDate.getTime()); //2014-07-10 16:07:00.0
Timestamp wflTime = null;
long diff=0;
if(VEH_NUM == vehicleNum){
diff = t1.getTime()-t2.getTime();//diff(END_DATE,startDate);
}
wflTime = new Timestamp( Math.abs(diff/(1000*60*60*24)));
return wflTime; //1970-01-01 05:30:00.007
}

最佳答案

表达式

new Timestamp( Math.abs(diff/(1000*60*60*24)));
从领域的角度来看,

在语义上是错误的。为什么?您尝试将一段时间(实际上是以毫秒为单位的持续时间,未固定在时间轴上)转换为此处固定的时间点,从 UNIX 纪元 (1970-01-01) 开始计数。这就像用几何术语将长度转换为点。

两个时间戳之间的差异不应该是新的时间戳,而只是持续时间(这里是您的差异变量以毫秒为单位)。这取决于您想要如何将其标准化为年和月。

OP回答后更新

清洁 Joda 解决方案:

public static void main(String... args) {
Timestamp t1 = new Timestamp(0);
Timestamp t2 = new Timestamp(86400000 + 7261000);
System.out.println(getDurationJoda(t1, t2));
// output: 1 day, 2 hours, 1 minute, 1 second.
}

public static String getDurationJoda(Timestamp start, Timestamp end) {
LocalDateTime ldtStart = new LocalDateTime(start);
LocalDateTime ldtEnd = new LocalDateTime(end);

Period p = new Period(ldtStart, ldtEnd, PeriodType.dayTime());

PeriodFormatter fmt =
new PeriodFormatterBuilder()
.appendDays().appendSuffix(" day, ", " days, ")
.appendHours().appendSuffix(" hour, ", " hours, ")
.appendMinutes().appendSuffix(" minute, ", " minutes, ")
.appendSeconds().appendSuffix(" second.", " seconds.").toFormatter();
return fmt.print(p);
}

Time4J 解决方案

此外,您可以使用我的库 Time4J 来替代此方案。其中包含一个可本地化的 PrettyTime 类,用于自版本 1.2 以来的持续时间格式化:

private static final IsoUnit DAYS = CalendarUnit.DAYS;
private static final IsoUnit HOURS = ClockUnit.HOURS;
private static final IsoUnit MINUTES = ClockUnit.MINUTES;
private static final IsoUnit SECONDS = ClockUnit.SECONDS;

public static void main(String... args) {
Timestamp t1 = new Timestamp(0);
Timestamp t2 = new Timestamp(86400000 + 7261000);
System.out.println(getDurationTime4J(t1, t2));
// output: 1 day, 2 hours, 1 minute, and 1 second
}

public static String getDurationTime4J(Timestamp start, Timestamp end) {
PlainTimestamp startTS = TemporalTypes.SQL_TIMESTAMP.transform(start);
PlainTimestamp endTS = TemporalTypes.SQL_TIMESTAMP.transform(end);

Duration<?> duration =
Duration.in(DAYS, HOURS, MINUTES, SECONDS).between(startTS, endTS);
return PrettyTime.of(Locale.ENGLISH).print(duration, TextWidth.WIDE);
}

最后但并非最不重要的一点是,在格式化持续时间之前尝试评估字符串条件,并使用 equals() 而不是 ==,例如:

if (VEH_NUM.equals(vehicleNum)) {
// call getDuration(..., ...)
} else {
// return zero duration string
}

关于java - 减去两个 java.sql.timestamp 给出错误的年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25260478/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com