Search in sources :

Example 71 with ZoneId

use of java.time.ZoneId in project dbflute-core by dbflute.

the class DfTypeUtil method toZonedDateTime.

// -----------------------------------------------------
// ZonedDateTime
// -------------
/**
 * @param obj The object to be converted. (NullAllowed: if null or empty, returns null)
 * @param timeZone The time-zone for the local date. (NotNull)
 * @return The zoned date-time. (NullAllowed: when the argument is null or empty)
 */
protected static ZonedDateTime toZonedDateTime(Object obj, TimeZone timeZone) {
    // internal function for now, needs to know zoned handling
    assertTimeZoneNotNull("toZonedDateTime()", timeZone);
    if (obj == null) {
        return null;
    }
    final Date zonedResourceDate = toZonedResourceDate(obj, timeZone);
    if (zonedResourceDate == null) {
        return null;
    }
    final ZoneId zoneId = timeZone.toZoneId();
    return ZonedDateTime.ofInstant(zonedResourceDate.toInstant(), zoneId);
}
Also used : ZoneId(java.time.ZoneId) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 72 with ZoneId

use of java.time.ZoneId in project jphp by jphp-compiler.

the class DateFunctions method date_parse_from_format.

public static Memory date_parse_from_format(Environment env, TraceInfo traceInfo, Memory format, Memory date) {
    try {
        ZoneId zoneId = zoneId(date_default_timezone_get(env, traceInfo));
        DateTimeParseResult result = DateFormat.createParseResultFromFormat(format.toString(), date.toString(), ZonedDateTime.now(zoneId));
        return result.toArrayMemory();
    } catch (DateTimeException | NoSuchElementException | IllegalArgumentException e) {
        return new DateTimeParseResult(null, Collections.emptySet(), null, null).toArrayMemory();
    }
}
Also used : DateTimeException(java.time.DateTimeException) ZoneId(java.time.ZoneId) DateTimeParseResult(org.develnext.jphp.zend.ext.standard.date.DateTimeParseResult) NoSuchElementException(java.util.NoSuchElementException)

Example 73 with ZoneId

use of java.time.ZoneId in project jphp by jphp-compiler.

the class ZoneIdFactory method aliasFor.

public static String aliasFor(ZonedDateTime dateTime) {
    String id = dateTime.getZone().getId();
    switch(id) {
        case "GMT":
        case "UTC":
            return id;
    }
    List<TimezoneWithAlias> aliases = idToAliases.get(id);
    if (aliases == null) {
        return null;
    }
    ZoneId zone = dateTime.getZone();
    ZoneRules rules = zone.getRules();
    boolean dst = zone.getRules().isDaylightSavings(dateTime.toInstant());
    ZoneOffset zoneOffset = rules.getOffset(dateTime.toInstant());
    int offset = zoneOffset.getTotalSeconds();
    for (TimezoneWithAlias alias : aliases) {
        if (alias.timezone.getOffset() == offset && alias.timezone.isDst() == dst) {
            return alias.alias.toUpperCase();
        }
    }
    return null;
}
Also used : ZoneId(java.time.ZoneId) ZoneRules(java.time.zone.ZoneRules) ZoneOffset(java.time.ZoneOffset)

Example 74 with ZoneId

use of java.time.ZoneId in project apollo by ctripcorp.

the class RelativeDateFormat method format.

public static String format(Date date) {
    Instant instant = date.toInstant();
    ZoneId zoneId = ZoneId.systemDefault();
    LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
    Duration duration = Duration.between(localDateTime, LocalDateTime.now());
    if (duration.toMillis() <= 0L) {
        return "now";
    }
    if (duration.getSeconds() <= 60L) {
        return (duration.getSeconds() <= 0 ? 1 : duration.getSeconds()) + ONE_SECOND_AGO;
    }
    if (duration.toMinutes() < 45L) {
        return (duration.toMinutes() <= 0 ? 1 : duration.toMinutes()) + ONE_MINUTE_AGO;
    }
    if (duration.toHours() < 24L) {
        return (duration.toHours() <= 0 ? 1 : duration.toHours()) + ONE_HOUR_AGO;
    }
    if (localDateTime.isAfter(LocalDateTime.now().minusDays(1))) {
        return "yesterday";
    }
    if (localDateTime.isAfter(LocalDateTime.now().minusDays(2))) {
        return "the day before yesterday";
    }
    if (duration.toDays() < 30L) {
        return (duration.toDays() <= 0 ? 1 : duration.toDays()) + ONE_DAY_AGO;
    }
    if (duration.toDays() / 30 <= 3L) {
        return duration.toDays() / 30 + ONE_MONTH_AGO;
    }
    return TIMESTAMP_FORMAT.format(date);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) Duration(java.time.Duration)

