什么Java库对象封装了数字和“时间单位”?

什么Java库对象封装了数字和“时间单位”?,java,time,duration,java-8,timeunit,Java,Time,Duration,Java 8,Timeunit,我想传递一个时间编号和它所在的TimeUnit long number = some number; TimeUnit timeUnit = some arbitrary time unit 什么东西可以在Java库中的一个对象中同时保存时间和时间单位?时间单位只是枚举,它保存一些时间单位类型,如秒、毫秒、 TimeUnit不用于保持时间,但可以使用TimeUnitAPI将单位中的时间转换为另一个单位 我认为你必须创建你的对象来保存时间和它的单位 如果您使用Java8。您可以使用一些新的日期A

我想传递一个时间编号和它所在的
TimeUnit

long number = some number;
TimeUnit timeUnit = some arbitrary time unit

什么东西可以在Java库中的一个
对象中同时保存时间和时间单位?

时间单位只是枚举,它保存一些时间单位类型,如秒、毫秒、

TimeUnit不用于保持时间,但可以使用TimeUnitAPI将单位中的时间转换为另一个单位

我认为你必须创建你的对象来保存时间和它的单位

如果您使用Java8。您可以使用一些新的日期API


在Joda时代有一个,但如果您只想使用JRE包含的对象类型,则需要创建一个Java Bean(可能称为Interval)。

没有Java库对象封装一个数字和任意的
时间单位。但是,Java 8中有一个可以将自身转换为所需的任何时间单位:

java.time.Duration
存储提供的数量,然后提供与所有其他时间单位的比较和转换。例如:

// Create duration from nano time
Duration systemTime = Duration.ofNanos(System.nanoTime());

// Create duration from millis time
Duration systemTime = Duration.ofMillis(System.currentTimeMillis());
当然,如果进行加减运算或其他数学运算,精度仅与当前运算和提供的
持续时间
的精度相同

/**
* Example that tells if something has reached the required age
* TRUE IF THE current system time is older or equal to the required system time
*/
    // TRUE IF THE FILE HAS NOT WAITED ENOUGH TIME AFTER LAST CREATE/MODIFY
    private boolean isMature() {
        // it is not known whether required age is nanos or millis
        Duration requiredAge = systemTimeWhenMature();
        // so create a duration in whatever time unit you have
        // more precise = possibly better here
        Duration actualAge = Duration.ofNanos(System.nanoTime());
       // if ON or OLDER THAN REQUIRED AGE
       // actualAge - requiredAge  = balance
       // 20 - 21 = -1 (NOT MATURE)
       // 21 - 21 =  0 (OK)
       // 22 - 21 =  1 (OK)
       Duration balance = actualAge.minus(requiredAge);
       if (balance.isNegative()) {
           logger.info("Something not yet expired. Expires in {} millis.", balance.negated());
           return false;
       } else {
           return true;
       }
    }
还有更多的持续时间方法可用于转换和处理各种单位中的存储量

了解精度将如何影响计算非常重要。这表明 一般精度合同举例:

    // PICK A NUMBER THAT IS NOT THE SAME WHEN CONVERTED TO A LESSER PRECISION
    long timeNanos = 1234567891011121314L;
    long timeMillis = TimeUnit.MILLISECONDS.convert(timeNanos, TimeUnit.NANOSECONDS);

    // create from milliseconds
    Duration millisAccurate = Duration.ofMillis(timeMillis);
    Duration nanosAccurate = Duration.ofNanos(timeNanos);

    // false because of precision difference
    assertFalse(timeMillis == timeNanos);
    assertFalse(millisAccurate.equals(nanosAccurate));

    // true because same logical precision conversion takes place
    assertTrue(timeMillis - timeNanos <= 0);
    assertTrue(millisAccurate.minus(nanosAccurate).isNegative());

    // timeNanos has greater precision and therefore is > timeMillie
    assertTrue(timeNanos - timeMillis > 0);
    assertTrue(nanosAccurate.minus(millisAccurate).negated().isNegative());
//选择一个转换为较低精度时不相同的数字
长时间纳米=123456789101121314L;
long-timeMillis=TimeUnit.millises.convert(timeNanos,TimeUnit.NANOSECONDS);
//从毫秒创建
持续时间毫秒精度=持续时间(单位为毫秒);
持续时间NanosPrecision=纳米的持续时间(timeNanos);
//由于精度差异而为假
assertFalse(timeMillis==timeNanos);
assertFalse(毫秒精度等于(纳米精度));
//由于发生相同的逻辑精度转换,因此为true
assertTrue(timeMillis-timeNanos timeMillie
assertTrue(timeNanos-timeMillis>0);
assertTrue(纳米精度.减号(毫秒精度).Negative().isNegative());
简言之,我真不敢相信我花了这么长时间才找到持续时间!
:)

@Keppil-也许他只是对另一种语言有更多的经验?我不知道。但是你可以用几行代码来创建一个。我刚刚找到了答案(还没有发布),如果没有其他人先发布,我会稍后发布。如果你找到了,那么你就得到了答案和一百万分!!!如果您还没有使用Java8及其新版本,请添加提供类似类的第三部分库。JSR 310的灵感来源于Joda Time,但完全是重新设计的。类似的概念,但不同的实现。@BasilBourque或use,它更接近于Java8,并将使未来的过渡更加顺利。