Example usage for java.util TimeZone toZoneId

List of usage examples for java.util TimeZone toZoneId

Introduction

In this page you can find the example usage for java.util TimeZone toZoneId.

Prototype

public ZoneId toZoneId() 

Source Link

Document

Converts this TimeZone object to a ZoneId .

Usage

From source file:com.example.app.support.AppUtil.java

/**
 * Convert the given Date to a ZonedDateTime at the given TimeZone
 *
 * @param date the Date//from   w  w w  . j av a 2s .  com
 * @param zone the TimeZone to convert to
 *
 * @return a ZonedDateTime that represents the same instant as the Date, at the TimeZone specified.
 */
@Nullable
public static ZonedDateTime toZonedDateTime(@Nullable Date date, @Nullable TimeZone zone) {
    if (date == null || zone == null)
        return null;
    return ZonedDateTime.ofInstant(date.toInstant(), zone.toZoneId());
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Get a ZonedDateTime for comparison on membership dates
 *
 * @param zone the TimeZone//from   w w  w  .  j  av a  2s  .c om
 *
 * @return the ZonedDateTime
 */
public static ZonedDateTime getZonedDateTimeForComparison(TimeZone zone) {
    ZonedDateTime dt = ZonedDateTime.now(zone.toZoneId());
    dt = dt.plus(1L, ChronoUnit.HOURS);
    dt = dt.truncatedTo(ChronoUnit.HOURS);
    return dt;
}

From source file:com.example.app.support.AppUtil.java

/**
 * Convert the given Date from UTC to a ZonedDateTime at the given TimeZone
 *
 * @param date the UTC date/*from  w w  w  .  j a v a  2s  .  c o m*/
 * @param zone the TimeZone to convert the time to
 *
 * @return a ZonedDateTime that represents the same instant as the UTC date, but at the given TimeZone.
 */
@Nullable
public static ZonedDateTime convertFromPersisted(@Nullable Date date, @Nullable TimeZone zone) {
    if (date == null || zone == null)
        return null;
    ZonedDateTime from = ZonedDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC);
    return from.withZoneSameInstant(zone.toZoneId());
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Convert the given Date from UTC to a ZonedDateTime at the given TimeZone
 *
 * @param date the UTC date//  w  w  w .j  av a  2s .  c  om
 * @param zone the TimeZone to convert the time to
 *
 * @return a ZonedDateTime that represents the same instant as the UTC date, but at the given TimeZone.
 */
@Nullable
@Contract(value = "null,_->null;_,null->null;!null,!null->!null", pure = true)
public static ZonedDateTime convertFromPersisted(@Nullable Date date, @Nullable TimeZone zone) {
    if (date == null || zone == null)
        return null;
    ZonedDateTime from = ZonedDateTime.ofInstant(date.toInstant(), ZoneOffset.UTC);
    return from.withZoneSameInstant(zone.toZoneId());
}

From source file:alfio.repository.EventRepositoryTest.java

@Test
public void testSQLInsertedDatesRespectTheirTimeZone() throws Exception {
    //these are the values of what we have inserted in the SQL insert script
    TimeZone eventTimeZone = TimeZone.getTimeZone("America/New_York");
    ZoneId eventZoneId = eventTimeZone.toZoneId();
    ZonedDateTime beginEventDate = ZonedDateTime.of(2015, 10, 10, 0, 0, 0, 0, eventZoneId);
    ZonedDateTime endEventDate = ZonedDateTime.of(2015, 10, 10, 23, 59, 0, 0, eventZoneId);

    Event e = eventRepository.findById(0);
    assertNotNull("Event not found in DB", e);

    assertEquals("Begin date is not correct", e.getBegin(), beginEventDate);
    assertEquals("End date is not correct", e.getEnd(), endEventDate);

    //since when debugging the toString method is used .... and it rely on the system TimeZone, we test it too
    System.out.println(e.getBegin().toString());
    System.out.println(e.getEnd().toString());
}

From source file:com.example.app.support.service.AppUtil.java

/**
 * Convert the given Date to a ZonedDateTime at the given TimeZone
 *
 * @param date the Date//from   w ww. ja  va2  s .  co m
 * @param zone the TimeZone to convert to
 *
 * @return a ZonedDateTime that represents the same instant as the Date, at the TimeZone specified.
 */
@Nullable
public ZonedDateTime toZonedDateTime(@Nullable Date date, @Nullable TimeZone zone) {
    if (date == null || zone == null)
        return null;
    return ZonedDateTime.ofInstant(date.toInstant(), zone.toZoneId());
}

From source file:com.example.app.profile.model.ProfileDAO.java

/**
 * Get a list of Users that have a Membership to the given Profile that has the given MembershipOperation
 *
 * @param profile the Profile//www .j ava2 s .c om
 * @param operation the Operation
 * @param timeZone the timezone.
 *
 * @return list of Users
 */
public List<User> getUsersWithOperation(@Nonnull Profile profile, TimeZone timeZone,
        @Nonnull MembershipOperation operation) {
    ZonedDateTime now = ZonedDateTime.now(timeZone.toZoneId());
    return getUsersWithOperation(profile, operation, now, now);
}