Example 75 with ZoneId

use of java.time.ZoneId in project uavstack by uavorg.

the class Jdk8DateCodec method deserialze.

@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName, String format, int feature) {
    JSONLexer lexer = parser.lexer;
    if (lexer.token() == JSONToken.NULL) {
        lexer.nextToken();
        return null;
    }
    if (lexer.token() == JSONToken.LITERAL_STRING) {
        String text = lexer.stringVal();
        lexer.nextToken();
        DateTimeFormatter formatter = null;
        if (format != null) {
            if (defaultPatttern.equals(format)) {
                formatter = defaultFormatter;
            } else {
                formatter = DateTimeFormatter.ofPattern(format);
            }
        }
        if ("".equals(text)) {
            return null;
        }
        if (type == LocalDateTime.class) {
            LocalDateTime localDateTime;
            if (text.length() == 10 || text.length() == 8) {
                LocalDate localDate = parseLocalDate(text, format, formatter);
                localDateTime = LocalDateTime.of(localDate, LocalTime.MIN);
            } else {
                localDateTime = parseDateTime(text, formatter);
            }
            return (T) localDateTime;
        } else if (type == LocalDate.class) {
            LocalDate localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = LocalDate.of(localDateTime.getYear(), localDateTime.getMonthValue(), localDateTime.getDayOfMonth());
            } else {
                localDate = parseLocalDate(text, format, formatter);
            }
            return (T) localDate;
        } else if (type == LocalTime.class) {
            LocalTime localDate;
            if (text.length() == 23) {
                LocalDateTime localDateTime = LocalDateTime.parse(text);
                localDate = LocalTime.of(localDateTime.getHour(), localDateTime.getMinute(), localDateTime.getSecond(), localDateTime.getNano());
            } else {
                localDate = LocalTime.parse(text);
            }
            return (T) localDate;
        } else if (type == ZonedDateTime.class) {
            if (formatter == defaultFormatter) {
                formatter = ISO_FIXED_FORMAT;
            }
            ZonedDateTime zonedDateTime = parseZonedDateTime(text, formatter);
            return (T) zonedDateTime;
        } else if (type == OffsetDateTime.class) {
            OffsetDateTime offsetDateTime = OffsetDateTime.parse(text);
            return (T) offsetDateTime;
        } else if (type == OffsetTime.class) {
            OffsetTime offsetTime = OffsetTime.parse(text);
            return (T) offsetTime;
        } else if (type == ZoneId.class) {
            ZoneId offsetTime = ZoneId.of(text);
            return (T) offsetTime;
        } else if (type == Period.class) {
            Period period = Period.parse(text);
            return (T) period;
        } else if (type == Duration.class) {
            Duration duration = Duration.parse(text);
            return (T) duration;
        } else if (type == Instant.class) {
            Instant instant = Instant.parse(text);
            return (T) instant;
        }
    } else if (lexer.token() == JSONToken.LITERAL_INT) {
        long millis = lexer.longValue();
        lexer.nextToken();
        if (type == LocalDateTime.class) {
            return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId());
        }
        if (type == LocalDate.class) {
            return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalDate();
        }
        if (type == LocalTime.class) {
            return (T) LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), JSON.defaultTimeZone.toZoneId()).toLocalTime();
        }
        throw new UnsupportedOperationException();
    } else {
        throw new UnsupportedOperationException();
    }
    return null;
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) Period(java.time.Period) JSONLexer(com.alibaba.fastjson.parser.JSONLexer) Duration(java.time.Duration) LocalDate(java.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) OffsetDateTime(java.time.OffsetDateTime) OffsetTime(java.time.OffsetTime) DateTimeFormatter(java.time.format.DateTimeFormatter)

Aggregations

ZoneId (java.time.ZoneId)420 Test (org.junit.Test)137 ZonedDateTime (java.time.ZonedDateTime)108 LocalDateTime (java.time.LocalDateTime)69 LocalDate (java.time.LocalDate)55 Instant (java.time.Instant)49 Test (org.testng.annotations.Test)41 ZoneOffset (java.time.ZoneOffset)37 DateTimeFormatter (java.time.format.DateTimeFormatter)32 Date (java.util.Date)29 ArrayList (java.util.ArrayList)23 LocalTime (java.time.LocalTime)21 TemporalAccessor (java.time.temporal.TemporalAccessor)15 List (java.util.List)15 HashMap (java.util.HashMap)14 Clock (java.time.Clock)13 Collectors (java.util.stream.Collectors)13 DateTimeException (java.time.DateTimeException)12 Duration (java.time.Duration)12 OffsetDateTime (java.time.OffsetDateTime)11