Java Code Examples for java.time.LocalDate#now()

The following examples show how to use java.time.LocalDate#now() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TCKLocalDate.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_Clock_allSecsInDay_utc() {
    for (int i = 0; i < (2 * 24 * 60 * 60); i++) {
        Instant instant = Instant.ofEpochSecond(i);
        Clock clock = Clock.fixed(instant, ZoneOffset.UTC);
        LocalDate test = LocalDate.now(clock);
        assertEquals(test.getYear(), 1970);
        assertEquals(test.getMonth(), Month.JANUARY);
        assertEquals(test.getDayOfMonth(), (i < 24 * 60 * 60 ? 1 : 2));
    }
}
 
Example 2
Source File: CapletVolatilitiesParametricTest.java    From finmath-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlatVolatilityUsingD() {
	final LocalDate referenceDate = LocalDate.now();
	final double a = 0, b = 0, c = 0, d = 0.4;
	final CapletVolatilitiesParametric volatilityModel = new CapletVolatilitiesParametric("flat", referenceDate, a, b, c, d);

	final double maturity	= 2.0;
	final double strike	= 0.03;
	final double volatility = volatilityModel.getValue(maturity, strike, QuotingConvention.VOLATILITYLOGNORMAL);

	assertTrue(volatility - d < 1E-15);
}
 
Example 3
Source File: AssertJJava8UnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLocalDate_shouldAssert() throws Exception {
    final LocalDate givenLocalDate = LocalDate.of(2016, 7, 8);
    final LocalDate todayDate = LocalDate.now();

    assertThat(givenLocalDate).isBefore(LocalDate.of(2020, 7, 8)).isAfterOrEqualTo(LocalDate.of(1989, 7, 8));

    assertThat(todayDate).isAfter(LocalDate.of(1989, 7, 8)).isToday();
}
 
Example 4
Source File: InMemBookingRepository.java    From Mastering-Microservices-with-Java with MIT License 5 votes vote down vote up
/**
 * Initialize the in-memory Booking Repository with sample Map
 */
public InMemBookingRepository() {
    entities = new HashMap();
    index = index.add(BigInteger.ONE);
    Booking booking = new Booking(index.toString(), "Booking ".concat(index.toString()), "1", "1", "1", LocalDate.now(), LocalTime.now());
    entities.put(index.toString(), booking);
    index = index.add(BigInteger.ONE);
    Booking booking2 = new Booking(index.toString(), "Booking ".concat(index.toString()), "2", "2", "2", LocalDate.now(), LocalTime.now());
    entities.put(index.toString(), booking2);
}
 
Example 5
Source File: AnnotatedServiceRequestConverterTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void testDefaultRequestConverter_timeBean() {
    final WebClient client = WebClient.of(server.httpUri());

    final TimeBean timeBean = new TimeBean();
    timeBean.duration = Duration.ofDays(1);
    timeBean.instant = Instant.now();
    timeBean.localDate = LocalDate.now();
    timeBean.localDateTime = LocalDateTime.now();
    timeBean.localTime = LocalTime.now();
    timeBean.offsetDateTime = OffsetDateTime.now();
    timeBean.offsetTime = OffsetTime.now();
    timeBean.period = Period.of(1, 2, 3);
    timeBean.zonedDateTime = ZonedDateTime.now();
    timeBean.zoneId = ZoneId.systemDefault();
    timeBean.zoneOffset = ZoneOffset.ofHours(1);

    final QueryParams queryParams = QueryParams.builder()
                                               .add("duration", timeBean.duration.toString())
                                               .add("instant", timeBean.instant.toString())
                                               .add("localDate", timeBean.localDate.toString())
                                               .add("localDateTime", timeBean.localDateTime.toString())
                                               .add("localTime", timeBean.localTime.toString())
                                               .add("offsetDateTime", timeBean.offsetDateTime.toString())
                                               .add("offsetTime", timeBean.offsetTime.toString())
                                               .add("period", timeBean.period.toString())
                                               .add("zonedDateTime", timeBean.zonedDateTime.toString())
                                               .add("zoneId", timeBean.zoneId.toString())
                                               .add("zoneOffset", timeBean.zoneOffset.toString())
                                               .build();

    final AggregatedHttpResponse response = client.execute(HttpRequest.of(HttpMethod.POST,
                                                                          "/2/default/time",
                                                                          MediaType.FORM_DATA,
                                                                          queryParams.toQueryString()))
                                                  .aggregate()
                                                  .join();
    assertThat(response.status()).isEqualTo(HttpStatus.OK);
    assertThat(response.contentUtf8()).isEqualTo(timeBean.toString());
}
 
