Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算小时差的断言失败_Java_Date_Calendar_Assertion - Fatal编程技术网

Java 计算小时差的断言失败

Java 计算小时差的断言失败,java,date,calendar,assertion,Java,Date,Calendar,Assertion,我从日历日期中减去500小时,我的方法是以小时为单位计算差异 为什么下面的断言失败了 测试方法: public long dataFetchTotalTime(long time) { long hours = 0; Calendar ackCal = Calendar.getInstance(); long diff = ackCal.getTime().getTime() - time; hours = TimeUnit.HO

我从日历日期中减去
500小时
,我的方法是以小时为单位计算差异

为什么下面的断言失败了

测试方法:

 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.set(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
java.lang.AssertionError: expected:<500> but was:<514>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.add(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
测试用例:

 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.set(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
java.lang.AssertionError: expected:<500> but was:<514>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.add(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
错误跟踪:

 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.set(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
java.lang.AssertionError: expected:<500> but was:<514>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.add(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
java.lang.AssertionError:应为:但为:
位于org.junit.Assert.fail(Assert.java:88)
位于org.junit.Assert.failNotEquals(Assert.java:834)
位于org.junit.Assert.assertEquals(Assert.java:645)
位于org.junit.Assert.assertEquals(Assert.java:631)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
这是一个错误:

ackCal.set(Calendar.HOUR_OF_DAY, -500);
测试方法:

 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.set(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
java.lang.AssertionError: expected:<500> but was:<514>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.add(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
测试用例:

 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.set(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
java.lang.AssertionError: expected:<500> but was:<514>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:834)
    at org.junit.Assert.assertEquals(Assert.java:645)
    at org.junit.Assert.assertEquals(Assert.java:631)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 public long dataFetchTotalTime(long time) {
        long hours = 0;
        Calendar ackCal = Calendar.getInstance();
        long diff = ackCal.getTime().getTime() - time;
        hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
        return hours;
    }
@Test
public void checkFetchTimeDiff()
{
    long expected = 500;
    Client client = new Client();
    Calendar ackCal = Calendar.getInstance();
    ackCal.add(Calendar.HOUR_OF_DAY, -500);
    long actual = client.dataFetchTotalTime(ackCal.getTime().getTime());
    Assert.assertEquals(expected, actual);
}
tl;博士 看这个

五百

java.time 您正在使用可怕的日期时间类,这些类在几年前被JSR310中定义的现代java.time类所取代

ZoneDateTime
Calendar
类,实际上它常用的子类,
gregoriacalendar
,被替换为。
ZoneDateTime
类表示通过特定地区的人们使用的挂钟时间看到的一个时刻。换句话说,一个日期,一天中的一个时间,在一个时区的上下文中

您可以根据需要进行转换,但最好完全避免遗留的日期时间类。若要转换,请调用添加到旧类中的新方法

GregorianCalendar gc = ( GregorianCalendar ) myJavaUtilCalendar  ;  // Cast from the more general to the concrete.
ZonedDateTime zdt = gc.toZonedDateTime() ;  // Convert from legacy class to modern class.
要捕获当前时刻,请指定所需的时区。在任何一个特定的时刻,世界各地的日期都因地区而异

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
持续时间
做你的数学题。以小时-分-秒或年-月-天的比例表示未附加到时间线的时间跨度

Duration d = Duration.ofHours( 500 ) ;
ZonedDateTime zdtLater = zdt.plus( d ) ; 
要确定经过的时间,请使用

要以标准ISO 8601格式查看文本,请调用
toString

String output = elapsed.toString() ;
要以总小时数查看已用时间,请拨打


关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,&

要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是

该项目现已启动,建议迁移到类

您可以直接与数据库交换java.time对象。使用兼容的或更高版本。不需要字符串,也不需要
java.sql.*

从哪里获得java.time类

  • 、和更高版本-标准Java API的一部分,带有捆绑实现。
    • Java9添加了一些次要功能和修复
    • 大多数java.time功能都在中向后移植到Java6和Java7
    • 更高版本的Android捆绑包实现了java.time类
    • 对于早期的Android(