当前位置: 首页>>代码示例>>Java>>正文


Java Period.ofYears方法代码示例

本文整理汇总了Java中java.time.Period.ofYears方法的典型用法代码示例。如果您正苦于以下问题:Java Period.ofYears方法的具体用法?Java Period.ofYears怎么用?Java Period.ofYears使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.time.Period的用法示例。


在下文中一共展示了Period.ofYears方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: apply

import java.time.Period; //导入方法依赖的package包/类
@Override
public final Period apply(String value) {
    try {
        if (getNullChecker().applyAsBoolean(value)) {
            return null;
        } else {
            final Matcher matcher = pattern.matcher(value);
            if (matcher.matches()) {
                final int digits = Integer.parseInt(matcher.group(1));
                final char code = matcher.group(2).toUpperCase().charAt(0);
                switch (code) {
                    case 'D':   return Period.ofDays(digits);
                    case 'W':   return Period.ofWeeks(digits);
                    case 'M':   return Period.ofMonths(digits);
                    case 'Y':   return Period.ofYears(digits);
                    default:    throw new IllegalArgumentException("Unsupported period type: " + code);
                }
            } else {
                throw new IllegalArgumentException("Cannot parse value into an Period: " + value + " pattern: " + pattern.pattern());
            }
        }
    } catch (Exception ex) {
        throw new FormatException("Failed to parse value into Period: " + value, ex);
    }
}
 
开发者ID:zavtech,项目名称:morpheus-core,代码行数:26,代码来源:ParserOfPeriod.java

示例2: convertTimeQuantityToPeriodOrMilliSeconds

import java.time.Period; //导入方法依赖的package包/类
private Object convertTimeQuantityToPeriodOrMilliSeconds(DvQuantity dvQuantity) {
    int magnitude = Double.valueOf(dvQuantity.getMagnitude()).intValue();
    if ("a".equals(dvQuantity.getUnit())) {
        return Period.ofYears(magnitude);
    } else if ("mo".equals(dvQuantity.getUnit())) {
        return Period.ofMonths(magnitude);
    } else if ("d".equals(dvQuantity.getUnit())) {
        return Period.ofDays(magnitude);
    } else if ("h".equals(dvQuantity.getUnit())) {
        return HOUR_IN_MILLISECONDS * magnitude;
    }
    throw new UnsupportedOperationException("Unsupported time period unit: " + dvQuantity.getUnit());
}
 
开发者ID:gdl-lang,项目名称:gdl2,代码行数:14,代码来源:Interpreter.java

示例3: test_hashCode

