Example usage for java.time ZonedDateTime toLocalDate

List of usage examples for java.time ZonedDateTime toLocalDate

Introduction

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

Prototype

@Override 
public LocalDate toLocalDate() 

Source Link

Document

Gets the LocalDate part of this date-time.

Usage

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

@Test
public void getUserDayActivityOverviews_activityPresent_resultsWithActivity() {
    ZonedDateTime today = getDayStartTime(ZonedDateTime.now(userAnonZone));
    ZonedDateTime yesterday = today.minusDays(1);

    // gambling goal was created 2 weeks ago, see above
    // mock some activity on yesterday 20:58-21:00
    DayActivity yesterdayRecordedActivity = DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, yesterday.toLocalDate());
    Activity recordedActivity = Activity.createInstance(userAnonZone,
            yesterday.plusHours(20).plusMinutes(58).toLocalDateTime(),
            yesterday.plusHours(21).plusMinutes(00).toLocalDateTime(), Optional.empty());
    yesterdayRecordedActivity.addActivity(recordedActivity);
    Set<UUID> relevantGoalIds = userAnonEntity.getGoals().stream().map(Goal::getId).collect(Collectors.toSet());
    when(mockDayActivityRepository.findAll(userAnonId, relevantGoalIds, today.minusDays(2).toLocalDate(),
            today.plusDays(1).toLocalDate())).thenReturn(Arrays.asList(yesterdayRecordedActivity));

    Page<DayActivityOverviewDto<DayActivityDto>> dayOverviews = service.getUserDayActivityOverviews(userId,
            new PageRequest(0, 3));

    // assert that the right retrieve from database was done
    verify(mockDayActivityRepository, times(1)).findAll(userAnonId, relevantGoalIds,
            today.minusDays(2).toLocalDate(), today.plusDays(1).toLocalDate());

    // because the gambling goal was added with creation date two weeks ago, there are multiple days, equal to the limit of
    // our page request = 3
    assertThat(dayOverviews.getNumberOfElements(), equalTo(3));

    // get the current day (first item)
    DayActivityOverviewDto<DayActivityDto> dayOverview = dayOverviews.getContent().get(0);
    assertThat(dayOverview.getDayActivities().size(), equalTo(userAnonEntity.getGoals().size()));
    DayActivityDto dayActivityForGambling = dayOverview.getDayActivities().stream()
            .filter(a -> a.getGoalId().equals(gamblingGoal.getId())).findAny().get();
    assertThat(dayActivityForGambling.getStartTime(), equalTo(today));
    assertThat(dayActivityForGambling.getTotalActivityDurationMinutes().get(), equalTo(0));
    assertThat(dayActivityForGambling.getTotalMinutesBeyondGoal(), equalTo(0));

    // get yesterday, with recorded activity
    dayOverview = dayOverviews.getContent().get(1);
    assertThat(dayOverview.getDayActivities().size(), equalTo(1));
    dayActivityForGambling = dayOverview.getDayActivities().stream()
            .filter(a -> a.getGoalId().equals(gamblingGoal.getId())).findAny().get();
    assertThat(dayActivityForGambling.getStartTime(), equalTo(yesterday));
    assertThat(dayActivityForGambling.getTotalActivityDurationMinutes().get(), equalTo(2));
    assertThat(dayActivityForGambling.getTotalMinutesBeyondGoal(), equalTo(2));
}

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

