Example usage for java.time ZoneId of

List of usage examples for java.time ZoneId of

Introduction

In this page you can find the example usage for java.time ZoneId of.

Prototype

public static ZoneId of(String zoneId) 

Source Link

Document

Obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use.

Usage

From source file:Main.java

public static void main(String[] args) {
    Set<String> allZones = ZoneId.getAvailableZoneIds();
    List<String> zoneList = new ArrayList<String>(allZones);
    Collections.sort(zoneList);/*  www .  j a  v a  2  s  .c  om*/

    LocalDateTime dt = LocalDateTime.now();
    for (String s : zoneList) {
        ZoneId zone = ZoneId.of(s);
        ZonedDateTime zdt = dt.atZone(zone);
        ZoneOffset offset = zdt.getOffset();
        String out = String.format("%35s %10s%n", zone, offset);
        System.out.println(out);
    }
}

From source file:Main.java

public static void main(String[] args) {

    // the current date
    LocalDate currentDate = LocalDate.now();

    // 2014-02-10
    LocalDate tenthFeb2014 = LocalDate.of(2014, Month.FEBRUARY, 10);

    // months values start at 1 (2014-08-01)
    LocalDate firstAug2014 = LocalDate.of(2014, 8, 1);

    // the 65th day of 2010 (2010-03-06)
    LocalDate sixtyFifthDayOf2010 = LocalDate.ofYearDay(2010, 65);

    // times, e.g. 19:12:30.733

    LocalTime currentTime = LocalTime.now(); // current time
    LocalTime midday = LocalTime.of(12, 0); // 12:00
    LocalTime afterMidday = LocalTime.of(13, 30, 15); // 13:30:15

    // 12345th second of day (03:25:45)
    LocalTime fromSecondsOfDay = LocalTime.ofSecondOfDay(12345);

    // dates with times, e.g. 2014-02-18T19:08:37.950
    LocalDateTime currentDateTime = LocalDateTime.now();

    // 2014-10-02 12:30
    LocalDateTime secondAug2014 = LocalDateTime.of(2014, 10, 2, 12, 30);

    // 2014-12-24 12:00
    LocalDateTime christmas2014 = LocalDateTime.of(2014, Month.DECEMBER, 24, 12, 0);

    // current (local) time in Los Angeles
    LocalTime currentTimeInLosAngeles = LocalTime.now(ZoneId.of("America/Los_Angeles"));

    // current time in UTC time zone
    LocalTime nowInUtc = LocalTime.now(Clock.systemUTC());

    System.out.println("date/time creation: currentDate: " + currentDate);
    System.out.println("date/time creation: tenthFeb2014: " + tenthFeb2014);
    System.out.println("date/time creation: firstAug2014: " + firstAug2014);
    System.out.println("date/time creation: sixtyFifthDayOf2010: " + sixtyFifthDayOf2010);
    System.out.println("date/time creation: currentTime: " + currentTime);
    System.out.println("date/time creation: midday: " + midday);
    System.out.println("date/time creation: afterMidday: " + afterMidday);
    System.out.println("date/time creation: fromSecondsOfDay: " + fromSecondsOfDay);
    System.out.println("date/time creation: currentTimeInLosAngeles: " + currentTimeInLosAngeles);
    System.out.println("date/time creation: currentDateTime: " + currentDateTime);
    System.out.println("date/time creation: secondAug2014: " + secondAug2014);
    System.out.println("date/time creation: christmas2014: " + christmas2014);
}

From source file:Main.java

