gpt4 book ai didi

java - 两个日历时间之间的差异。多一小时出结果

转载 作者:太空宇宙 更新时间:2023-11-04 14:38:44 25 4
gpt4 key购买 nike

我的日历中有 2 次。

    Calendar orarioP1 = new GregorianCalendar(2014, 3, 28, 8, 30, 00);
Calendar orarioA1 = new GregorianCalendar(2014, 3, 28, 12, 35, 06);

我实现了这个函数来获取时差,但它返回了+1小时的时差。
我需要返回正确的差异 4:05:06 但现在它返回我 5:05:06。谁能帮我吗?

public String getTimeDiff() {

SimpleDateFormat sdf = new SimpleDateFormat("h:mm:ss");
long milliSec1 = orarioP1.getTimeInMillis();
long milliSec2 = orarioA1.getTimeInMillis();

long timeDifInMilliSec;
if(milliSec1 >= milliSec2) {
timeDifInMilliSec = milliSec1 - milliSec2;
} else {
timeDifInMilliSec = milliSec2 - milliSec1;
}

long timeDifSeconds = timeDifInMilliSec / 1000;
long timeDifMinutes = timeDifInMilliSec / (60 * 1000);
long timeDifHours = timeDifInMilliSec / (60 * 60 * 1000);
long timeDifDays = timeDifInMilliSec / (24 * 60 * 60 * 1000);

Date date = new Date(timeDifSeconds);
String difference = sdf.format(date);

return difference;
}

最佳答案

尝试这样的事情

    Calendar orarioP1 = new GregorianCalendar(2014, 3, 28, 8, 30, 00);
orarioP1.setTimeZone(TimeZone.getTimeZone("GMT"));
Calendar orarioA1 = new GregorianCalendar(2014, 3, 28, 12, 35, 06);
orarioA1.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println("Difference In Hours : "
+(orarioA1.get(Calendar.HOUR_OF_DAY)-orarioP1.get(Calendar.HOUR_OF_DAY)));
System.out.println("Difference In Minutes : "
+(orarioA1.get(Calendar.MINUTE)-orarioP1.get(Calendar.MINUTE)));
System.out.println("Difference In Seconds : "
+(orarioA1.get(Calendar.SECOND)-orarioP1.get(Calendar.SECOND)));
System.out.println("Difference In MiliSeconds : "
+(orarioA1.get(Calendar.MILLISECOND)-orarioP1.get(Calendar.MILLISECOND)));

关于java - 两个日历时间之间的差异。多一小时出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25229935/

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