@Test
public void getUserWeekActivityOverviews_activityPresent_resultsWithActivity() {
    ZonedDateTime today = getDayStartTime(ZonedDateTime.now(userAnonZone));

    // gambling goal was created 2 weeks ago, see above
    // mock some activity in previous week on Saturday 19:10-19:55
    WeekActivity previousWeekRecordedActivity = WeekActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today.minusWeeks(1)).toLocalDate());
    ZonedDateTime saturdayStartOfDay = getWeekStartTime(today).minusDays(1);
    DayActivity previousWeekSaturdayRecordedActivity = DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, saturdayStartOfDay.toLocalDate());
    Activity recordedActivity = Activity.createInstance(userAnonZone,
            saturdayStartOfDay.plusHours(19).plusMinutes(10).toLocalDateTime(),
            saturdayStartOfDay.plusHours(19).plusMinutes(55).toLocalDateTime(), Optional.empty());
    previousWeekSaturdayRecordedActivity.addActivity(recordedActivity);
    previousWeekRecordedActivity.addDayActivity(DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today).minusDays(7).toLocalDate()));
    previousWeekRecordedActivity.addDayActivity(DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today).minusDays(6).toLocalDate()));
    previousWeekRecordedActivity.addDayActivity(DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today).minusDays(5).toLocalDate()));
    previousWeekRecordedActivity.addDayActivity(DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today).minusDays(4).toLocalDate()));
    previousWeekRecordedActivity.addDayActivity(DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today).minusDays(3).toLocalDate()));
    previousWeekRecordedActivity.addDayActivity(DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, getWeekStartTime(today).minusDays(2).toLocalDate()));
    previousWeekRecordedActivity.addDayActivity(previousWeekSaturdayRecordedActivity);

    when(mockWeekActivityRepository.findAll(userAnonId, getWeekStartTime(today.minusWeeks(4)).toLocalDate(),
            getWeekStartTime(today).plusWeeks(1).toLocalDate()))
                    .thenReturn(new HashSet<>(Arrays.asList(previousWeekRecordedActivity)));

    Page<WeekActivityOverviewDto> weekOverviews = service.getUserWeekActivityOverviews(userId,
            new PageRequest(0, 5));

    // assert that the right retrieve from database was done
    verify(mockWeekActivityRepository, times(1)).findAll(userAnonId,
            getWeekStartTime(today.minusWeeks(4)).toLocalDate(),
            getWeekStartTime(today).plusWeeks(1).toLocalDate());

    // because the gambling goal was added with creation date two weeks ago, there are multiple weeks
    assertThat(weekOverviews.getNumberOfElements(), equalTo(3));

    // get the current week (first item)
    WeekActivityOverviewDto weekOverview = weekOverviews.getContent().get(0);
    assertThat(weekOverview.getWeekActivities().size(), equalTo(userAnonEntity.getGoals().size()));
    WeekActivityDto weekActivityForGambling = weekOverview.getWeekActivities().stream()
            .filter(a -> a.getGoalId().equals(gamblingGoal.getId())).findAny().get();
    assertThat(weekActivityForGambling.getStartTime(), equalTo(getWeekStartTime(today)));
    // TODO: mock day activity in this week?
    // int thisWeekNumberOfWeekDaysPast = today.getDayOfWeek() == DayOfWeek.SUNDAY ? 0 : today.getDayOfWeek().getValue();
    // assertThat(weekActivityForGambling.getDayActivities().size(), equalTo(1 + thisWeekNumberOfWeekDaysPast));
    //// always contains Sunday because it is the first day of the week
    // assertThat(weekActivityForGambling.getDayActivities(), hasKey(DayOfWeek.SUNDAY));

    // get the previous week, with recorded activity
    weekOverview = weekOverviews.getContent().get(1);
    assertThat(weekOverview.getWeekActivities().size(), equalTo(1));
    weekActivityForGambling = weekOverview.getWeekActivities().stream()
            .filter(a -> a.getGoalId().equals(gamblingGoal.getId())).findAny().get();
    assertThat(weekActivityForGambling.getStartTime(), equalTo(getWeekStartTime(today.minusWeeks(1))));
    assertThat(weekActivityForGambling.getDayActivities().size(), equalTo(7));
    DayActivityDto previousWeekSaturdayActivity = weekActivityForGambling.getDayActivities()
            .get(DayOfWeek.SATURDAY);
    assertThat(previousWeekSaturdayActivity.getTotalActivityDurationMinutes().get(), equalTo(45));
    assertThat(previousWeekSaturdayActivity.getTotalMinutesBeyondGoal(), equalTo(45));
    DayActivityDto previousWeekFridayActivity = weekActivityForGambling.getDayActivities()
            .get(DayOfWeek.FRIDAY);
    assertThat(previousWeekFridayActivity.getTotalActivityDurationMinutes().get(), equalTo(0));

    // get the week the gambling goal was created
    weekOverview = weekOverviews.getContent().get(2);
    assertThat(weekOverview.getWeekActivities().size(), equalTo(1));
    weekActivityForGambling = weekOverview.getWeekActivities().stream()
            .filter(a -> a.getGoalId().equals(gamblingGoal.getId())).findAny().get();
    assertThat(weekActivityForGambling.getStartTime(), equalTo(getWeekStartTime(today.minusWeeks(2))));
    // TODO: mock day activity in this week?
    // int expectedNumberOfWeekDaysRecorded = gamblingGoal.getCreationTime().getDayOfWeek() == DayOfWeek.SUNDAY ? 7
    // : 7 - gamblingGoal.getCreationTime().getDayOfWeek().getValue();
    // assertThat(weekActivityForGambling.getDayActivities().size(), equalTo(expectedNumberOfWeekDaysRecorded));
    //// always contains Saturday because it is the last day of the week
    // assertThat(weekActivityForGambling.getDayActivities(), hasKey(DayOfWeek.SATURDAY));
}

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

