下面列出了java.util.TimeZone#toZoneId ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
/**
* 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);
}
@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();
}
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;
}
/**
* 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);
}
public MetricsStreamCallback(List<Metric> metrics, TimeZone clickHouseTimeZone) {
this(metrics, clickHouseTimeZone.toZoneId(), LocalDate.now());
}
public static Object resolveZoneId(HttpServletRequest request) {
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
}
public static Object resolveZoneId(HttpServletRequest request) {
TimeZone timeZone = RequestContextUtils.getTimeZone(request);
return (timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault());
}
private ZoneId getTimeZone(){
CalendarService calendarService = CalendarServiceDataFactoryImpl.createService(gtfsDao);
TimeZone tz = calendarService.getTimeZoneForAgencyId(gtfsDao.getAllAgencies().iterator().next().getId());
return tz.toZoneId();
}
public ZoneId getClientZoneId() {
TimeZone timeZone = getClientInfo().getProperties().getTimeZone();
return timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault();
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 转换成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);
}