< prev index next >

src/java.base/share/classes/java/time/ZoneOffset.java

Print this page

424             throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00");
425         }
426         if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) {
427             return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> {
428                 ZoneOffset result = new ZoneOffset(totalSecs);
429                 ID_CACHE.putIfAbsent(result.getId(), result);
430                 return result;
431             });
432         } else {
433             return new ZoneOffset(totalSeconds);
434         }
435     }
436 
437     //-----------------------------------------------------------------------
438     /**
439      * Constructor.
440      *
441      * @param totalSeconds  the total time-zone offset in seconds, from -64800 to +64800
442      */
443     private ZoneOffset(int totalSeconds) {
444         super();
445         this.totalSeconds = totalSeconds;
446         id = buildId(totalSeconds);
447     }
448 
449     private static String buildId(int totalSeconds) {
450         if (totalSeconds == 0) {
451             return "Z";
452         } else {
453             int absTotalSeconds = Math.abs(totalSeconds);
454             StringBuilder buf = new StringBuilder();
455             int absHours = absTotalSeconds / SECONDS_PER_HOUR;
456             int absMinutes = (absTotalSeconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
457             buf.append(totalSeconds < 0 ? "-" : "+")
458                 .append(absHours < 10 ? "0" : "").append(absHours)
459                 .append(absMinutes < 10 ? ":0" : ":").append(absMinutes);
460             int absSeconds = absTotalSeconds % SECONDS_PER_MINUTE;
461             if (absSeconds != 0) {
462                 buf.append(absSeconds < 10 ? ":0" : ":").append(absSeconds);
463             }
464             return buf.toString();

424             throw new DateTimeException("Zone offset not in valid range: -18:00 to +18:00");
425         }
426         if (totalSeconds % (15 * SECONDS_PER_MINUTE) == 0) {
427             return SECONDS_CACHE.computeIfAbsent(totalSeconds, totalSecs -> {
428                 ZoneOffset result = new ZoneOffset(totalSecs);
429                 ID_CACHE.putIfAbsent(result.getId(), result);
430                 return result;
431             });
432         } else {
433             return new ZoneOffset(totalSeconds);
434         }
435     }
436 
437     //-----------------------------------------------------------------------
438     /**
439      * Constructor.
440      *
441      * @param totalSeconds  the total time-zone offset in seconds, from -64800 to +64800
442      */
443     private ZoneOffset(int totalSeconds) {

444         this.totalSeconds = totalSeconds;
445         id = buildId(totalSeconds);
446     }
447 
448     private static String buildId(int totalSeconds) {
449         if (totalSeconds == 0) {
450             return "Z";
451         } else {
452             int absTotalSeconds = Math.abs(totalSeconds);
453             StringBuilder buf = new StringBuilder();
454             int absHours = absTotalSeconds / SECONDS_PER_HOUR;
455             int absMinutes = (absTotalSeconds / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR;
456             buf.append(totalSeconds < 0 ? "-" : "+")
457                 .append(absHours < 10 ? "0" : "").append(absHours)
458                 .append(absMinutes < 10 ? ":0" : ":").append(absMinutes);
459             int absSeconds = absTotalSeconds % SECONDS_PER_MINUTE;
460             if (absSeconds != 0) {
461                 buf.append(absSeconds < 10 ? ":0" : ":").append(absSeconds);
462             }
463             return buf.toString();
< prev index next >