public static void main(String[] args) {
    TemporalQuery<Integer> daysBeforeChristmas = new TemporalQuery<Integer>() {

        public int daysTilChristmas(int acc, Temporal temporal) {
            int month = temporal.get(ChronoField.MONTH_OF_YEAR);
            int day = temporal.get(ChronoField.DAY_OF_MONTH);
            int max = temporal.with(TemporalAdjusters.lastDayOfMonth()).get(ChronoField.DAY_OF_MONTH);
            if (month == 12 && day <= 25)
                return acc + (25 - day);
            return daysTilChristmas(acc + (max - day + 1),
                    temporal.with(TemporalAdjusters.firstDayOfNextMonth()));
        }/*from ww w.j  a v  a2 s .c  o  m*/

        @Override
        public Integer queryFrom(TemporalAccessor temporal) {
            if (!(temporal instanceof Temporal))
                throw new RuntimeException("Temporal accessor must be of type Temporal");
            return daysTilChristmas(0, (Temporal) temporal);
        }
    };

    System.out.println(LocalDate.of(2013, 12, 26).query(daysBeforeChristmas)); // 364
    System.out.println(LocalDate.of(2013, 12, 23).query(daysBeforeChristmas)); // 2
    System.out.println(LocalDate.of(2013, 12, 25).query(daysBeforeChristmas)); // 0
    System.out.println(ZonedDateTime.of(2013, 12, 1, 11, 0, 13, 938282, ZoneId.of("America/Los_Angeles"))
            .query(daysBeforeChristmas)); // 24
}

From source file:Main.java

public static LocalDate asLocalDateInUTC(Date date) {
    ZoneId UTC = ZoneId.of("Z");
    return date.toInstant().atZone(UTC).toLocalDate();
}

From source file:Main.java

public static String getCurrentTimeStamp() {
    Clock clock = Clock.system(ZoneId.of("Europe/Berlin"));
    ZonedDateTime now = ZonedDateTime.now(clock);
    return DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(now);
}

From source file:com.example.db.Message.java

public static Message createNew(final User user, final String text) {
    final long id = System.currentTimeMillis();
    return new Message(id, user.getId(), text, LocalDateTime.now(ZoneId.of("UTC")));
}

From source file:com.example.db.User.java

public static User createNew(final String username, final String password) {
    final String id = UUID.randomUUID().toString();
    return new User(id, username, password, UserRole.forNormalUser(), LocalDateTime.now(ZoneId.of("UTC")));
}

From source file:com.todo.backend.security.JWTUtils.java

public static String createToken(Long userId, UserRole userRole, String secretKey) {

    final ZonedDateTime validity = ZonedDateTime.now(ZoneId.of("UTC")).plusSeconds(VALIDITY);

    return Jwts.builder().setSubject(userId.toString()).claim(AUTHORITIES_KEY, userRole.name())
            .signWith(SignatureAlgorithm.HS512, secretKey).setExpiration(Date.from(validity.toInstant()))
            .compact();/* w ww.ja v  a  2  s .co m*/
}

From source file:org.nodatime.tzvalidate.Java8Dump.java

@Override
public ZoneTransitions getTransitions(String id, int fromYear, int toYear) {
    ZoneId zone = ZoneId.of(id);
    ZoneRules rules = zone.getRules();
    DateTimeFormatter nameFormat = DateTimeFormatter.ofPattern("zzz", Locale.US);
    ZoneTransitions transitions = new ZoneTransitions(id);

    Instant start = ZonedDateTime.of(fromYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();
    transitions.addTransition(null, rules.getOffset(start).getTotalSeconds() * 1000,
            rules.isDaylightSavings(start), nameFormat.format(start.atZone(zone)));
    Instant end = ZonedDateTime.of(toYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();

    ZoneOffsetTransition transition = rules.nextTransition(start.minusNanos(1));
    while (transition != null && transition.getInstant().isBefore(end)) {
        Instant instant = transition.getInstant();
        transitions.addTransition(new Date(instant.toEpochMilli()),
                rules.getOffset(instant).getTotalSeconds() * 1000, rules.isDaylightSavings(instant),
                nameFormat.format(instant.atZone(zone)));
        transition = rules.nextTransition(instant);
    }/* w w  w  .  ja  v a2s  . co m*/
    return transitions;
}

From source file:com.teradata.benchto.service.rest.converters.ZonedDateTimeConverter.java

@Override
public ZonedDateTime convert(String source) {
    return Instant.ofEpochMilli(Long.parseLong(source)).atZone(ZoneId.of("UTC"));
}