Example 6
Source File: TCKLocalDate.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_ZoneId() {
    ZoneId zone = ZoneId.of("UTC+01:02:03");
    LocalDate expected = LocalDate.now(Clock.system(zone));
    LocalDate test = LocalDate.now(zone);
    for (int i = 0; i < 100; i++) {
        if (expected.equals(test)) {
            return;
        }
        expected = LocalDate.now(Clock.system(zone));
        test = LocalDate.now(zone);
    }
    assertEquals(test, expected);
}
 
Example 7
Source File: TCKLocalDate.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void now_ZoneId() {
    ZoneId zone = ZoneId.of("UTC+01:02:03");
    LocalDate expected = LocalDate.now(Clock.system(zone));
    LocalDate test = LocalDate.now(zone);
    for (int i = 0; i < 100; i++) {
        if (expected.equals(test)) {
            return;
        }
        expected = LocalDate.now(Clock.system(zone));
        test = LocalDate.now(zone);
    }
    assertEquals(test, expected);
}
 
Example 8
Source File: VisitsServiceImpl.java    From sk-admin with Apache License 2.0 5 votes vote down vote up
@Override
public void count(HttpServletRequest request) {
    LocalDate localDate = LocalDate.now();
    Visits visits = visitsDao.findByDate(localDate.toString());
    visits.setPvCounts(visits.getPvCounts() + 1);
    long ipCounts = logDao.findIp(localDate.toString(), localDate.plusDays(1).toString());
    visits.setIpCounts(ipCounts);
    visitsDao.save(visits);
}
 
Example 9
Source File: Parser.java    From data-generator with MIT License 5 votes vote down vote up
private Parser parseDate() {
    Matcher m = datePattern.matcher(value);
    if (m.find()) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(m.group(3));
        LocalDate startDate = m.group(1).trim().equals("now") ? LocalDate.now()
                : LocalDate.parse(m.group(1).trim());
        LocalDate endDate = m.group(2).trim().equals("now") ? LocalDate.now()
                : LocalDate.parse(m.group(2).trim());
        int length = (int) startDate.until(endDate, ChronoUnit.DAYS);
        LocalDate randDate = length > 0 ? startDate.plusDays(rand.nextInt(length + 1))
                : startDate.minusDays(rand.nextInt(1 - length));
        value = value.replace(m.group(), randDate.format(formatter));
    }
    return this;
}
 
Example 10
Source File: TestExampleCode.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
void test_unknownChronologyWithDateTime() {
    ChronoLocalDate date = LocalDate.now();
    ChronoLocalDateTime<?> cldt = date.atTime(LocalTime.NOON);
    ChronoLocalDate ld = cldt.toLocalDate();
    ChronoLocalDateTime<?> noonTomorrow = tomorrowNoon(ld);
}
 
Example 11
Source File: TCKLocalDate.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=NullPointerException.class)
public void now_Clock_nullClock() {
    LocalDate.now((Clock) null);
}
 
Example 12
Source File: TestExampleCode.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
void test_unknownChronologyWithDateTime() {
    ChronoLocalDate date = LocalDate.now();
    ChronoLocalDateTime<?> cldt = date.atTime(LocalTime.NOON);
    ChronoLocalDate ld = cldt.toLocalDate();
    ChronoLocalDateTime<?> noonTomorrow = tomorrowNoon(ld);
}
 
Example 13
Source File: IssueTrendServiceImpl.java    From pacbot with Apache License 2.0 4 votes vote down vote up
/**
 * Fill no data dates with previous.
 *
 * @param trendList the trend list
 * @param firstDay the first day
 * @param lastDay the last day
 */
