Example usage for java.time ZonedDateTime withHour

List of usage examples for java.time ZonedDateTime withHour

Introduction

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

Prototype

public ZonedDateTime withHour(int hour) 

Source Link

Document

Returns a copy of this ZonedDateTime with the hour-of-day altered.

Usage

From source file:Main.java

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

From source file:nu.yona.server.test.util.JUnitUtil.java

public static void skipBefore(String description, ZonedDateTime now, int hour, int minute) {
    assumeThat(description, now,// w  w  w .j  a v a  2  s  .c  o  m
            ZonedDateTimeMatchers.after(now.withHour(hour).withMinute(minute).withSecond(0)));
}

From source file:com.omertron.slackbot.functions.scheduler.AbstractBotTask.java

/**
 * Calculate the time between "now" and the execution time.
 *
 * @param targetHour/*from  w w  w.  ja v a2  s  . c  om*/
 * @param targetMin
 * @param targetSec
 * @return
 */
private long computeNextDelay(int targetHour, int targetMin, int targetSec) {
    ZonedDateTime zonedNow = localeDateTime();
    ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin).withSecond(targetSec)
            .withNano(0);

    if (zonedNow.compareTo(zonedNextTarget) >= 0) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
    }

    Duration duration = Duration.between(zonedNow, zonedNextTarget);

    // If we are scheduled within the next minute, then skip a day as we probably just ran fast
    if (duration.getSeconds() <= 60l) {
        zonedNextTarget = zonedNextTarget.plusDays(1);
        duration = Duration.between(zonedNow, zonedNextTarget);
    }

    return duration.getSeconds();
}

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

@Test
public void analyze_unorderedAppActivity_addedOrdered() {
    JUnitUtil.skipBefore("Skip shortly after midnignt", now(), 0, 10);
    String app = "Poker App";
    ZonedDateTime today = now().truncatedTo(ChronoUnit.DAYS);
    ZonedDateTime t1 = today.withHour(0).withMinute(0).withSecond(1);
    ZonedDateTime t2 = t1.plusSeconds(15);
    ZonedDateTime t3 = t2.plusSeconds(1);
    ZonedDateTime t4 = t3.plusMinutes(5);

    service.analyze(userAnonId, deviceAnonId, new AppActivitiesDto(now(), new AppActivitiesDto.Activity[] {
            new AppActivitiesDto.Activity(app, t3, t4), new AppActivitiesDto.Activity(app, t1, t2) }));

    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(t1));
    assertThat(payloads.get(0).endTime, equalTo(t2));
    assertThat(payloads.get(1).startTime, equalTo(t3));
    assertThat(payloads.get(1).endTime, equalTo(t4));
    verify(mockActivityUpdater, never()).updateTimeExistingActivity(any(), any());
    verify(mockActivityUpdater, never()).updateTimeLastActivity(any(), any(), any());
}

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  a2 s  .co m
    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_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 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;//ww  w .ja va2  s. co 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)))));
}

From source file:org.qcert.camp.translator.SemRule2CAMP.java

/**
 * Incomplete but evolving translation for object allocations involving temporal constructs
 * @param ast the object allocation (SemNewObject)
 * @return the translation/*from w w w  . j  a  va  2  s.  c o  m*/
 */
private CampPattern translateTemporalAllocation(SemNewObject ast) {
    List<SemValue> args = ast.getArguments();
    if (DATE_TYPE.equals(ast.getType().getDisplayName())) {
        Iterator<SemValue> argIter = args.iterator();
        ZonedDateTime translation = ZonedDateTime.ofInstant(Instant.EPOCH, ZoneId.systemDefault())
                .withYear(intFrom(argIter.next())).withMonth(intFrom(argIter.next()))
                .withDayOfMonth(intFrom(argIter.next()));
        if (argIter.hasNext())
            translation = translation.withHour(intFrom(argIter.next()));
        if (argIter.hasNext())
            translation = translation.withMinute(intFrom(argIter.next()));
        if (argIter.hasNext())
            translation = translation.withSecond(intFrom(argIter.next()));
        ConstPattern constant = new ConstPattern(translation.toString());
        return new UnaryPattern(UnaryOperator.ATimeFromString, constant);
    }
    if (TIME_TYPE.equals(ast.getType().getDisplayName()) && args.size() == 1) {
        long epochMilli = longFrom(args.get(0));
        LocalTime translation = ZonedDateTime.ofInstant(Instant.ofEpochMilli(epochMilli), ZoneOffset.UTC)
                .toLocalTime();
        // TODO this should really be a unique CAMP type corresponding to the TTRL type for LocalTimeComponentValue
        return new ConstPattern(translation.toString());
    }
    return notImplemented("Translation of temporal allocation: " + ast);
}