Example usage for java.time.temporal ChronoUnit HOURS

List of usage examples for java.time.temporal ChronoUnit HOURS

Introduction

In this page you can find the example usage for java.time.temporal ChronoUnit HOURS.

Prototype

ChronoUnit HOURS

To view the source code for java.time.temporal ChronoUnit HOURS.

Click Source Link

Document

Unit that represents the concept of an hour.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalTime localTime = LocalTime.of(11, 20, 50);
    System.out.println(localTime.plus(3, ChronoUnit.HOURS));
    System.out.println(localTime.plus(Duration.ofDays(3))); //11:20:50
    try {//from  w w w  . j a  v a  2  s. c om
        System.out.println(localTime.plus(Period.ofDays(3)));
    } catch (UnsupportedTemporalTypeException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] argv) {
    ZoneId INDIA = ZoneId.of("Asia/Kolkata");

    LocalDateTime utah = LocalDateTime.now();
    LocalDateTime india = LocalDateTime.now(INDIA);
    long betweenHours = ChronoUnit.HOURS.between(utah, india);
    long betweenMins = ChronoUnit.MINUTES.between(utah, india);
    System.out.println("betweenHours = " + betweenHours);
    System.out.println("betweenMins = " + (betweenMins / 60.0));
}

From source file:Main.java

public static void main(String... args) {
    ZoneId zone1 = ZoneId.of("Europe/Berlin");
    ZoneId zone2 = ZoneId.of("Brazil/East");

    LocalTime now1 = LocalTime.now(zone1);
    LocalTime now2 = LocalTime.now(zone2);

    System.out.println(now1);//from w  w  w  .j a va  2  s.c o  m
    System.out.println(now2);

    long hoursBetween = ChronoUnit.HOURS.between(now1, now2);
    long minutesBetween = ChronoUnit.MINUTES.between(now1, now2);
    System.out.println(hoursBetween);
    System.out.println(minutesBetween);

}

From source file:Main.java

public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 7);
    LocalDate ld2 = LocalDate.of(2014, Month.MAY, 21);
    long days = ChronoUnit.DAYS.between(ld1, ld2);
    System.out.println(days);/*from w  ww  .  j a v a2  s  .  co m*/

    LocalTime lt1 = LocalTime.of(6, 0);
    LocalTime lt2 = LocalTime.of(9, 30);
    long hours = ChronoUnit.HOURS.between(lt1, lt2);
    System.out.println(hours);
    long minutes = ChronoUnit.MINUTES.between(lt1, lt2);
    System.out.println(minutes);
}

From source file:Main.java

public static void main(String[] args) {
    LocalDate ld1 = LocalDate.of(2014, Month.JANUARY, 7);
    LocalDate ld2 = LocalDate.of(2014, Month.MAY, 18);

    LocalTime lt1 = LocalTime.of(7, 0);
    LocalTime lt2 = LocalTime.of(9, 30);

    long days = ld1.until(ld2, ChronoUnit.DAYS);
    System.out.println(days);//from  www. jav  a2s  .  c o  m
    long hours = lt1.until(lt2, ChronoUnit.HOURS);
    System.out.println(hours);
    long minutes = lt1.until(lt2, ChronoUnit.MINUTES);
    System.out.println(minutes);
}

From source file:Main.java

public static ChronoUnit convert(TimeUnit tu) {
    if (tu == null) {
        return null;
    }//from w  w  w  .  ja  v a2  s  . c  om
    switch (tu) {
    case DAYS:
        return ChronoUnit.DAYS;
    case HOURS:
        return ChronoUnit.HOURS;
    case MINUTES:
        return ChronoUnit.MINUTES;
    case SECONDS:
        return ChronoUnit.SECONDS;
    case MICROSECONDS:
        return ChronoUnit.MICROS;
    case MILLISECONDS:
        return ChronoUnit.MILLIS;
    case NANOSECONDS:
        return ChronoUnit.NANOS;
    default:
        assert false : "there are no other TimeUnit ordinal values";
        return null;
    }
}

From source file:dk.dbc.rawrepo.oai.ResumptionToken.java

/**
 * Encode a json object wit a timeout/*from  ww  w .  j  av a2 s  .co m*/
 *
 * @param obj        json
 * @param validHours timeout
 * @return base64 encoded string
 */
public static String encode(ObjectNode obj, int validHours) {
    if (obj == null) {
        return null;
    }
    long epoch = Instant.now().plus(validHours, ChronoUnit.HOURS).toEpochMilli() / 1000L;
    String format = String.format("%x%s", epoch, obj.toString());
    return PackText.encode(format);
}

From source file:io.cfp.auth.service.TokenService.java

/**
 * Create a Token for a user// w  ww  . jav a 2 s.co  m
  * @param email Email of the token owner
  * @param isSuperAdmin True if the user is a super admin
  * @return Token value
  */
public String create(String email, boolean isSuperAdmin) {
    JwtBuilder builder = Jwts.builder().setSubject(email)
            .setExpiration(Date.from(Instant.now().plus(TOKEN_EXPIRATION, ChronoUnit.HOURS)))
            .signWith(SignatureAlgorithm.HS512, signingKey);

    if (isSuperAdmin) {
        builder.claim("superAdmin", true);
    }

    return builder.compact();
}

From source file:dk.dbc.rawrepo.oai.ResumptionToken.java

/**
 * Construct a ResumptionToken from a json object
 *
 * @param obj        json value/*from   ww w  .j a  v  a2 s.co  m*/
 * @param validHours timeout
 * @return Resumption token to add to an oaipmh request
 */
public static ResumptionTokenType toToken(ObjectNode obj, int validHours) {
    if (obj == null) {
        return null;
    }
    ResumptionTokenType token = OBJECT_FACTORY.createResumptionTokenType();

    Instant timeout = Instant.now().plus(validHours, ChronoUnit.HOURS).truncatedTo(ChronoUnit.SECONDS);

    XMLGregorianCalendar date = OAIResource.gregorianTimestamp(timeout);

    token.setExpirationDate(date);

    token.setValue(encode(obj, validHours));
    return token;
}

From source file:com.joyent.manta.client.MantaClientSigningIT.java

@Test
public final void testCanCreateSignedGETUriFromPath() throws IOException {
    if (config.isClientEncryptionEnabled()) {
        throw new SkipException("Signed URLs are not decrypted by the client");
    }/* ww w .  ja v a2 s.c om*/

    final String name = UUID.randomUUID().toString();
    final String path = testPathPrefix + name;

    mantaClient.put(path, TEST_DATA);

    // This will throw an error if the newly inserted object isn't present
    mantaClient.head(path);

    Instant expires = Instant.now().plus(1, ChronoUnit.HOURS);
    URI uri = mantaClient.getAsSignedURI(path, "GET", expires);

    HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();

    try (InputStream is = connection.getInputStream()) {
        connection.setReadTimeout(3000);
        connection.connect();

        if (connection.getResponseCode() != 200) {
            Assert.fail(IOUtils.toString(connection.getErrorStream(), Charset.defaultCharset()));
        }

        String actual = IOUtils.toString(is, Charset.defaultCharset());

        Assert.assertEquals(actual, TEST_DATA);
    } finally {
        connection.disconnect();
    }
}