@Test
public void getUserDayActivityDetail_activityPresent_resultWithActivity() {
    ZonedDateTime today = getDayStartTime(ZonedDateTime.now(userAnonZone));
    ZonedDateTime yesterday = today.minusDays(1);

    LocalTime activityStartTimeOnDay = LocalTime.parse("20:14:57");
    LocalTime activityEndTimeOnDay = LocalTime.parse("20:21:00");

    int hour = 20;
    int[] expectedSpread = getEmptySpread();
    expectedSpread[hour * 4] = 1;/*from   www  .  j  a v a  2 s.com*/
    expectedSpread[hour * 4 + 1] = 6;

    // gambling goal was created 2 weeks ago, see above
    // mock some activity on yesterday
    DayActivity yesterdayRecordedActivity = DayActivity.createInstance(userAnonEntity, gamblingGoal,
            userAnonZone, yesterday.toLocalDate());
    ZonedDateTime activityStartTime = yesterday.withHour(activityStartTimeOnDay.getHour())
            .withMinute(activityStartTimeOnDay.getMinute()).withSecond(activityStartTimeOnDay.getSecond());
    ZonedDateTime activityEndTime = yesterday.withHour(activityEndTimeOnDay.getHour())
            .withMinute(activityEndTimeOnDay.getMinute()).withSecond(activityEndTimeOnDay.getSecond());
    Activity recordedActivity = Activity.createInstance(userAnonZone, activityStartTime.toLocalDateTime(),
            activityEndTime.toLocalDateTime(), Optional.empty());
    yesterdayRecordedActivity.addActivity(recordedActivity);
    when(mockDayActivityRepository.findOne(userAnonId, yesterday.toLocalDate(), gamblingGoal.getId()))
            .thenReturn(yesterdayRecordedActivity);

    DayActivityDto activityDay = service.getUserDayActivityDetail(userId, yesterday.toLocalDate(),
            gamblingGoal.getId());

    verify(mockDayActivityRepository, times(1)).findOne(userAnonId, yesterday.toLocalDate(),
            gamblingGoal.getId());
    assertThat(activityDay.getSpread(), equalTo(Arrays.asList(ArrayUtils.toObject((expectedSpread)))));
}

From source file:org.tightblog.ui.restapi.WeblogEntryController.java

@GetMapping(value = "/{id}")
public WeblogEntry getWeblogEntry(@PathVariable String id, Principal p, HttpServletResponse response)
        throws ServletException {
    try {/*  w  w w .ja  v  a2s .c o  m*/
        WeblogEntry entry = weblogEntryRepository.findByIdOrNull(id);
        if (entry != null) {
            Weblog weblog = entry.getWeblog();
            if (userManager.checkWeblogRole(p.getName(), weblog, WeblogRole.EDIT_DRAFT)) {
                entry.setCommentsUrl(urlService.getCommentManagementURL(weblog.getId(), entry.getId()));
                entry.setPermalink(urlService.getWeblogEntryURL(entry));
                entry.setPreviewUrl(urlService.getWeblogEntryDraftPreviewURL(entry));

                if (entry.getPubTime() != null) {
                    log.debug("entry pubtime is {}", entry.getPubTime());
                    ZonedDateTime zdt = entry.getPubTime().atZone(entry.getWeblog().getZoneId());
                    entry.setHours(zdt.getHour());
                    entry.setMinutes(zdt.getMinute());
                    entry.setCreator(null);
                    entry.setDateString(pubDateFormat.format(zdt.toLocalDate()));
                }

                response.setStatus(HttpServletResponse.SC_OK);
                return entry;
            } else {
                response.setStatus(HttpServletResponse.SC_FORBIDDEN);
            }
        } else {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (Exception e) {
        log.error("Error retrieving entry {}", id, e);
        throw new ServletException(e.getMessage());
    }
    return null;
}