本文整理汇总了Java中java.time.Period.ofDays方法的典型用法代码示例。如果您正苦于以下问题:Java Period.ofDays方法的具体用法?Java Period.ofDays怎么用?Java Period.ofDays使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.time.Period
的用法示例。
在下文中一共展示了Period.ofDays方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSplitLocalDate
import java.time.Period; //导入方法依赖的package包/类
@Test(dataProvider = "directions")
public void testSplitLocalDate(boolean ascending) {
final Period step = Period.ofDays(1);
final LocalDate start = LocalDate.of(1990, 1, 1);
final LocalDate end = ascending ? start.plusDays(10000) : start.minusDays(10000);
final Range<LocalDate> range = Range.of(start, end, step);
final List<Range<LocalDate>> segments = range.split(100);
Assert.assertTrue(segments.size() > 1, "There are multiple segments");
for (int i=1; i<segments.size(); ++i) {
final Range<LocalDate> prior = segments.get(i-1);
final Range<LocalDate> next = segments.get(i);
Assert.assertEquals(prior.end(), next.start(), "Date connect as expect");
if (i == 1) Assert.assertEquals(prior.start(), range.start(), "First segment start matches range start");
if (i == segments.size()-1) {
Assert.assertEquals(next.end(), range.end(), "Last segment end matches range end");
}
}
}
示例2: 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);
}
}
示例3: stringToPeriod
import java.time.Period; //导入方法依赖的package包/类
public Period stringToPeriod(String in) {
if (in == null || in.isEmpty()) {
return Period.ofDays(1);
} else {
return Period.parse(in);
}
}
示例4: localDateRanges
import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="localDateRanges")
public Object[][] localDateRanges() {
return new Object[][] {
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(1), false },
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(5), false },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2013, 1, 1), Period.ofDays(3), false },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2014, 1, 1), Period.ofDays(7), false },
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(1), true },
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(5), true },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2013, 1, 1), Period.ofDays(3), true },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2014, 1, 1), Period.ofDays(7), true },
};
}
示例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},
};
}
示例6: localDateRanges
import java.time.Period; //导入方法依赖的package包/类
@DataProvider(name="LocalDateRanges")
public Object[][] localDateRanges() {
return new Object[][] {
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(1), false },
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(5), false },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2013, 1, 1), Period.ofDays(3), false },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2014, 1, 1), Period.ofDays(6), false },
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(1), true },
{ LocalDate.of(1990, 1, 1), LocalDate.of(1990, 12, 31), Period.ofDays(5), true },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2013, 1, 1), Period.ofDays(3), true },
{ LocalDate.of(2014, 12, 1), LocalDate.of(2014, 1, 1), Period.ofDays(6), true },
};
}
示例7: 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);
}
示例8: 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"},
};
}
示例9: 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);
}
示例10: 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},
};
}
示例11: 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 );
}
示例12: test_minusDays_overflowTooSmall
import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_minusDays_overflowTooSmall() {
Period test = Period.ofDays(Integer.MIN_VALUE);
test.minusDays(1);
}
示例13: 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)},
};
}
示例14: test_plusDays_overflowTooBig
import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_plusDays_overflowTooBig() {
Period test = Period.ofDays(Integer.MAX_VALUE);
test.plusDays(1);
}
示例15: test_minusDays_overflowTooBig
import java.time.Period; //导入方法依赖的package包/类
@Test(expectedExceptions=ArithmeticException.class)
public void test_minusDays_overflowTooBig() {
Period test = Period.ofDays(Integer.MAX_VALUE);
test.minusDays(-1);
}