Example usage for java.time ZonedDateTime minusDays

List of usage examples for java.time ZonedDateTime minusDays

Introduction

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

Prototype

public ZonedDateTime minusDays(long days) 

Source Link

Document

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

Usage

From source file:Main.java

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

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

private ZonedDateTime getWeekStartTime(ZonedDateTime dateTime) {
    ZonedDateTime dateAtStartOfDay = getDayStartTime(dateTime);
    switch (dateAtStartOfDay.getDayOfWeek()) {
    case SUNDAY:// w  ww . j  ava  2  s. c o m
        return dateAtStartOfDay;
    default:
        return dateAtStartOfDay.minusDays(dateAtStartOfDay.getDayOfWeek().getValue());
    }
}

From source file:io.stallion.jobs.Schedule.java

/**
 * Runs the given days of the week, every other week.
 *
 * @param startingAt// w w w  . j  av  a 2s.  c om
 * @param days
 * @return
 */
public Schedule daysBiweekly(Long startingAt, DayOfWeek... days) {
    this._days.verifyAndUpdateUnset();
    this._days.setIntervalType(Days.IntervalType.BIWEEKLY_DAY_OF_WEEK);
    for (DayOfWeek day : days) {
        this._days.add(day.getValue());
    }
    ZonedDateTime startingWeek = ZonedDateTime.ofInstant(Instant.ofEpochMilli(startingAt), ZoneId.of("UTC"));
    // Get the Monday 12PM of that week
    startingWeek = startingWeek.minusDays(startingWeek.getDayOfWeek().getValue() - 1).withSecond(0).withHour(12)
            .withMinute(0).withNano(0);
    this._days.setStartingDate(startingWeek);
    return this;
}

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

@Test
public void addActivity_appActivityOnNewDay_newDayActivityButNoGoalConflictMessageCreated() {
    ZonedDateTime today = now().truncatedTo(ChronoUnit.DAYS);
    // mock earlier activity at yesterday 23:59:58,
    // add new activity at today 00:00:01
    ZonedDateTime existingActivityTime = today.minusDays(1).withHour(23).withMinute(59).withSecond(58);

    DayActivity existingDayActivity = mockExistingActivity(gamblingGoal, existingActivityTime);
    ActivityDto lastRegisteredActivity = ActivityDto.createInstance(existingDayActivity.getActivities().get(0));

    ZonedDateTime startTime = today.withHour(0).withMinute(0).withSecond(1);
    ZonedDateTime endTime = today.withHour(0).withMinute(10);

    service.addActivity(userAnonEntity, createPayload(startTime, endTime), GoalDto.createInstance(gamblingGoal),
            Optional.of(lastRegisteredActivity));

    verifyNoGoalConflictMessagesCreated();

    // Verify there are now two day activities
    verify(mockUserAnonymizedService, atLeastOnce()).updateUserAnonymized(userAnonEntity);
    List<WeekActivity> weekActivities = gamblingGoal.getWeekActivities();
    assertThat("One week activity created", weekActivities.size(), equalTo(1));
    List<DayActivity> dayActivities = weekActivities.get(0).getDayActivities();
    assertThat("Two day activities created", dayActivities.size(), equalTo(2));
    DayActivity yesterdaysDayActivity;/*from  w  w  w. j  a  v  a2s  . com*/
    DayActivity todaysDayActivity;
    if (dayActivities.get(0).getStartDate().isBefore(dayActivities.get(1).getStartDate())) {
        yesterdaysDayActivity = dayActivities.get(0);
        todaysDayActivity = dayActivities.get(1);
    } else {
        yesterdaysDayActivity = dayActivities.get(1);
        todaysDayActivity = dayActivities.get(0);
    }

    // Double check yesterday's activity
    List<Activity> yesterdaysActivities = yesterdaysDayActivity.getActivities();
    assertThat("One activity created for yesterday", yesterdaysActivities.size(), equalTo(1));
    Activity yesterdaysActivity = yesterdaysActivities.get(0);
    assertThat("Expect right goal set to yesterday's activity", yesterdaysActivity.getActivityCategory(),
            equalTo(gamblingGoal.getActivityCategory()));

    // Verify one activity was created, with the right goal
    List<Activity> activities = todaysDayActivity.getActivities();
    assertThat("One activity created", activities.size(), equalTo(1));
    Activity activity = activities.get(0);
    assertThat("Expect right goal set to activity", activity.getActivityCategory(),
            equalTo(gamblingGoal.getActivityCategory()));

    assertThat("Expect new day", todaysDayActivity, not(equalTo(existingDayActivity)));
    assertThat("Expect right date", todaysDayActivity.getStartDate(), equalTo(today.toLocalDate()));
    assertThat("Expect activity added", todaysDayActivity.getLastActivity(deviceAnonId), notNullValue());
    assertThat("Expect matching start time", todaysDayActivity.getLastActivity(deviceAnonId).getStartTime(),
            equalTo(startTime.toLocalDateTime()));
    assertThat("Expect matching end time", todaysDayActivity.getLastActivity(deviceAnonId).getEndTime(),
            equalTo(endTime.toLocalDateTime()));

    // Verify that there is an activity cached
    verify(mockAnalysisEngineCacheService, atLeastOnce()).updateLastActivityForUser(eq(userAnonId),
            eq(deviceAnonId), eq(gamblingGoal.getId()), any());
}

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

@Test
public void analyze_appActivityPreviousDayPrecedingCachedDayActivity_addActivity() {
    ZonedDateTime now = now();
    ZonedDateTime yesterdayNoon = now.minusDays(1).withHour(12).withMinute(0).withSecond(0);

    mockExistingActivity(gamblingGoal, now);

    ZonedDateTime startTime = yesterdayNoon;
    ZonedDateTime endTime = yesterdayNoon.plusMinutes(10);

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

    // Verify that a database lookup was done for yesterday
    verify(mockDayActivityRepository).findOne(userAnonId, yesterdayNoon.toLocalDate(), gamblingGoal.getId());

    verify(mockActivityUpdater).addActivity(any(), any(), eq(GoalDto.createInstance(gamblingGoal)), any());
    verify(mockActivityUpdater, never()).updateTimeExistingActivity(any(), any());
    verify(mockActivityUpdater, never()).updateTimeLastActivity(any(), any(), any());
}

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:nu.yona.server.analysis.service.AnalysisEngineServiceTest.java

@Test
public void analyze_appActivityOnNewDay_addActivity() {
    JUnitUtil.skipBefore("Skip shortly after midnight", now(), 0, 15);
    ZonedDateTime today = now().truncatedTo(ChronoUnit.DAYS);
    // mock earlier activity at yesterday 23:59:58,
    // add new activity at today 00:00:01
    ZonedDateTime existingActivityTime = today.minusDays(1).withHour(23).withMinute(59).withSecond(58);

    mockExistingActivity(gamblingGoal, existingActivityTime);

    ZonedDateTime startTime = today.withHour(0).withMinute(0).withSecond(1);
    ZonedDateTime endTime = today.withHour(0).withMinute(10);

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

    verify(mockActivityUpdater).addActivity(any(), any(), eq(GoalDto.createInstance(gamblingGoal)), any());
    verify(mockActivityUpdater, never()).updateTimeExistingActivity(any(), any());
    verify(mockActivityUpdater, never()).updateTimeLastActivity(any(), any(), any());
}

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 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;//w  w w.  jav a  2  s  . c  o m
    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)))));
}