Java Code Examples for java.util.TimeZone#toZoneId()

The following examples show how to use java.util.TimeZone#toZoneId() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: flink   File: SqlDateTimeUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Do addition on timestamp.
 *
 * @param ts    the timestamp.
 * @param days  days count you want to add.
 * @return datetime string.
 */
public static String dateAdd(long ts, int days, TimeZone tz) {
	ZoneId zoneId = tz.toZoneId();
	Instant instant = Instant.ofEpochMilli(ts);
	ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
	long resultTs = zdt.plusDays(days).toInstant().toEpochMilli();
	return dateFormat(resultTs, DATE_FORMAT_STRING, tz);
}
 
Example 2
Source Project: swage   File: RollingFileWriter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a RollingFileWriter as specified
 *
 * @param directory Path where the log files will be created
 * @param name Base name to use for log files
 * @param zone Time zone of timestamps on rolling file names
 * @param minuteRotation If true, rotate logs every minute, if false rotate every hour
 */
RollingFileWriter(Path directory, String name, TimeZone zone, boolean minuteRotation)
{
    if (directory == null) {
        throw new IllegalArgumentException("Directory required");
    }
    if (name == null) {
        throw new IllegalArgumentException("File base name required");
    }
    if (zone == null) {
        throw new IllegalArgumentException("Time zone required");
    }

    this.directory = directory;
    this.baseName = name;
    this.timeZone = zone.toZoneId();

    final String formatPattern = (minuteRotation ? ".yyyy-MM-dd-HH-mm" : ".yyyy-MM-dd-HH");
    this.format = DateTimeFormatter.ofPattern(formatPattern);

    if (minuteRotation) {
        rollInterval = ChronoUnit.SECONDS;
    } else {
        rollInterval = ChronoUnit.HOURS;
    }

    // Set to roll immediately on first write, to ensure file is created
    this.rollAt = Instant.now().truncatedTo(rollInterval);
}
 
Example 3
@Override
public ZoneId getZoneId(ZoneId input) {
    Environment env = Environment.getCurrentEnvironment();
    if (env == null) {
        throw new IllegalStateException(getClass().getName() + " called outside of a template processing thread");
    }
    final TimeZone timeZone = env.getTimeZone();
    if (timeZone == null) {
        throw new NullPointerException("Current Environment has no TimeZone set!");
    }
    return timeZone.toZoneId();
}
 
Example 4
Source Project: jackcess   File: DatabaseImpl.java    License: Apache License 2.0 5 votes vote down vote up
private void setZoneInfo(TimeZone newTimeZone, ZoneId newZoneId) {
  if(newTimeZone != null) {
    newZoneId = newTimeZone.toZoneId();
  } else if(newZoneId != null) {
    newTimeZone = TimeZone.getTimeZone(newZoneId);
  } else {
    newTimeZone = getDefaultTimeZone();
    newZoneId = newTimeZone.toZoneId();
  }

  _timeZone = newTimeZone;
  _zoneId = newZoneId;
}
 
Example 5
Source Project: flink   File: SqlDateTimeUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Do addition on timestamp.
 *
 * @param ts    the timestamp.
 * @param days  days count you want to add.
 * @return datetime string.
 */
public static String dateAdd(long ts, int days, TimeZone tz) {
	ZoneId zoneId = tz.toZoneId();
	Instant instant = Instant.ofEpochMilli(ts);
	ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
	long resultTs = zdt.plusDays(days).toInstant().toEpochMilli();
	return dateFormat(resultTs, DATE_FORMAT_STRING, tz);
}
 
Example 6
Source Project: graphouse   File: MetricsStreamCallback.java    License: Apache License 2.0 4 votes vote down vote up
public MetricsStreamCallback(List<Metric> metrics, TimeZone clickHouseTimeZone) {
    this(metrics, clickHouseTimeZone.toZoneId(), LocalDate.now());
}
 
Example 7
public static Object resolveZoneId(HttpServletRequest request) {
	TimeZone timeZone = RequestContextUtils.getTimeZone(request);
	return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
}
 
Example 8
public static Object resolveZoneId(HttpServletRequest request) {
	TimeZone timeZone = RequestContextUtils.getTimeZone(request);
	return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
}
 
