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


Java LocalDate.until方法代码示例

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


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

示例1: test_periodUntil_LocalDate

import java.time.LocalDate; //导入方法依赖的package包/类
@Test(dataProvider="until")
public void test_periodUntil_LocalDate(int y1, int m1, int d1, int y2, int m2, int d2, int ye, int me, int de) {
    LocalDate start = LocalDate.of(y1, m1, d1);
    LocalDate end = LocalDate.of(y2, m2, d2);
    Period test = start.until(end);
    assertEquals(test.getYears(), ye);
    assertEquals(test.getMonths(), me);
    assertEquals(test.getDays(), de);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:TCKLocalDate.java

示例2: calcCancellationFee

import java.time.LocalDate; //导入方法依赖的package包/类
/**
 * Calculates and returns the cancellation fee that should be assessed when a user cancels a
 * booking.
 *
 * This method uses the new Java 8 Time API, which a little confusing at first, but way more
 * useful (and easier to use) than the nightmarish way things used to be done.
 *
 * It does NOT account for time zones, since in the real world, this code would be running
 * server side, not client side, and then time zones would not matter.
 *
 * Sadly, the new Java Time and Date API is almost laughably annoying in that there is NO built
 * in easy way to transform an existing Java Date object into a new time api date object,
 * excepting the toInstant method, which brings with it it's own cluttered bag of complications.
 * Why?!?
 *
 * @param booking The booking to calculate cancellation fees for.
 *
 * @return The cancellation fee to cancel the provided booking.
 */
public static double calcCancellationFee(Booking booking) {
    // Define some useful dates for our calculations
    LocalDate today = LocalDate.now();
    Date temp = booking.getCheckInDate();
    ZonedDateTime zdt = Instant.ofEpochMilli(temp.getTime()).atZone(ZoneId.systemDefault());
    LocalDate checkInDate = zdt.toLocalDate();
    temp = booking.getCreatedDate();
    zdt = Instant.ofEpochMilli(temp.getTime()).atZone(ZoneId.systemDefault());
    LocalDate bookingCreationDate = LocalDate.from(zdt);
    long daysTillCheckIn = today.until(checkInDate, ChronoUnit.DAYS);

    // Determine the fee percentage based on specifications
    double feePercentage;
    if(bookingCreationDate.plusDays(2).isAfter(today)) {
        feePercentage = 0.00;
    } else if(daysTillCheckIn > 30) {
        feePercentage = 20.00;
    } else if(daysTillCheckIn > 7) {
        feePercentage = 30.00;
    } else {
        feePercentage = 60.00;
    }

    // Calculate the fee from the bill and the fee percentage
    double fee = 0.00;

    for(BillItem item : booking.getBill().getCharges()) {
        double totalItemPrice = item.getTotalPrice();
        // Check for cases where another discount is applied, etc.
        if(item.getTotalPrice() > 0) {
            fee += (totalItemPrice * feePercentage);
        }
    }

    return fee;
}
 
开发者ID:maillouxc,项目名称:git-rekt,代码行数:56,代码来源:BookingService.java

示例3: main

import java.time.LocalDate; //导入方法依赖的package包/类
/**
 * 程序执行入口.
 *
 * @param args 命令行参数
 */
public static void main(String[] args) {

	LocalDate today = LocalDate.now();

	//Get the Year, check if it's leap year
	System.out.println("Year " + today.getYear() + " is Leap Year? " + today.isLeapYear());

	//Compare two LocalDate for before and after
	System.out.println("Today is before 01/01/2015? " + today.isBefore(LocalDate.of(2015, 1, 1)));

	//Create LocalDateTime from LocalDate
	System.out.println("Current Time=" + today.atTime(LocalTime.now()));

	//plus and minus operations
	System.out.println("10 days after today will be " + today.plusDays(10));
	System.out.println("3 weeks after today will be " + today.plusWeeks(3));
	System.out.println("20 months after today will be " + today.plusMonths(20));

	System.out.println("10 days before today will be " + today.minusDays(10));
	System.out.println("3 weeks before today will be " + today.minusWeeks(3));
	System.out.println("20 months before today will be " + today.minusMonths(20));

	//Temporal adjusters for adjusting the dates
	System.out.println("First date of this month= " + today.with(TemporalAdjusters.firstDayOfMonth()));
	LocalDate lastDayOfYear = today.with(TemporalAdjusters.lastDayOfYear());
	System.out.println("Last date of this year= " + lastDayOfYear);

	Period period = today.until(lastDayOfYear);
	System.out.println("Period Format= " + period);
	System.out.println("Months remaining in the year= " + period.getMonths());
}
 
开发者ID:subaochen,项目名称:java-tutorial,代码行数:37,代码来源:DateTimeAPITest.java

示例4: test_until_TemporalUnit

import java.time.LocalDate; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit(LocalDate date1, LocalDate date2, TemporalUnit unit, long expected) {
    long amount = date1.until(date2, unit);
    assertEquals(amount, expected);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKLocalDate.java

示例5: test_until_TemporalUnit_negated

import java.time.LocalDate; //导入方法依赖的package包/类
@Test(dataProvider="periodUntilUnit")
public void test_until_TemporalUnit_negated(LocalDate date1, LocalDate date2, TemporalUnit unit, long expected) {
    long amount = date2.until(date1, unit);
    assertEquals(amount, -expected);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKLocalDate.java

示例6: test_until_invalidType

import java.time.LocalDate; //导入方法依赖的package包/类
@Test(expectedExceptions=DateTimeException.class)
public void test_until_invalidType() {
    LocalDate start = LocalDate.of(2010, 6, 30);
    start.until(LocalTime.of(11, 30), DAYS);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:6,代码来源:TCKLocalDate.java

示例7: getYearsBetween

import java.time.LocalDate; //导入方法依赖的package包/类
/**
 * 取得两个日期之间相差的年数
 * getYearsBetween
 *
 * @param t1 开始时间
 * @param t2 结果时间
 * @return t1到t2间的年数,如果t2在 t1之后,返回正数,否则返回负数
 */
public static long getYearsBetween(LocalDate t1, LocalDate t2) {
    return t1.until(t2, ChronoUnit.YEARS);
}
 
开发者ID:yu199195,项目名称:happylifeplat-transaction,代码行数:12,代码来源:DateUtils.java

示例8: getDaysBetween

import java.time.LocalDate; //导入方法依赖的package包/类
/**
 * 取得两个日期之间相差的日数
 *
 * @param t1 开始日期
 * @param t2 结束日期
 * @return t1到t2间的日数,如果t2 在 t1之后,返回正数,否则返回负数
 */
public static long getDaysBetween(LocalDate t1, LocalDate t2) {
    return t1.until(t2, ChronoUnit.DAYS);
}
 
开发者ID:yu199195,项目名称:happylifeplat-transaction,代码行数:11,代码来源:DateUtils.java

示例9: getMonthsBetween

import java.time.LocalDate; //导入方法依赖的package包/类
/**
 * 取得两个日期之间相差的月数
 *
 * @param t1 开始日期
 * @param t2 结束日期
 * @return t1到t2间的日数,如果t2 在 t1之后,返回正数,否则返回负数
 */
public static long getMonthsBetween(LocalDate t1, LocalDate t2) {
    return t1.until(t2, ChronoUnit.MONTHS);
}
 
开发者ID:yu199195,项目名称:happylifeplat-transaction,代码行数:11,代码来源:DateUtils.java


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