private void fillNoDataDatesWithPrevious(
        List<Map<String, Object>> trendList, LocalDate firstDay,
        LocalDate lastDay) {

    // We don't want data for future weeks. If the quarter being
    // requested is the ongoing quarter, the max we we are interested
    // is data up to and including the ongoing day in the ongoing week.
    if (lastDay.isAfter(LocalDate.now())) {
        lastDay = LocalDate.now();
    }

    List<LocalDate> listOfAllDates = new ArrayList<>();

    LocalDate iterationDate = firstDay;

    // Have a temp variable called iterationDate. Keep incrementing it by 1,
    // until we reach the end date. In each such iteration, add each date to
    // our list of dates
    while (!iterationDate.isAfter(lastDay)) {
        listOfAllDates.add(iterationDate);
        iterationDate = iterationDate.plusDays(1);
    }

    // Iterate through each date. If the data from ES is missing for any
    // such
    // date, add a dummy map with zero values
    Map<String, Object> currentData = new LinkedHashMap<>();
    currentData.put(TOTAL, 0);
    currentData.put(COMPLAINT, 0);
    currentData.put(NON_COMPLIANT, 0);
    currentData.put(COMPLIANCE_PERCENT, HUNDRED);

    for (int i = 0; i < listOfAllDates.size(); i++) {
        LocalDate date = listOfAllDates.get(i);
        Map<String, Object> trendInfo = getTrendDataForDate(trendList, date);
        if (trendInfo == null) {
            trendInfo = new LinkedHashMap<>();
            trendInfo.put("date", date.format(DateTimeFormatter.ISO_DATE));
            trendInfo.put(NON_COMPLIANT, currentData.get(NON_COMPLIANT));
            trendInfo.put(TOTAL, currentData.get(TOTAL));
            trendInfo.put(COMPLAINT, currentData.get(COMPLAINT));
            if (currentData.get(COMPLIANCE_PERCENT) != null) {
                trendInfo.put(COMPLIANCE_PERCENT,
                        currentData.get(COMPLIANCE_PERCENT));
            }
            trendList.add(i, trendInfo);
        } else {
            currentData = trendInfo;
        }
    }

}
 
Example 14
Source File: IntersectionTypeReceiverTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public LocalDate get() {
    return LocalDate.now();
}
 
Example 15
Source File: LocalDateSchema.java    From joyrpc with Apache License 2.0 4 votes vote down vote up
@Override
public LocalDate newMessage() {
    return LocalDate.now();
}
 
Example 16
Source File: TCKLocalDate.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions=DateTimeException.class)
public void now_Clock_tooBig() {
    Clock clock = Clock.fixed(MAX_INSTANT.plusSeconds(24 * 60 * 60), ZoneOffset.UTC);
    LocalDate.now(clock);
}
 
Example 17
Source File: Order.java    From Design-Patterns-and-SOLID-Principles-with-Java with MIT License 4 votes vote down vote up
public Order(Customer customer, Map<Product, Integer> quantities) {
    this(customer, LocalDate.now(), quantities, true);
}
 
Example 18
Source File: ThaiBuddhistDate.java    From TencentKona-8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains the current {@code ThaiBuddhistDate} from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date - today.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@linkplain Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date, not null
 * @throws DateTimeException if the current date cannot be obtained
 */
public static ThaiBuddhistDate now(Clock clock) {
    return new ThaiBuddhistDate(LocalDate.now(clock));
}
 
Example 19
Source File: ThaiBuddhistDate.java    From jdk8u-dev-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains the current {@code ThaiBuddhistDate} from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date - today.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@linkplain Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date, not null
 * @throws DateTimeException if the current date cannot be obtained
 */
public static ThaiBuddhistDate now(Clock clock) {
    return new ThaiBuddhistDate(LocalDate.now(clock));
}
 
Example 20
Source File: MinguoDate.java    From jdk8u-jdk with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Obtains the current {@code MinguoDate} from the specified clock.
 * <p>
 * This will query the specified clock to obtain the current date - today.
 * Using this method allows the use of an alternate clock for testing.
 * The alternate clock may be introduced using {@linkplain Clock dependency injection}.
 *
 * @param clock  the clock to use, not null
 * @return the current date, not null
 * @throws DateTimeException if the current date cannot be obtained
 */
public static MinguoDate now(Clock clock) {
    return new MinguoDate(LocalDate.now(clock));
}