Java tutorial
/* Copyright (C) 2016 NTT DATA Corporation This program is free software; you can redistribute it and/or Modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */ package com.clustercontrol.util; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Locale; import java.util.TimeZone; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * HinemosManager?????Hinemos???<br/> * <br> * TODO Ver5.1????Hinemos?????? * ??static?????????????????? * */ public class HinemosTime { private static Log log = LogFactory.getLog(HinemosTime.class); private static volatile long timeOffsetMillis = 0; private static volatile TimeZone timeZone = TimeZone.getDefault(); /** * HinemosManager????Hinemos?<br> * @return */ public static long currentTimeMillis() { return System.currentTimeMillis() + timeOffsetMillis; } /** * Hinemos?Date??<br> * @return */ public static Date getDateInstance() { return new Date(HinemosTime.currentTimeMillis()); } /** * Hinemos??HinemosManager?????<br> * @return */ public static String getDateString() { SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss XXX yyyy", Locale.US); sdf.setTimeZone(HinemosTime.getTimeZone()); return sdf.format(currentTimeMillis()); } /** * Hinemos??HinemosManager????Calendar??<br> * @return */ public static Calendar getCalendarInstance() { Calendar calendar = Calendar.getInstance(getTimeZone()); calendar.setTimeInMillis(HinemosTime.currentTimeMillis()); return calendar; } /** * Hinemos?????Calendar??<br> * @param timezone * @return */ public static Calendar getCalendarInstance(TimeZone timezone) { Calendar calendar = Calendar.getInstance(timezone); calendar.setTimeInMillis(calendar.getTimeInMillis() + timeOffsetMillis); return calendar; } /** * Hinemos??HinemosManager????Calendar??<br> * @param locale * @return */ public static Calendar getCalendarInstance(Locale locale) { Calendar calendar = Calendar.getInstance(getTimeZone(), locale); calendar.setTimeInMillis(calendar.getTimeInMillis() + timeOffsetMillis); return calendar; } /** * Hinemos?????Calendar??<br> * @param timezone * @param locale * @return */ public static Calendar getCalendarInstance(TimeZone timezone, Locale locale) { Calendar calendar = Calendar.getInstance(timezone, locale); calendar.setTimeInMillis(calendar.getTimeInMillis() + timeOffsetMillis); return calendar; } /** * ????? * @return timeOffsetMillis ??? */ public static long getTimeOffsetMillis() { log.debug("getTimeOffsetMillis():" + timeOffsetMillis); return timeOffsetMillis; } /** * ???? * @param offset ??? */ public static void setTimeOffsetMillis(long offset) { log.debug("setTimeOffsetMillis(): " + timeOffsetMillis + " -> " + offset); timeOffsetMillis = offset; } /** * HinemosManager????<br> * @return timeZone */ public static TimeZone getTimeZone() { log.debug("getTimeZone():" + timeZone); return timeZone; } /** * HinemosManager??????UTC???<br> * @return timeZone */ public static int getTimeZoneOffset() { log.debug("getTimezoneOffset():" + timeZone.getRawOffset()); return timeZone.getRawOffset(); } /** * HinemosManager???UTC???? * @param offset */ public static void setTimeZoneOffset(int offset) { log.debug("setTimeZoneOffset(): " + getTimeZoneOffset() + " -> " + offset); timeZone.setRawOffset(offset); } }