Example usage for java.time ZonedDateTime truncatedTo

List of usage examples for java.time ZonedDateTime truncatedTo

Introduction

In this page you can find the example usage for java.time ZonedDateTime truncatedTo.

Prototype

public ZonedDateTime truncatedTo(TemporalUnit unit) 

Source Link

Document

Returns a copy of this ZonedDateTime with the time truncated.

Usage

From source file:Main.java

public static void main(String[] args) {
    ZonedDateTime dateTime = ZonedDateTime.now();
    ZonedDateTime n = dateTime.truncatedTo(ChronoUnit.YEARS);
    System.out.println(n);/* w  ww  .ja v  a2  s .com*/
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Get a ZonedDateTime for comparison on membership dates
 *
 * @param zone the TimeZone/*from   w w w .  j av a 2s  . c o m*/
 *
 * @return the ZonedDateTime
 */
public static ZonedDateTime getZonedDateTimeForComparison(TimeZone zone) {
    ZonedDateTime dt = ZonedDateTime.now(zone.toZoneId());
    dt = dt.plus(1L, ChronoUnit.HOURS);
    dt = dt.truncatedTo(ChronoUnit.HOURS);
    return dt;
}

From source file:nu.yona.server.analysis.service.ActivityServiceTest.java

private ZonedDateTime getDayStartTime(ZonedDateTime dateTime) {
    return dateTime.truncatedTo(ChronoUnit.DAYS);
}

From source file:nu.yona.server.analysis.service.AnalysisEngineServiceTest.java

@Test
public void analyze_crossDayAppActivity_twoDayActivitiesCreated() {
    ZonedDateTime endTime = now();
    JUnitUtil.skipBefore("Skip shortly after midnight", endTime, 0, 5);
    ZonedDateTime startTime = endTime.minusDays(1);

    service.analyze(userAnonId, deviceAnonId, createSingleAppActivity("Poker App", startTime, endTime));

    ArgumentCaptor<ActivityPayload> activityPayloadCaptor = ArgumentCaptor.forClass(ActivityPayload.class);
    verify(mockActivityUpdater, times(2)).addActivity(any(), activityPayloadCaptor.capture(),
            eq(GoalDto.createInstance(gamblingGoal)), any());
    List<ActivityPayload> payloads = activityPayloadCaptor.getAllValues();
    assertThat(payloads.size(), equalTo(2));
    assertThat(payloads.get(0).startTime, equalTo(startTime));
    assertThat(payloads.get(0).endTime, equalTo(endTime.truncatedTo(ChronoUnit.DAYS)));
    assertThat(payloads.get(1).startTime, equalTo(endTime.truncatedTo(ChronoUnit.DAYS)));
    assertThat(payloads.get(1).endTime, equalTo(endTime));
    verify(mockActivityUpdater, never()).updateTimeExistingActivity(any(), any());
    verify(mockActivityUpdater, never()).updateTimeLastActivity(any(), any(), any());
}

From source file:alfio.manager.TicketReservationManager.java

void sendReminderForOfflinePaymentsToEventManagers() {
    eventRepository.findAllActives(ZonedDateTime.now(Clock.systemUTC())).stream().filter(event -> {
        ZonedDateTime dateTimeForEvent = ZonedDateTime.now(event.getZoneId());
        return dateTimeForEvent.truncatedTo(ChronoUnit.HOURS).getHour() == 5; //only for the events at 5:00 local time
    }).forEachOrdered(event -> {// w ww.  jav a  2s .  c  o  m
        ZonedDateTime dateTimeForEvent = ZonedDateTime.now(event.getZoneId()).truncatedTo(ChronoUnit.DAYS)
                .plusDays(1);
        List<TicketReservationInfo> reservations = ticketReservationRepository
                .findAllOfflinePaymentReservationWithExpirationBefore(dateTimeForEvent, event.getId());
        log.info("for event {} there are {} pending offline payments to handle", event.getId(),
                reservations.size());
        if (!reservations.isEmpty()) {
            Organization organization = organizationRepository.getById(event.getOrganizationId());
            List<String> cc = notificationManager.getCCForEventOrganizer(event);
            String subject = String.format(
                    "There are %d pending offline payments that will expire in event: %s", reservations.size(),
                    event.getDisplayName());
            String baseUrl = configurationManager
                    .getRequiredValue(Configuration.from(event.getOrganizationId(), event.getId(), BASE_URL));
            Map<String, Object> model = TemplateResource
                    .prepareModelForOfflineReservationExpiringEmailForOrganizer(event, reservations, baseUrl);
            notificationManager.sendSimpleEmail(event, organization.getEmail(), cc, subject,
                    () -> templateManager.renderTemplate(event,
                            TemplateResource.OFFLINE_RESERVATION_EXPIRING_EMAIL_FOR_ORGANIZER, model,
                            Locale.ENGLISH));
            extensionManager.handleOfflineReservationsWillExpire(event, reservations);
        }
    });
}

From source file:org.openhab.binding.darksky.internal.handler.DarkSkyWeatherAndForecastHandler.java

/**
 * Applies the given configuration to the given timestamp.
 *
 * @param dateTime timestamp represented as {@link ZonedDateTime}
 * @param config {@link DarkSkyChannelConfiguration} instance
 * @return the modified timestamp/*from ww  w.j  a v  a  2  s.  c o  m*/
 */
private ZonedDateTime applyChannelConfig(ZonedDateTime dateTime, @Nullable DarkSkyChannelConfiguration config) {
    ZonedDateTime modifiedDateTime = dateTime;
    if (config != null) {
        if (config.getOffset() != 0) {
            if (logger.isTraceEnabled()) {
                logger.trace("Apply offset of {} min to timestamp '{}'.", config.getOffset(),
                        modifiedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
            }
            modifiedDateTime = modifiedDateTime.plusMinutes(config.getOffset());
        }
        long earliestInMinutes = config.getEarliestInMinutes();
        if (earliestInMinutes > 0) {
            ZonedDateTime earliestDateTime = modifiedDateTime.truncatedTo(ChronoUnit.DAYS)
                    .plusMinutes(earliestInMinutes);
            if (modifiedDateTime.isBefore(earliestDateTime)) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Use earliest timestamp '{}' instead of '{}'.",
                            earliestDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
                            modifiedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
                }
                return earliestDateTime;
            }
        }
        long latestInMinutes = config.getLatestInMinutes();
        if (latestInMinutes > 0) {
            ZonedDateTime latestDateTime = modifiedDateTime.truncatedTo(ChronoUnit.DAYS)
                    .plusMinutes(latestInMinutes);
            if (modifiedDateTime.isAfter(latestDateTime)) {
                if (logger.isTraceEnabled()) {
                    logger.trace("Use latest timestamp '{}' instead of '{}'.",
                            latestDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME),
                            modifiedDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
                }
                return latestDateTime;
            }
        }
    }
    return modifiedDateTime;
}