當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。