當前位置: 首頁>>代碼示例>>Java>>正文


Java LocalDateTime.with方法代碼示例

本文整理匯總了Java中java.time.LocalDateTime.with方法的典型用法代碼示例。如果您正苦於以下問題:Java LocalDateTime.with方法的具體用法?Java LocalDateTime.with怎麽用?Java LocalDateTime.with使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.time.LocalDateTime的用法示例。


在下文中一共展示了LocalDateTime.with方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testRoundingDate

import java.time.LocalDateTime; //導入方法依賴的package包/類
@Test
public void testRoundingDate() {
    int month = randomIntBetween(1, 12);
    int day = randomIntBetween(1, 28);
    int hourOfDay = randomIntBetween(0, 23);
    int minute = randomIntBetween(0, 59);
    int year = randomIntBetween(2016, 2025);
    LocalDateTime dateTime = LocalDateTime.of(year, month, day, hourOfDay, minute);
    LocalDateTime roundedDate = Generator.roundEndDateToTheNextHalfHour(dateTime);
    assertThat(roundedDate.isAfter(dateTime), is(true));

    LocalDateTime expectedDate = dateTime.with(temporal -> {
        int currentMinute = temporal.get(ChronoField.MINUTE_OF_HOUR);
        if (currentMinute >= 30) {
            return temporal.plus(60 - currentMinute, ChronoUnit.MINUTES);
        } else {
            return temporal.plus(30 - currentMinute, ChronoUnit.MINUTES);
        }
    });

    assertThat(roundedDate, is(expectedDate));
}
 
開發者ID:spinscale,項目名稱:maxcube-java,代碼行數:23,代碼來源:GeneratorTest.java

示例2: getDateRelativeToToday

import java.time.LocalDateTime; //導入方法依賴的package包/類
private LocalDateTime getDateRelativeToToday(ChronoField field, int buffer) {
    LocalDateTime date = LocalDateTime.now();
    // want a date with a buffer either side of it and also not including the date itself
    int gap = 2 * buffer + 1;
    int max = (int)field.range().getSmallestMaximum();
    int rand = getRandomInt(1, max - gap);
    int value = rand >= date.get(field) - buffer ? rand + gap : rand;
    LocalDateTime relativeDate = date.with(field, value);

    // Adding logging to help with some spurious errors...
    log.info("Date: " + date + " rand: " + rand + " value: " + value + " Picked date: " + relativeDate);

    return relativeDate;
}
 
開發者ID:NHS-digital-website,項目名稱:hippo,代碼行數:15,代碼來源:CmsSteps.java

示例3: test_appendText2arg_format

import java.time.LocalDateTime; //導入方法依賴的package包/類
@Test(dataProvider="printText")
public void test_appendText2arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    DateTimeFormatter f = builder.appendText(field, style).toFormatter(Locale.ENGLISH);
    LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
    dt = dt.with(field, value);
    String text = f.format(dt);
    assertEquals(text, expected);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:9,代碼來源:TCKDateTimeTextPrinting.java

示例4: test_appendText1arg_format

import java.time.LocalDateTime; //導入方法依賴的package包/類
@Test(dataProvider="printText")
public void test_appendText1arg_format(TemporalField field, TextStyle style, int value, String expected) throws Exception {
    if (style == TextStyle.FULL) {
        DateTimeFormatter f = builder.appendText(field).toFormatter(Locale.ENGLISH);
        LocalDateTime dt = LocalDateTime.of(2010, 1, 1, 0, 0);
        dt = dt.with(field, value);
        String text = f.format(dt);
        assertEquals(text, expected);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:11,代碼來源:TCKDateTimeTextPrinting.java

示例5: adjustField

import java.time.LocalDateTime; //導入方法依賴的package包/類
private static LocalDateTime adjustField(LocalDateTime time, ChronoField field, int stepRate) {
    return time.with(field, time.get(field) - time.get(field) % stepRate);
}
 
開發者ID:dlemmermann,項目名稱:CalendarFX,代碼行數:4,代碼來源:Util.java


注:本文中的java.time.LocalDateTime.with方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。