Java Code Examples for java.time.LocalDateTime#plusSeconds()

The following examples show how to use java.time.LocalDateTime#plusSeconds() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: smart-admin   File: LoginTokenService.java    License: MIT License 6 votes vote down vote up
/**
 * 功能描述: 生成JWT TOKEN
 *
 * @param employeeDTO
 * @return
 * @auther yandanyang
 * @date 2018/9/12 0012 上午 10:08
 */
public String generateToken(EmployeeDTO employeeDTO) {
    Long id = employeeDTO.getId();
    /**将token设置为jwt格式*/
    String baseToken = UUID.randomUUID().toString();
    LocalDateTime localDateTimeNow = LocalDateTime.now();
    LocalDateTime localDateTimeExpire = localDateTimeNow.plusSeconds(EXPIRE_SECONDS);
    Date from = Date.from(localDateTimeNow.atZone(ZoneId.systemDefault()).toInstant());
    Date expire = Date.from(localDateTimeExpire.atZone(ZoneId.systemDefault()).toInstant());

    Claims jwtClaims = Jwts.claims().setSubject(baseToken);
    jwtClaims.put(CLAIM_ID_KEY, id);
    String compactJws = Jwts.builder().setClaims(jwtClaims).setNotBefore(from).setExpiration(expire).signWith(SignatureAlgorithm.HS512, jwtKey).compact();

    EmployeeBO employeeBO = employeeService.getById(id);
    RequestTokenBO tokenBO = new RequestTokenBO(employeeBO);

    return compactJws;
}
 
Example 2
Source Project: morpheus-core   File: RangeSplitTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test(dataProvider = "directions")
public void testSplitLocalDateTime(boolean ascending) {
    final Duration step = Duration.ofSeconds(1);
    final LocalDateTime start = LocalDateTime.of(2014, 1, 1, 14, 15, 20, 0);
    final LocalDateTime end = start.plusSeconds(10000000 * (ascending ? 1 : -1));
    final Range<LocalDateTime> range = Range.of(start, end, step);
    final List<Range<LocalDateTime>> segments = range.split(100);
    Assert.assertTrue(segments.size() > 1, "There are multiple segments");
    for (int i=1; i<segments.size(); ++i) {
        final Range<LocalDateTime> prior = segments.get(i-1);
        final Range<LocalDateTime> next = segments.get(i);
        Assert.assertEquals(prior.end(), next.start(), "Date connect as expect");
        if (i == 1) Assert.assertEquals(prior.start(), range.start(), "First segment start matches range start");
        if (i == segments.size()-1) {
            Assert.assertEquals(next.end(), range.end(), "Last segment end matches range end");
        }
    }
}
 
Example 3
public TimeframeInterval createTimeframeInterval(LocalDateTime now, Integer evId, Integer socCurrent, Integer socRequested,
                                         LocalDateTime chargeEnd) {
    SocRequest request = new SocRequest();
    request.setEvId(evId);
    request.setSoc(socRequested);
    request.setSocInitial(socCurrent);
    request.setEnabled(true);

    if(chargeEnd == null) {
        Integer energy = request.calculateEnergy(getVehicle(evId));
        chargeEnd = now.plusSeconds(calculateChargeSeconds(getVehicle(evId), energy));
    }

    Interval interval = new Interval(now, chargeEnd);

    TimeframeInterval timeframeInterval = new TimeframeInterval(interval, request);

    return timeframeInterval;
}
 
Example 4
private void addAuthentication(HttpServletResponse response, String username, String jti) {
    LocalDateTime now = LocalDateTime.now();
    final LocalDateTime expirationDate = now.plusSeconds(jwtProperty.getExpirationTime());

    if (jti == null) {
        jti = UUID.randomUUID().toString();
        while (credentialStateService.findOne(jti) != null) {
            jti = UUID.randomUUID().toString();
        }

        credentialStateService.save(jti, username, CredentialStateEnum.LOGIN);
    }

    String jwt = Jwts.builder()
            .setSubject(username)
            .setId(jti)
            .setIssuedAt(Date.from(now.atZone(ZoneId.systemDefault()).toInstant()))
            .setExpiration(Date.from(expirationDate.atZone(ZoneId.systemDefault()).toInstant()))
            .signWith(SignatureAlgorithm.HS512, jwtProperty.getSecret())
            .compact();

    response.setHeader(HEIMDALL_AUTHORIZATION_NAME, jwt);
}
 