import java.time.Period; //导入方法依赖的package包/类
public void test_hashCode() {
    Period test5 = Period.ofDays(5);
    Period test6 = Period.ofDays(6);
    Period test5M = Period.ofMonths(5);
    Period test5Y = Period.ofYears(5);
    assertEquals(test5.hashCode() == test5.hashCode(), true);
    assertEquals(test5.hashCode() == test6.hashCode(), false);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:TCKPeriod.java

示例4: data_plus_TemporalAmount

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="plus_TemporalAmount")
Object[][] data_plus_TemporalAmount() {
    return new Object[][] {
        {YearMonth.of(1, 1), Period.ofYears(1), YearMonth.of(2, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(-12), YearMonth.of(-11, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofYears(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofYears(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), Period.ofYears(-999999999), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), Period.ofYears(999999999), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(1), YearMonth.of(1, 2), null},
        {YearMonth.of(1, 1), Period.ofMonths(-12), YearMonth.of(0, 1), null},
        {YearMonth.of(1, 1), Period.ofMonths(121), YearMonth.of(11, 2), null},
        {YearMonth.of(1, 1), Period.ofMonths(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofMonths(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofMonths(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), Period.ofMonths(-1), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), Period.ofMonths(1), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofYears(1).withMonths(2), YearMonth.of(2, 3), null},
        {YearMonth.of(1, 1), Period.ofYears(-12).withMonths(-1), YearMonth.of(-12, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(2).withYears(1), YearMonth.of(2, 3), null},
        {YearMonth.of(1, 1), Period.ofMonths(-1).withYears(-12), YearMonth.of(-12, 12), null},

        {YearMonth.of(1, 1), Period.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofHours(365*24), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofMinutes(365*24*60), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofSeconds(365*24*3600), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofNanos(365*24*3600*1000000000), null, DateTimeException.class},
    };
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:35,代码来源:TCKYearMonth.java

示例5: data_minus_TemporalAmount

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="minus_TemporalAmount")
Object[][] data_minus_TemporalAmount() {
    return new Object[][] {
        {YearMonth.of(1, 1), Period.ofYears(1), YearMonth.of(0, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(-12), YearMonth.of(13, 1), null},
        {YearMonth.of(1, 1), Period.ofYears(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofYears(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofYears(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 1), Period.ofYears(999999999), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(0, 12), Period.ofYears(-999999999), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofMonths(1), YearMonth.of(0, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(-12), YearMonth.of(2, 1), null},
        {YearMonth.of(1, 1), Period.ofMonths(121), YearMonth.of(-10, 12), null},
        {YearMonth.of(1, 1), Period.ofMonths(0), YearMonth.of(1, 1), null},
        {YearMonth.of(999999999, 12), Period.ofMonths(0), YearMonth.of(999999999, 12), null},
        {YearMonth.of(-999999999, 1), Period.ofMonths(0), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(-999999999, 2), Period.ofMonths(1), YearMonth.of(-999999999, 1), null},
        {YearMonth.of(999999999, 11), Period.ofMonths(-1), YearMonth.of(999999999, 12), null},

        {YearMonth.of(1, 1), Period.ofYears(1).withMonths(2), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofYears(-12).withMonths(-1), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofMonths(2).withYears(1), YearMonth.of(-1, 11), null},
        {YearMonth.of(1, 1), Period.ofMonths(-1).withYears(-12), YearMonth.of(13, 2), null},

        {YearMonth.of(1, 1), Period.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofDays(365), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofHours(365*24), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofMinutes(365*24*60), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofSeconds(365*24*3600), null, DateTimeException.class},
        {YearMonth.of(1, 1), Duration.ofNanos(365*24*3600*1000000000), null, DateTimeException.class},
    };
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:35,代码来源:TCKYearMonth.java

示例6: data_plusValid

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="plusValid")
Object[][] data_plusValid() {
    return new Object[][] {
            {2012, Period.ofYears(0), 2012},
            {2012, Period.ofYears(1), 2013},
            {2012, Period.ofYears(2), 2014},
            {2012, Period.ofYears(-2), 2010},
    };
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:TCKYear.java

示例7: data_toString

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="toStringAndParse")
Object[][] data_toString() {
    return new Object[][] {
            {Period.ZERO, "P0D"},
            {Period.ofDays(0), "P0D"},
            {Period.ofYears(1), "P1Y"},
            {Period.ofMonths(1), "P1M"},
            {Period.ofDays(1), "P1D"},
            {Period.of(1, 2, 0), "P1Y2M"},
            {Period.of(0, 2, 3), "P2M3D"},
            {Period.of(1, 2, 3), "P1Y2M3D"},
    };
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:TCKPeriod.java

示例8: test_hashCode

import java.time.Period; //导入方法依赖的package包/类
@Test
public void test_hashCode() {
    Period test5 = Period.ofDays(5);
    Period test6 = Period.ofDays(6);
    Period test5M = Period.ofMonths(5);
    Period test5Y = Period.ofYears(5);
    assertEquals(test5.hashCode() == test5.hashCode(), true);
    assertEquals(test5.hashCode() == test6.hashCode(), false);
    assertEquals(test5.hashCode() == test5M.hashCode(), false);
    assertEquals(test5.hashCode() == test5Y.hashCode(), false);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:12,代码来源:TestPeriod.java

示例9: main

import java.time.Period; //导入方法依赖的package包/类
public static void main(String[] args) {
    Period period = Period.of(1, 2, 7);
    Period period2 = Period.ofYears(2);
    Period period3 = Period.ofMonths(5);
    Period period4 = Period.ofWeeks(10);
    Period period5 = Period.ofDays(15);

    Period period6 = Period.ofDays(15);

    Period p5Yrs1 = Period.parse("P5y");
    Period p5Yrs2 = Period.parse("p5y");
    Period p5Yrs3 = Period.parse("P5Y");
    Period p5Yrs4 = Period.parse("+P5Y");
    Period p5Yrs5 = Period.parse("P+5Y");
    Period p5Yrs6 = Period.parse("-P-5Y");

    System.out.printf("%s : %s", p5Yrs1, p5Yrs2);
    System.out.println();
    System.out.printf("-P-5Y: %s", p5Yrs6);
    System.out.println();

    Period p5Yrs7 = Period.parse("P5y1m2d");
    Period p5Yrs8 = Period.parse("p9m");
    Period p5Yrs9 = Period.parse("P60d");

    // For the string form PnW, the count of weeks is multiplied by 7 to
    // get the number of days
    Period p5Yrs10 = Period.parse("-P5Y5W");

    System.out.printf("p5Yrs7: %s,  p5Yrs8: %s, p5Yrs9: %s, p5Yrs10: %s",
            p5Yrs7,
            p5Yrs8,
            p5Yrs9,
            p5Yrs10);
    System.out.println();

    // static method between
    LocalDate carnivalStart = LocalDate.of(2050, 12, 31);
    LocalDate carnivalEnd = LocalDate.of(2051, 1, 2);

    // period = endDate - startDate
    Period periodBetween = Period.between(carnivalEnd, carnivalStart);
    System.out.println(periodBetween );

}
 
开发者ID:huby,项目名称:java-se8-oca-study-guide,代码行数:46,代码来源:Main.java

示例10: test_plusYears_overflowTooSmall

import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_plusYears_overflowTooSmall() {
    Period test = Period.ofYears(Integer.MIN_VALUE);
    test.plusYears(-1);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKPeriod.java

示例11: data_factory_parseSuccess

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="parseSuccess")
Object[][] data_factory_parseSuccess() {
    return new Object[][] {
            {"P1Y", Period.ofYears(1)},
            {"P12Y", Period.ofYears(12)},
            {"P987654321Y", Period.ofYears(987654321)},
            {"P+1Y", Period.ofYears(1)},
            {"P+12Y", Period.ofYears(12)},
            {"P+987654321Y", Period.ofYears(987654321)},
            {"P+0Y", Period.ofYears(0)},
            {"P0Y", Period.ofYears(0)},
            {"P-0Y", Period.ofYears(0)},
            {"P-25Y", Period.ofYears(-25)},
            {"P-987654321Y", Period.ofYears(-987654321)},
            {"P" + Integer.MAX_VALUE + "Y", Period.ofYears(Integer.MAX_VALUE)},
            {"P" + Integer.MIN_VALUE + "Y", Period.ofYears(Integer.MIN_VALUE)},

            {"P1M", Period.ofMonths(1)},
            {"P12M", Period.ofMonths(12)},
            {"P987654321M", Period.ofMonths(987654321)},
            {"P+1M", Period.ofMonths(1)},
            {"P+12M", Period.ofMonths(12)},
            {"P+987654321M", Period.ofMonths(987654321)},
            {"P+0M", Period.ofMonths(0)},
            {"P0M", Period.ofMonths(0)},
            {"P-0M", Period.ofMonths(0)},
            {"P-25M", Period.ofMonths(-25)},
            {"P-987654321M", Period.ofMonths(-987654321)},
            {"P" + Integer.MAX_VALUE + "M", Period.ofMonths(Integer.MAX_VALUE)},
            {"P" + Integer.MIN_VALUE + "M", Period.ofMonths(Integer.MIN_VALUE)},

            {"P1W", Period.ofDays(1 * 7)},
            {"P12W", Period.ofDays(12 * 7)},
            {"P7654321W", Period.ofDays(7654321 * 7)},
            {"P+1W", Period.ofDays(1 * 7)},
            {"P+12W", Period.ofDays(12 * 7)},
            {"P+7654321W", Period.ofDays(7654321 * 7)},
            {"P+0W", Period.ofDays(0)},
            {"P0W", Period.ofDays(0)},
            {"P-0W", Period.ofDays(0)},
            {"P-25W", Period.ofDays(-25 * 7)},
            {"P-7654321W", Period.ofDays(-7654321 * 7)},

            {"P1D", Period.ofDays(1)},
            {"P12D", Period.ofDays(12)},
            {"P987654321D", Period.ofDays(987654321)},
            {"P+1D", Period.ofDays(1)},
            {"P+12D", Period.ofDays(12)},
            {"P+987654321D", Period.ofDays(987654321)},
            {"P+0D", Period.ofDays(0)},
            {"P0D", Period.ofDays(0)},
            {"P-0D", Period.ofDays(0)},
            {"P-25D", Period.ofDays(-25)},
            {"P-987654321D", Period.ofDays(-987654321)},
            {"P" + Integer.MAX_VALUE + "D", Period.ofDays(Integer.MAX_VALUE)},
            {"P" + Integer.MIN_VALUE + "D", Period.ofDays(Integer.MIN_VALUE)},

            {"P0Y0M0D", Period.of(0, 0, 0)},
            {"P2Y0M0D", Period.of(2, 0, 0)},
            {"P0Y3M0D", Period.of(0, 3, 0)},
            {"P0Y0M4D", Period.of(0, 0, 4)},
            {"P2Y3M25D", Period.of(2, 3, 25)},
            {"P-2Y3M25D", Period.of(-2, 3, 25)},
            {"P2Y-3M25D", Period.of(2, -3, 25)},
            {"P2Y3M-25D", Period.of(2, 3, -25)},
            {"P-2Y-3M-25D", Period.of(-2, -3, -25)},

            {"P0Y0M0W0D", Period.of(0, 0, 0)},
            {"P2Y3M4W25D", Period.of(2, 3, 4 * 7 + 25)},
            {"P-2Y-3M-4W-25D", Period.of(-2, -3, -4 * 7 - 25)},
    };
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:73,代码来源:TCKPeriod.java

示例12: test_plusYears_overflowTooBig

import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_plusYears_overflowTooBig() {
    Period test = Period.ofYears(Integer.MAX_VALUE);
    test.plusYears(1);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKPeriod.java

示例13: test_multipliedBy_overflowTooSmall

import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_multipliedBy_overflowTooSmall() {
    Period test = Period.ofYears(Integer.MIN_VALUE / 2 - 1);
    test.multipliedBy(2);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKPeriod.java

示例14: test_minusYears_overflowTooBig

import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_minusYears_overflowTooBig() {
    Period test = Period.ofYears(Integer.MAX_VALUE);
    test.minusYears(-1);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKPeriod.java

示例15: test_minusYears_overflowTooSmall

import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_minusYears_overflowTooSmall() {
    Period test = Period.ofYears(Integer.MIN_VALUE);
    test.minusYears(1);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:6,代码来源:TCKPeriod.java


注:本文中的java.time.Period.ofYears方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。