Example 9
Source Project: gtfs-validator   File: GtfsStatisticsService.java    License: MIT License 4 votes vote down vote up
private ZoneId getTimeZone(){
	CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(gtfsDao);
	TimeZone tz = calendarService.getTimeZoneForAgencyId(gtfsDao.getAllAgencies().iterator().next().getId());
	return tz.toZoneId();
}
 
Example 10
Source Project: Orienteer   File: OrienteerWebSession.java    License: Apache License 2.0 4 votes vote down vote up
public ZoneId getClientZoneId() {
	TimeZone timeZone = getClientInfo().getProperties().getTimeZone();
	return timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault();
}
 
Example 11
Source Project: flink   File: SqlDateTimeUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * NOTE:
 * (1). JDK relies on the operating system clock for time.
 * Each operating system has its own method of handling date changes such as
 * leap seconds(e.g. OS will slow down the  clock to accommodate for this).
 * (2). DST(Daylight Saving Time) is a legal issue, governments changed it
 * over time. Some days are NOT exactly 24 hours long, it could be 23/25 hours
 * long on the first or last day of daylight saving time.
 * JDK can handle DST correctly.
 * TODO:
 *       carefully written algorithm can improve the performance
 */
public static int dateDiff(long t1, long t2, TimeZone tz) {
	ZoneId zoneId = tz.toZoneId();
	LocalDate ld1 = Instant.ofEpochMilli(t1).atZone(zoneId).toLocalDate();
	LocalDate ld2 = Instant.ofEpochMilli(t2).atZone(zoneId).toLocalDate();
	return (int) ChronoUnit.DAYS.between(ld2, ld1);
}
 
Example 12
Source Project: flink   File: SqlDateTimeUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Do subtraction on date string.
 *
 * @param ts    the timestamp.
 * @param days days count you want to subtract.
 * @param tz time zone of the date time string
 * @return datetime string.
 */
public static String dateSub(long ts, int days, TimeZone tz) {
	ZoneId zoneId = tz.toZoneId();
	Instant instant = Instant.ofEpochMilli(ts);
	ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
	long resultTs = zdt.minusDays(days).toInstant().toEpochMilli();
	return dateFormat(resultTs, DATE_FORMAT_STRING, tz);
}
 
Example 13
Source Project: flink   File: SqlDateTimeUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * NOTE:
 * (1). JDK relies on the operating system clock for time.
 * Each operating system has its own method of handling date changes such as
 * leap seconds(e.g. OS will slow down the  clock to accommodate for this).
 * (2). DST(Daylight Saving Time) is a legal issue, governments changed it
 * over time. Some days are NOT exactly 24 hours long, it could be 23/25 hours
 * long on the first or last day of daylight saving time.
 * JDK can handle DST correctly.
 * TODO:
 *       carefully written algorithm can improve the performance
 */
public static int dateDiff(long t1, long t2, TimeZone tz) {
	ZoneId zoneId = tz.toZoneId();
	LocalDate ld1 = Instant.ofEpochMilli(t1).atZone(zoneId).toLocalDate();
	LocalDate ld2 = Instant.ofEpochMilli(t2).atZone(zoneId).toLocalDate();
	return (int) ChronoUnit.DAYS.between(ld2, ld1);
}
 
Example 14
Source Project: flink   File: SqlDateTimeUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Do subtraction on date string.
 *
 * @param ts    the timestamp.
 * @param days days count you want to subtract.
 * @param tz time zone of the date time string
 * @return datetime string.
 */
public static String dateSub(long ts, int days, TimeZone tz) {
	ZoneId zoneId = tz.toZoneId();
	Instant instant = Instant.ofEpochMilli(ts);
	ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, zoneId);
	long resultTs = zdt.minusDays(days).toInstant().toEpochMilli();
	return dateFormat(resultTs, DATE_FORMAT_STRING, tz);
}
 
Example 15
/**
 * 转换成java8 时间
 *
 * @param calendar 日历
 * @return LocalDateTime
 */
public static LocalDateTime fromCalendar(final Calendar calendar) {
	TimeZone tz = calendar.getTimeZone();
	ZoneId zid = tz == null ? ZoneId.systemDefault() : tz.toZoneId();
	return LocalDateTime.ofInstant(calendar.toInstant(), zid);
}