Example 5
@Test
public void test_plusSeconds_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    int hour = 0;
    int min = 0;
    int sec = 0;

    for (int i = 0; i < 3700; i++) {
        t = t.plusSeconds(1);
        sec++;
        if (sec == 60) {
            min++;
            sec = 0;
        }
        if (min == 60) {
            hour++;
            min = 0;
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
    }
}
 
Example 6
@Test
public void test_toEpochSecond_afterEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), i);
        ldt = ldt.plusSeconds(1);
    }
}
 
Example 7
@Test
public void test_plusSeconds_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    int hour = 0;
    int min = 0;
    int sec = 0;

    for (int i = 0; i < 3700; i++) {
        t = t.plusSeconds(1);
        sec++;
        if (sec == 60) {
            min++;
            sec = 0;
        }
        if (min == 60) {
            hour++;
            min = 0;
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
    }
}
 
Example 8
@Test(dataProvider="plusSeconds_fromZero")
public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.plusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 9
@Test(dataProvider="plusSeconds_fromZero")
public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.plusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 10
@Test
public void test_toEpochSecond_afterEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), i);
        ldt = ldt.plusSeconds(1);
    }
}
 
Example 11
@Test
public void test_plusSeconds_one() {
    LocalDateTime t = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDate d = t.toLocalDate();

    int hour = 0;
    int min = 0;
    int sec = 0;

    for (int i = 0; i < 3700; i++) {
        t = t.plusSeconds(1);
        sec++;
        if (sec == 60) {
            min++;
            sec = 0;
        }
        if (min == 60) {
            hour++;
            min = 0;
        }

        assertEquals(t.toLocalDate(), d);
        assertEquals(t.getHour(), hour);
        assertEquals(t.getMinute(), min);
        assertEquals(t.getSecond(), sec);
    }
}
 
Example 12
@Test
public void test_toEpochSecond_afterEpoch() {
    LocalDateTime ldt = LocalDateTime.of(1970, 1, 1, 0, 0).plusHours(1);
    for (int i = 0; i < 100000; i++) {
        ZonedDateTime a = ZonedDateTime.of(ldt, ZONE_PARIS);
        assertEquals(a.toEpochSecond(), i);
        ldt = ldt.plusSeconds(1);
    }
}
 
Example 13
public LocalDateTime updateSecond(LocalDateTime datetime, String second) {
  if (!Strings.isNullOrEmpty(second)) {
    Matcher matcher = patternMonth.matcher(second);
    if (matcher.find()) {
      Long seconds = Long.parseLong(matcher.group());
      if (second.startsWith("+")) datetime = datetime.plusSeconds(seconds);
      else if (second.startsWith("-")) datetime = datetime.minusSeconds(seconds);
      else datetime = datetime.withSecond(seconds.intValue());
    }
  }
  return datetime;
}
 
Example 14
Source Project: tutorials   File: DateDiffUnitTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenTwoDateTimesInJava8_whenDifferentiatingInSecondsUsingUntil_thenWeGetTen() {
    LocalDateTime now = LocalDateTime.now();
    LocalDateTime tenSecondsLater = now.plusSeconds(10);

    long diff = now.until(tenSecondsLater, ChronoUnit.SECONDS);

    assertEquals(diff, 10);
}
 
Example 15
@Test(dataProvider="plusSeconds_fromZero")
public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.plusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 16
private String getAccessToken() throws URISyntaxException, IOException {
  LocalDateTime now = LocalDateTime.now();
  if (tokenInfo == null || tokenInfo.validTill.minusSeconds(60).isBefore(now)) {
    Token token = generateToken();
    if (token.access_token == null) {
      throw new IOException("Error obtaining access token");
    }
    TokenInfo ti = new TokenInfo();
    ti.token = token;
    ti.validTill = now.plusSeconds(token.expires_in);
    tokenInfo = ti;
  }
  return tokenInfo.token.access_token;
}
 
Example 17
@Test(dataProvider="plusSeconds_fromZero")
public void test_plusSeconds_fromZero(int seconds, LocalDate date, int hour, int min, int sec) {
    LocalDateTime base = TEST_2007_07_15_12_30_40_987654321.with(LocalTime.MIDNIGHT);
    LocalDateTime t = base.plusSeconds(seconds);

    assertEquals(date, t.toLocalDate());
    assertEquals(hour, t.getHour());
    assertEquals(min, t.getMinute());
    assertEquals(sec, t.getSecond());
}
 
Example 18
/**
 * Update time tick values
 */
