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


Java Period.of方法代码示例

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


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

示例1: until

import java.time.Period; //导入方法依赖的package包/类
@Override
public Period until(ChronoLocalDate endDate) {
    // TODO: untested
    CopticDate end = getChronology().date(endDate);
    long totalMonths = (end.prolepticYear - this.prolepticYear) * 13 + (end.month - this.month);  // safe
    int days = end.day - this.day;
    if (totalMonths > 0 && days < 0) {
        totalMonths--;
        CopticDate calcDate = this.plusMonths(totalMonths);
        days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
    } else if (totalMonths < 0 && days > 0) {
        totalMonths++;
        days -= end.lengthOfMonth();
    }
    long years = totalMonths / 13;  // safe
    int months = (int) (totalMonths % 13);  // safe
    return Period.of(Math.toIntExact(years), months, days);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:CopticDate.java

示例2: data_plusInvalidUnit

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="plusInvalidUnit")
Object[][] data_plusInvalidUnit() {
    return new Object[][] {
            {Period.of(0, 1, 0)},
            {Period.of(0, 0, 1)},
            {Period.of(0, 1, 1)},
            {Period.of(1, 1, 1)},
            {Duration.ofDays(1)},
            {Duration.ofHours(1)},
            {Duration.ofMinutes(1)},
            {Duration.ofSeconds(1)},
    };
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:TCKYear.java

示例3: test_minus_wrongChrono

import java.time.Period; //导入方法依赖的package包/类
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_minus_wrongChrono(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ChronoPeriod isoPeriod = Period.of(2, 3, 4);
    ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
    // one of these two will fail
    period.minus(isoPeriod);
    period.minus(thaiPeriod);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:TCKChronoPeriod.java

示例4: main

import java.time.Period; //导入方法依赖的package包/类
public static void main(String[] args) {
    Period period = Period.of(2, 4, 2);
    System.out.println(period.getYears());
    System.out.println(period.getMonths());
    System.out.println(period.getDays());

    Period days5 = Period.of(0, 0, 5);
    System.out.println(days5.isZero());

    Period daysMinus5 = Period.of(5, 0, -5);
    System.out.println(daysMinus5.isNegative());

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

示例5: test_plus_wrongChrono

import java.time.Period; //导入方法依赖的package包/类
@Test(dataProvider="calendars", expectedExceptions=DateTimeException.class)
public void test_plus_wrongChrono(Chronology chrono) {
    ChronoPeriod period = chrono.period(1, 2, 3);
    ChronoPeriod isoPeriod = Period.of(2, 3, 4);
    ChronoPeriod thaiPeriod = ThaiBuddhistChronology.INSTANCE.period(2, 3, 4);
    // one of these two will fail
    period.plus(isoPeriod);
    period.plus(thaiPeriod);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:TCKChronoPeriod.java

示例6: 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:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:14,代码来源:TCKPeriod.java

示例7: test_multipliedBy

import java.time.Period; //导入方法依赖的package包/类
@Test
public void test_multipliedBy() {
    Period test = Period.of(1, 2, 3);
    assertPeriod(test.multipliedBy(0), 0, 0, 0);
    assertPeriod(test.multipliedBy(1), 1, 2, 3);
    assertPeriod(test.multipliedBy(2), 2, 4, 6);
    assertPeriod(test.multipliedBy(-3), -3, -6, -9);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:9,代码来源:TCKPeriod.java

示例8: data_minusInvalidUnit

import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="minusInvalidUnit")
Object[][] data_minusInvalidUnit() {
    return new Object[][] {
            {Period.of(0, 1, 0)},
            {Period.of(0, 0, 1)},
            {Period.of(0, 1, 1)},
            {Period.of(1, 1, 1)},
            {Duration.ofDays(1)},
            {Duration.ofHours(1)},
            {Duration.ofMinutes(1)},
            {Duration.ofSeconds(1)},
    };
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:TCKYear.java

示例9: pymd

import java.time.Period; //导入方法依赖的package包/类
private static Period pymd(int y, int m, int d) {
    return Period.of(y, m, d);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:4,代码来源:TCKPeriod.java

示例10: 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

示例11: main

import java.time.Period; //导入方法依赖的package包/类
public static void main(String[] args) {
    Period period10Days = Period.of(0, 0, 10);
    Period period1Month = Period.of(0, 1, 0);

    System.out.println(period10Days.minus(period1Month));
    System.out.println(period10Days.minusDays(5));
    System.out.println(period10Days.minusMonths(5));
    System.out.println(period10Days.minusYears(5));

    // Send reminders to your friends (limited to printing a message)
    // for a event, say a birthday celebration, if it's due in 10 days
    LocalDate bday = LocalDate.of(2017, 04, 22);
    LocalDate today = LocalDate.now();
    Period period10Days1 = Period.of(0, 0, 10);

    if (bday.minus(period10Days1).isBefore(today))
        System.out.println("Time to send out reminders to friends");

    // The minusXXX() methods
    Period period10Days2 = Period.of(0, 0, 10);
    Period period1Month1 = Period.of(0, 1, 0);

    System.out.println(period10Days2.minus(period1Month1));
    System.out.println(period10Days2.minusDays(5));
    System.out.println(period10Days2.minusMonths(5));
    System.out.println(period10Days2.minusYears(5));

    // The Period class defines multiplyBy(int), which multiplies each element in
    // the period by integer value:
    Period year1Month9Day20 = Period.of(1, 9, 20);
    System.out.println(year1Month9Day20.multipliedBy(2));
    System.out.println(year1Month9Day20.multipliedBy(-2));

    // The plus(TemporalAmount), plusDays(long), plusWeeks(long),
    // plusMonths(long) and plusYears(long) methods add to Period
    // instances and return the modified value as a Period
    Period period5Month = Period.of(0, 5, 0);
    Period period10Month = Period.of(0, 10, 0);
    Period period10Days3 = Period.of(0, 0, 10);

    System.out.println(period5Month.plus(period10Month));
    System.out.println(period10Days.plusDays(10));
    System.out.println(period10Days.plusMonths(5));
    System.out.println(period10Days.minusYears(5));

    // withDays(), withMonths() and WithYears() methods accept a int value and
    // return a copy of Period with the specified value altered
    Period period = Period.of(2020, 12, 10);
    System.out.println(period.withDays(5));
    System.out.println(period.withMonths(24));
    System.out.println(period.withYears(2017));
}
 
开发者ID:huby,项目名称:java-se8-oca-study-guide,代码行数:53,代码来源:Main.java

示例12: test_equals_otherClass

import java.time.Period; //导入方法依赖的package包/类
public void test_equals_otherClass() {
    Period test = Period.of(1, 2, 3);
    assertEquals(test.equals(""), false);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:5,代码来源:TCKPeriod.java

示例13: factory_from_TemporalAmount_Period

import java.time.Period; //导入方法依赖的package包/类
@Test
public void factory_from_TemporalAmount_Period() {
    TemporalAmount amount = Period.of(1, 2, 3);
    assertPeriod(Period.from(amount), 1, 2, 3);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKPeriod.java

示例14: 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

示例15: test_normalized_max

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


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