Example usage for java.time ZonedDateTime minusWeeks

List of usage examples for java.time ZonedDateTime minusWeeks

Introduction

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

Prototype

public ZonedDateTime minusWeeks(long weeks) 

Source Link

Document

Returns a copy of this ZonedDateTime with the specified number of weeks subtracted.

Usage

From source file:Main.java

public static void main(String[] args) {
    ZonedDateTime dateTime = ZonedDateTime.now();
    ZonedDateTime n = dateTime.minusWeeks(1234);
    System.out.println(n);//from  ww  w.  j  a v a  2s.co  m
}

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));
}