private void updateTimeTickValues() {
    LocalDateTime sdate = JDateUtil.fromOADate(this.getMinValue());
    LocalDateTime edate = JDateUtil.fromOADate(this.getMaxValue());
    LocalDateTime ssdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), sdate.getDayOfMonth(),
            sdate.getHour(), sdate.getMinute(), sdate.getSecond());

    List<LocalDateTime> dates = new ArrayList<>();
    switch (this.timeUnit) {
        case YEAR:
            sdate = LocalDateTime.of(sdate.getYear(), 1, 1, 0, 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.withYear(sdate.getYear() + 1);
                dates.add(sdate);
            }
            break;
        case MONTH:
            sdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), 1, 0, 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusMonths(1);
                if (!sdate.isBefore(ssdate)) {
                    dates.add(sdate);
                }
            }
            break;
        case DAY:
            sdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), sdate.getDayOfMonth(),
                    0, 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusDays(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
        case HOUR:
            sdate = LocalDateTime.of(sdate.getYear(), sdate.getMonthValue(), sdate.getDayOfMonth(),
                    sdate.getHour(), 0, 0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusHours(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
        case MINUTE:
            sdate = ssdate.withSecond(0);
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusMinutes(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
        case SECOND:
            if (!sdate.isBefore(ssdate)) {
                dates.add(sdate);
            }
            while (!sdate.isAfter(edate)) {
                sdate = sdate.plusSeconds(1);
                if (sdate.isBefore(edate)) {
                    dates.add(sdate);
                }
            }
            break;
    }

    double[] tvs = new double[dates.size()];
    for (int i = 0; i < dates.size(); i++) {
        tvs[i] = JDateUtil.toOADate(dates.get(i));
    }
    this.setTickValues(tvs);
}
 
Example 19
Source Project: datacollector   File: OracleCDCSource.java    License: Apache License 2.0 4 votes vote down vote up
private LocalDateTime getEndTimeForStartTime(LocalDateTime startTime) {
  // Ensure the LogMiner window does not span further than the current time in database.
  LocalDateTime dbTime = nowAtDBTz();
  LocalDateTime endTime = startTime.plusSeconds(configBean.logminerWindow);
  return endTime.isAfter(dbTime) ? dbTime : endTime;
}
 
Example 20
Source Project: java-trader   File: ExchangeableUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static LocalDateTime resolveTime(ExchangeableTradingTimes tradingTimes, MarketType currSeg, String timeExpr) {
    LocalDateTime result = null;

    if ( timeExpr.startsWith("$")) {
        LocalDateTime timeBase = null;
        Map<String, LocalDateTime> segTimes = getPredefinedTimes(tradingTimes, currSeg);
        for(String segKey:segTimes.keySet()) {
            if ( timeExpr.toLowerCase().startsWith(segKey.toLowerCase())) {
                timeBase = segTimes.get(segKey);
                timeExpr = timeExpr.substring(segKey.length()).toLowerCase();
                break;
            }
        }
        if ( timeBase!=null ) {
            int timeAdjust=1;
            if ( timeExpr.startsWith("a")||timeExpr.startsWith("+")) {
                //after
                timeAdjust=1;
                timeExpr = timeExpr.substring(1);
            }else if ( timeExpr.startsWith("b")||timeExpr.startsWith("-")) {
                timeAdjust=-1;
                timeExpr = timeExpr.substring(1);
            }
            long seconds = ConversionUtil.str2seconds(timeExpr);
            result = timeBase.plusSeconds(seconds*timeAdjust);
        }
    }else {
        LocalDateTime times[] = tradingTimes.getMarketTimes();
        result = DateUtil.str2localdatetime(timeExpr);
        if ( result==null ) {
            LocalTime time = DateUtil.str2localtime(timeExpr);
            for(int i=0;i<times.length;i+=2) {
                LocalDateTime t0 = times[i];
                LocalDateTime t1 = times[i+1];
                if ( t0.toLocalDate().equals(t1.toLocalDate())) {
                    //不存在跨天
                    if ( t0.toLocalTime().compareTo(time)<=0 && t1.toLocalTime().compareTo(time)>=0 ) {
                        result = t0.toLocalDate().atTime(time);
                    }
                } else {
                    //跨天
                    if ( t0.toLocalTime().compareTo(time)<=0 ) {
                        result = t0.toLocalDate().atTime(time);
                    }else if ( t1.toLocalTime().compareTo(time)>=0 ) {
                        result = t1.toLocalDate().atTime(time);
                    }
                }
            }
        }
    }
    return result;
}