diff --git a/serverlogic/src/main/java/com/ljsd/GameApplication.java b/serverlogic/src/main/java/com/ljsd/GameApplication.java index 2f46f2a50..6262c9a59 100644 --- a/serverlogic/src/main/java/com/ljsd/GameApplication.java +++ b/serverlogic/src/main/java/com/ljsd/GameApplication.java @@ -1,6 +1,5 @@ package com.ljsd; -import com.ljsd.jieling.config.json.ConfMessage; import com.ljsd.jieling.config.json.ServerConfiguration; import com.ljsd.jieling.config.json.ServerProperties; import com.ljsd.jieling.db.redis.RedisUtil; @@ -48,10 +47,7 @@ public class GameApplication { configurableApplicationContext = app.run(args); ServerConfiguration serverConfiguration = configurableApplicationContext.getBean(ServerConfiguration.class); serverProperties = serverConfiguration.getServerProperties(); - - //加载配置开关 - ConfMessage.doWork(); - + serverId = serverProperties.getId(); //注册消息处理方法 ProtocolsManager protocolsManager = ProtocolsManager.getInstance(); // protocolsManager.initContext(); diff --git a/serverlogic/src/main/java/com/ljsd/jieling/thread/ThreadManager.java b/serverlogic/src/main/java/com/ljsd/jieling/thread/ThreadManager.java index bbeec9794..2a6b16dfb 100644 --- a/serverlogic/src/main/java/com/ljsd/jieling/thread/ThreadManager.java +++ b/serverlogic/src/main/java/com/ljsd/jieling/thread/ThreadManager.java @@ -1,6 +1,6 @@ package com.ljsd.jieling.thread; -import com.ljsd.jieling.thread.task.TestTask; +import com.ljsd.jieling.thread.task.PlatConfigureTask; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ConfigurableApplicationContext; @@ -15,14 +15,14 @@ public class ThreadManager { private static final Logger LOGGER = LoggerFactory.getLogger(ThreadManager.class); - private TestTask testTask; + private PlatConfigureTask testTask; private ScheduledThreadPoolExecutor scheduledExecutor; //管理task public void init(ConfigurableApplicationContext configurableApplicationContext) { - testTask = configurableApplicationContext.getBean(TestTask.class); + testTask = configurableApplicationContext.getBean(PlatConfigureTask.class); go(); } diff --git a/serverlogic/src/main/java/com/ljsd/jieling/thread/task/PlatConfigureTask.java b/serverlogic/src/main/java/com/ljsd/jieling/thread/task/PlatConfigureTask.java new file mode 100644 index 000000000..68ab636c7 --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/thread/task/PlatConfigureTask.java @@ -0,0 +1,33 @@ +package com.ljsd.jieling.thread.task; + +import com.ljsd.jieling.config.json.ConfMessage; +import com.ljsd.jieling.util.TimeUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.io.IOException; + +@Component +public class PlatConfigureTask implements Runnable { + + private static final Logger LOGGER = LoggerFactory.getLogger(PlatConfigureTask.class); + private int SLEEP_INTEVAL_TIME = 60 * 1000; //每1分钟检查一次 + + @Override + public void run() { + doLogic(); + } + + private void doLogic() { + while (true){ + try { + ConfMessage.doWork(); + TimeUtils.sleep(SLEEP_INTEVAL_TIME); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } +} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/thread/task/TestTask.java b/serverlogic/src/main/java/com/ljsd/jieling/thread/task/TestTask.java deleted file mode 100644 index 8e8f517b4..000000000 --- a/serverlogic/src/main/java/com/ljsd/jieling/thread/task/TestTask.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.ljsd.jieling.thread.task; - -import com.ljsd.jieling.db.redis.RedisUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; - -@Component -public class TestTask implements Runnable { - - private static final Logger LOGGER = LoggerFactory.getLogger(TestTask.class); - - - @Override - public void run() { - doLogic(); - } - - private void doLogic() { - long str = RedisUtil.getInstence().incr("TestTask",1L); - LOGGER.info("--> RedisUtil.incr : str = "+str); - LOGGER.info("TestTask ... "); - } -} diff --git a/serverlogic/src/main/java/com/ljsd/jieling/util/TimeUtils.java b/serverlogic/src/main/java/com/ljsd/jieling/util/TimeUtils.java new file mode 100644 index 000000000..74f49f7fd --- /dev/null +++ b/serverlogic/src/main/java/com/ljsd/jieling/util/TimeUtils.java @@ -0,0 +1,1146 @@ +package com.ljsd.jieling.util; + +/** + * Created by admin on 2014/11/17. + */ + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; +import java.util.concurrent.TimeUnit; + + +//import com.google.common.collect.Lists; + +/** + * 时间的工具类 + */ +public class TimeUtils { + + private static final Logger LOGGER = LoggerFactory.getLogger(TimeUtils.class); + + static { + //TimeZone.setDefault(TimeZone.getTimeZone("GMT+8")); 直接使用操作系统的时区 + TIME_ZONE = TimeZone.getDefault(); + } + + /** + * 一毫秒 + */ + public static final long ONE_MILLSECOND = 1L; + /** + * 一秒的毫秒数 + */ + public static final long ONE_SECOND = ONE_MILLSECOND * 1000; + /** + * 一分的毫秒数 + */ + public static final long ONE_MINUTE = ONE_SECOND * 60; + /** + * 一时的毫秒数 + */ + public static final long ONE_HOUR = ONE_MINUTE * 60; + /** + * 一天的毫秒数 + */ + public static final long ONE_DAY = ONE_HOUR * 24; + + + /** + * 毫秒 + */ + public static final long MILLI_SECOND = TimeUnit.MILLISECONDS.toMillis(1); + /** + * 秒 + */ + public static final long SECOND = TimeUnit.SECONDS.toMillis(1); + /** + * 分 + */ + public static final long MIN = TimeUnit.MINUTES.toMillis(1); + /** + * 时 + */ + public static final long HOUR = TimeUnit.HOURS.toMillis(1); + /** + * 天 + */ + public static final long DAY = TimeUnit.DAYS.toMillis(1); + + /** + * 每分钟秒数 + */ + public static final int SECONDS_MIN = (int) (MIN / SECOND); + /** + * 每小时秒数 + */ + public static final int SECONDS_HOUR = (int) (HOUR / SECOND); + /** + * 每小时分钟数 + */ + public static final int MIN_HOUR = (int) (HOUR / MIN); + /** + * 每天小时数 + */ + public static final int HOUR_DAY = (int) (DAY / HOUR); + /** + * 一周的天数 + */ + private static final int DAYOFWEEK_CARDINALITY = 7; + /** + * 周 + */ + public static final long WEEK = DAYOFWEEK_CARDINALITY * DAY; + + /** + * 年月日 时分秒, 格式如: 2011-01-11 01:10:59 + */ + //private static final DateFormat ymdhmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + /** + * 年月日 时分秒, 格式如: 20110111011059 + */ + // private static final DateFormat ymdhmsFormat_new = new SimpleDateFormat("yyyyMMddHHmmss"); + /** + * 年月日 时分, 格式如: 2011-01-11 01:10 + */ + // private static final DateFormat ymdhmFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + /** + * 年月日,格式如1970-07-10 + */ + //private static final DateFormat ymdFormat = new SimpleDateFormat("yyyy-MM-dd"); + /** + * 年月日,格式如1970年07月10日 + */ + // private static final DateFormat ymdChineseFormat = new SimpleDateFormat("yyyy年MM月dd日"); + /** + * 小时和分钟数,格式如10:20 + */ + // private static final DateFormat hmFormat = new SimpleDateFormat("HH:mm"); + // private static final DateFormat hmsFormat = new SimpleDateFormat("HH:mm:ss"); + //private static final Calendar calendar = Calendar.getInstance(); + + public static final TimeZone TIME_ZONE; + + /** + * 判断是否合法的时间格式(HH:mm:ss) + * + * @param dayTime + * @return + */ + public static boolean isValidDayTime(String dayTime) { + try { + String[] _timeStr = dayTime.split(":"); + int _hour = Integer.parseInt(_timeStr[0]); + int _minute = Integer.parseInt(_timeStr[1]); + int _second = Integer.parseInt(_timeStr[2]); + if (_hour < 0 || _hour > 23) { + return false; + } + + if (_minute < 0 || _minute > 59) { + return false; + } + + if (_second < 0 || _second > 59) { + return false; + } + + return true; + } catch (Exception e) { + LOGGER.error("isValidDayTime->msg={}", e.getMessage(), e); + return false; + } + } + + /** + * 判断是否合法的时间格式(HH:mm) + * + * @param hhmm + * @return + */ + public static boolean isValidHhMmTime(String hhmm) { + try { + String[] _timeStr = hhmm.split(":"); + int _hour = Integer.parseInt(_timeStr[0]); + int _minute = Integer.parseInt(_timeStr[1]); + if (_hour < 0 || _hour > 23) { + return false; + } + + if (_minute < 0 || _minute > 59) { + return false; + } + + return true; + } catch (Exception e) { + LOGGER.error("isValidHhMmTime->msg={}", e.getMessage(), e); + return false; + } + } + + /** + * 根据创建时间和有效时间计算截止时间 + * + * @param start 物品的创建时间 + * @param validTime 物品的有效时间长度 + * @param timeUnit 有效时间的单位 {@link TimeUtils#MILLI_SECOND} ~ {@link TimeUtils#DAY} + * @return 物品的截止时间 + */ + public static long getDeadLine(Timestamp start, long validTime, + long timeUnit) { + return TimeUtils.getDeadLine(start.getTime(), validTime, timeUnit); + } + + /** + * 根据创建时间和有效时间计算截止时间 + * + * @param start 物品的创建时间 + * @param validTime 物品的有效时间长度 + * @param timeUnit 有效时间的单位 {@link TimeUtils#MILLI_SECOND} ~ {@link TimeUtils#DAY} + * @return 物品的截止时间 + */ + public static long getDeadLine(long start, long validTime, long timeUnit) { + return start + validTime * timeUnit; + } + + + private static long now() { + return System.currentTimeMillis(); + } + +// /** +// * 获取当天零点时间 +// * +// * @return +// */ +// public static long getTodayBegin(TimeService timeService) { +// Calendar _calendar = Calendar.getInstance(); +// _calendar.setTimeInMillis(timeService.now()); +// _calendar.set(Calendar.HOUR_OF_DAY, 0); +// _calendar.set(Calendar.MINUTE, 0); +// _calendar.set(Calendar.SECOND, 0); +// _calendar.set(Calendar.MILLISECOND, 0); +// return _calendar.getTimeInMillis(); +// } + + /** + * 获取特定日期当天的零点时间 + * + * @return + */ + public static long getBeginOfDay(long time) { + Calendar _calendar = Calendar.getInstance(); + _calendar.setTimeInMillis(time); + _calendar.set(Calendar.HOUR_OF_DAY, 0); + _calendar.set(Calendar.MINUTE, 0); + _calendar.set(Calendar.SECOND, 0); + _calendar.set(Calendar.MILLISECOND, 0); + return _calendar.getTimeInMillis(); + } + + /** + * 获取时间戳字符串 + * + * @param date + * @return + */ + public static String getUrlTimeStamp(Date date) { + DateFormat _format = new SimpleDateFormat("yyyyMMddHHmmss"); + return _format.format(date); + } + +// /** +// * 是否是同一天 +// * +// * @param src +// * @param target +// * @return +// */ +// public static boolean isSameDay(long src, long target) { +// int offset = TIME_ZONE.getRawOffset(); // 只考虑了时区,没考虑夏令时 +// return (src + offset) / DAY == (target + offset) / DAY; +// } + + /** + * 将分钟数转换为小时数和分钟数的数组 如80分钟转换为1小时20分 + * + * @param mins + * @return + */ + public static int[] toTimeArray(int mins) { + int[] _result = new int[2]; + _result[0] = (int) (mins * MIN / HOUR); + _result[1] = (int) (mins - _result[0] * HOUR / MIN); + return _result; + } + + + /** + * 解析ISO8601格式的时间 + * + * @param source + * @return + * @throws ParseException + */ + public static Date getISO8601Time(String source) throws ParseException { + Calendar calendar = javax.xml.bind.DatatypeConverter.parseDateTime(source); + return calendar.getTime(); + } + + + /** + * 返回按时间单位计算后的ms时间,该时间必须足够小以致可用整型表示 + * + * @param time + * @param fromTimeUnit + * @return + */ + public static long translateTime(int time, long fromTimeUnit) { + return TimeUtils.translateTime(time, fromTimeUnit, MILLI_SECOND); + } + + /** + * 将指定的时间值转化为期望单位的时间值 + * + * @param time + * @param fromTimeUnit + * @param toTimeUnit + * @return + */ + public static long translateTime(long time, long fromTimeUnit, + long toTimeUnit) { + long milliTime = time * fromTimeUnit / toTimeUnit; + return milliTime; + } + + /** + * 设置指定时间的设置为给定的时间数(不改变的时间数可填-1) + * + * @param time + * @param year + * @param month + * @param day + * (月中的天数) + * @param hour + * @param minute + * @param second + * @return + */ +// public static long getTime(long time, int year, int month, int day, +// int hour, int minute, int second) { +// calendar.setTimeInMillis(time); +// int _unChange = -1; +// if (year != _unChange) { +// calendar.set(Calendar.YEAR, year); +// } +// if (month != _unChange) { +// calendar.set(Calendar.MONTH, month); +// } +// if (day != _unChange) { +// calendar.set(Calendar.DAY_OF_MONTH, day); +// } +// if (hour != _unChange) { +// calendar.set(Calendar.HOUR_OF_DAY, hour); +// } +// if (minute != _unChange) { +// calendar.set(Calendar.MINUTE, minute); +// } +// if (second != _unChange) { +// calendar.set(Calendar.SECOND, second); +// } +// return calendar.getTimeInMillis(); +// } + + /** + * 获得修正后的时间 + * + * @param originTime + * @param changeYear + * @param changeMonth + * @param changeDay + * @param changeHour + * @param changeMinute + * @param changeSecond + * @return + */ +// public static long getChangeTime(long originTime, int changeYear, +// int changeMonth, int changeDay, int changeHour, int changeMinute, +// int changeSecond) { +// calendar.setTimeInMillis(originTime); +// int _unChange = 0; +// if (changeYear != _unChange) { +// calendar.add(Calendar.YEAR, changeYear); +// } +// if (changeMonth != _unChange) { +// calendar.add(Calendar.MONTH, changeMonth); +// } +// if (changeDay != _unChange) { +// calendar.add(Calendar.DAY_OF_MONTH, changeDay); +// } +// if (changeHour != _unChange) { +// calendar.add(Calendar.HOUR_OF_DAY, changeHour); +// } +// if (changeMinute != _unChange) { +// calendar.add(Calendar.MINUTE, changeMinute); +// } +// if (changeSecond != _unChange) { +// calendar.add(Calendar.SECOND, changeSecond); +// } +// return calendar.getTimeInMillis(); +// } + + /** + * 判断start和end是否在同一个星期内(周一为一周开始) + * + * @param start + * @param end + * @return + * @author GuoHuang + * @date 2009-02-04 + */ + public static boolean isInSameWeek(long start, long end) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(start); + Calendar et = Calendar.getInstance(); + et.setTimeInMillis(end); + int days = Math.abs(TimeUtils.getSoFarWentDays(st, et)); + if (days < TimeUtils.DAYOFWEEK_CARDINALITY) { + // 设置Monday为一周的开始 + st.setFirstDayOfWeek(Calendar.MONDAY); + et.setFirstDayOfWeek(Calendar.MONDAY); + if (st.get(Calendar.WEEK_OF_YEAR) == et.get(Calendar.WEEK_OF_YEAR)) { + return true; + } + } + return false; + } + + /** + * 得到start和end相差几周 + */ + public static int getDiffWeeks(long start, long end) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(start); + //设置周一为一周开始 + st.setFirstDayOfWeek(Calendar.MONDAY); + st.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); + Calendar et = Calendar.getInstance(); + et.setFirstDayOfWeek(Calendar.MONDAY); + et.setTimeInMillis(end); + et.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); + int days = Math.abs(TimeUtils.getSoFarWentDays(st, et)); + return days/7; + } + + /** + * 根据两个long 得到相隔天数 + * + * @param start + * @param end + * @return + */ + public static int getSoFarWentDays(long start, long end) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(start); + Calendar et = Calendar.getInstance(); + et.setTimeInMillis(end); + int days = Math.abs(TimeUtils.getSoFarWentDays(st, et)); + return days; + } + + /** + * 以日期中的日为实际计算单位,计算两个时间点实际日的差距 比如 12-1 23:00 和12-2 01:00,相差1天,而不是小于24小时就算做0天 + * 如果(now - st)为正,则表示now在st之后 + * + * @param st + * @param now + * @return + */ + public static int getSoFarWentDays(Calendar st, Calendar now) { + + int sign = st.before(now) ? 1 : -1; + if (now.before(st)) { + Calendar tmp = now; + now = st; + st = tmp; + } + int days = now.get(Calendar.DAY_OF_YEAR) - st.get(Calendar.DAY_OF_YEAR); + if (st.get(Calendar.YEAR) != now.get(Calendar.YEAR)) { + Calendar cloneSt = (Calendar) st.clone(); + while (cloneSt.get(Calendar.YEAR) != now.get(Calendar.YEAR)) { + days += cloneSt.getActualMaximum(Calendar.DAY_OF_YEAR); + cloneSt.add(Calendar.YEAR, 1); + } + } + + return days * sign; + } + + + public static int getSoFarWentHours(long time1, long time2) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(time1); + + Calendar now = Calendar.getInstance(); + now.setTimeInMillis(time2); + + + if (now.before(st)) { + Calendar tmp = now; + now = st; + st = tmp; + } + + st.clear(Calendar.MILLISECOND); + st.clear(Calendar.SECOND); + st.clear(Calendar.MINUTE); + + int diffHour = 0; + Calendar cloneSt = (Calendar) st.clone(); + while (cloneSt.before(now)) { + cloneSt.add(Calendar.HOUR, 1); + diffHour++; + } + + if (diffHour != 0) { + return diffHour - 1; + } else { + return diffHour; + } + } + + /** + * specTime is in [st,now] or not? + * + * @param st + * @param now + * @param specTime + * @return + */ + private static boolean hasSpecTimeBetween(long st, long now, long specTime) { + if (st <= specTime && specTime <= now) { + return true; + } + return false; + } + + /** + * 得到从time1 到time2 中,specTime所指定的时分秒的时刻,有几次 + * + * @param time1 + * @param time2 + * @param specTime + * @return + */ + public static int getSpecTimeCountBetween(long time1, long time2, long specTime) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(time1); + + Calendar now = Calendar.getInstance(); + now.setTimeInMillis(time2); + + Calendar spec = Calendar.getInstance(); + spec.setTimeInMillis(specTime); + + if (now.before(st)) { + Calendar tmp = now; + now = st; + st = tmp; + } + + //第一个时间的年月日和被比较时间的时间部分合成 + Calendar st_spec = mergeDateAndTime(st, spec); + + if (isSameDay(time1, time2)) { + if (hasSpecTimeBetween(time1, time2, st_spec.getTimeInMillis())) { + return 1; + } else { + return 0; + } + } + + int diffDay = 0; + Calendar cloneSt = (Calendar) st_spec.clone(); + while (cloneSt.before(now)) { + cloneSt.add(Calendar.DATE, 1); + diffDay++; + } + + if (st.after(st_spec)) { + diffDay--; + } + + return diffDay; + } + + + /** + * 获取指定日期星期内的星期几的0点 + * + * @param time + * @param week 0星期天 1 星期一类推 + * @return + */ + public static long getWeekZero(long time, int week) { + Calendar _calendar = Calendar.getInstance(); + _calendar.setTimeInMillis(time); + int w = _calendar.get(Calendar.DAY_OF_WEEK) - 1; + long addTime = (week - w) * 24L * 3600 * 1000; + _calendar.set(Calendar.HOUR_OF_DAY, 0); + _calendar.set(Calendar.MINUTE, 0); + _calendar.set(Calendar.SECOND, 0); + _calendar.set(Calendar.MILLISECOND, 0); + return _calendar.getTimeInMillis() + addTime; + } + + /** + * 获取指定日期星期内的星期几的0点 + * + * @param time + * @param week 0星期天 1 星期一类推 + * @return + */ + public static long getWeekEnd(long time, int week) { + Calendar _calendar = Calendar.getInstance(); + _calendar.setTimeInMillis(time); + int w = _calendar.get(Calendar.DAY_OF_WEEK) - 1; + long addTime = (week - w) * 24L * 3600 * 1000; + _calendar.set(Calendar.HOUR_OF_DAY, 23); + _calendar.set(Calendar.MINUTE, 59); + _calendar.set(Calendar.SECOND, 59); + _calendar.set(Calendar.MILLISECOND, 0); + return _calendar.getTimeInMillis() + addTime; + } + +// public static void main(String[] args) throws Exception { +// Date now = new Date(); +// LOGGER.info("date:" + now); +// Date target = new Date(getLastWeekdayStartTime(now.getTime(), 6)); +// LOGGER.info("last:" + target); +// +// LOGGER.info(SECOND); +// LOGGER.info(System.currentTimeMillis()); +// LOGGER.info(getWeekZero(System.currentTimeMillis(), 1)); +// } + + /** + * 把日期和时间合并 + * + * @param date 代表一个日期,方法其只取日期部分 + * @param time 代表一个时间,方法其只取时间部分 + * @return + */ + public static Calendar mergeDateAndTime(Calendar date, Calendar time) { + Calendar cal = Calendar.getInstance(); + cal.set(date.get(Calendar.YEAR), date.get(Calendar.MONTH), date + .get(Calendar.DATE), time.get(Calendar.HOUR_OF_DAY), time + .get(Calendar.MINUTE), time.get(Calendar.SECOND)); + cal.set(Calendar.MILLISECOND, 0); + return cal; + } + + /** + * 获取几天后的当前时间点 + * + * @param day + * @return + */ + public static Date getAfterToday(int day) { + Calendar c = Calendar.getInstance(); + c.add(Calendar.DATE, day); + return c.getTime(); + } + + /** + * 设置几分钟之后的时间点 + * + * @param minutes + * @return + */ + public static Date getAfterMinutes(int minutes) { + Calendar c = Calendar.getInstance(); + c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes); + return c.getTime(); + } + + /** + * 是否为同一个月 + * + * @param src + * @param target + * @return + */ + public static boolean isSameMonth(long src, long target) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(src); + Calendar et = Calendar.getInstance(); + et.setTimeInMillis(target); + + return st.get(Calendar.MONTH) == et.get(Calendar.MONTH); + } + + /** + * 是否为同一周 + * + * @param src + * @param target + * @return + */ + public static boolean isSameWeek(long src, long target) { + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(src); + Calendar et = Calendar.getInstance(); + et.setTimeInMillis(target); + + return st.get(Calendar.WEEK_OF_YEAR) == et.get(Calendar.WEEK_OF_YEAR); + } + + /** + * 是否为同一周周一算第一天 + * + * @return + */ + public static boolean isSameWeWeek(long hour, long lastTime) { + lastTime = lastTime - ONE_DAY - (hour) * 3600 * 1000; + long now = now() - ONE_DAY - (hour) * 3600 * 1000; + Calendar st = Calendar.getInstance(); + st.setTimeInMillis(lastTime); + Calendar et = Calendar.getInstance(); + et.setTimeInMillis(now); + return st.get(Calendar.WEEK_OF_YEAR) == et.get(Calendar.WEEK_OF_YEAR); + } + + /** + * 获得一天中特定时间相对0点的毫秒数 + * + * @param hour + * @param min + * @param sec + * @return + */ + public static long getRelativeTimeOfDay(int hour, int min, int sec) { + return hour * HOUR + min * MIN + sec * SECOND; + } + + /** + * 获得当前时间之前上一个星期几的0点时间 + * + * @param now + * @param targetWeekday + * @return + */ + public static long getLastWeekdayStartTime(long now, int targetWeekday) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(now); + int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); + int daysDiffer = dayOfWeek - targetWeekday; + if (daysDiffer < 0) { + daysDiffer = daysDiffer + DAYOFWEEK_CARDINALITY; + } + + long dayBegin = getBeginOfDay(now); + return dayBegin - daysDiffer * DAY; + } + + + /** + * 获取系统当前默认时区与指定时区的时间差.(单位:毫秒) + * + * @param + * @return 系统当前默认时区与指定时区的时间差.(单位:毫秒) + */ + public static double getUTCDifference() { + return TIME_ZONE.getRawOffset() * 1.0d / HOUR; + } + + + /** + * 判断两个日期是否同一天 + * + * @param r1 + * @param r2 + * @return + */ + public static boolean isSameDay(long r1, long r2) { + Calendar c1 = Calendar.getInstance(); + c1.setTimeInMillis(r1); + int y1 = c1.get(Calendar.YEAR); + int m1 = c1.get(Calendar.MONTH) + 1; + int d1 = c1.get(Calendar.DAY_OF_MONTH); + + Calendar c2 = Calendar.getInstance(); + c2.setTimeInMillis(r2); + int y2 = c2.get(Calendar.YEAR); + int m2 = c2.get(Calendar.MONTH) + 1; + int d2 = c2.get(Calendar.DAY_OF_MONTH); + + return (y1 == y2) && (m1 == m2) && (d1 == d2); + } + + public static boolean isSameDayWithNow(long r1) { + long r2 = now(); + return isSameDay(r1, r2); + } + + // 是否同一周(是否过了周一早上5点) + public static boolean isSameWeekWithNow(long r1) { + Calendar c1 = Calendar.getInstance(); + c1.setTimeInMillis(r1 - 104400000); + int y1 = c1.get(Calendar.YEAR); + int m1 = c1.get(Calendar.MONTH) + 1; + int w1 = c1.get(Calendar.WEEK_OF_YEAR); +// int d1 = c1.get(Calendar.DAY_OF_MONTH); + + Calendar c2 = Calendar.getInstance(); + c2.setTimeInMillis(now() - 104400000); + int y2 = c2.get(Calendar.YEAR); + int m2 = c2.get(Calendar.MONTH) + 1; + int w2 = c2.get(Calendar.WEEK_OF_YEAR); +// int d2 = c2.get(Calendar.DAY_OF_MONTH); + + return (y1 == y2) && (m1 == m2) && (w1 == w2); + } + + // 是否同一月 + public static boolean isSameMonthWithNow(long r1) { + Calendar c1 = Calendar.getInstance(); + c1.setTimeInMillis(r1); + int y1 = c1.get(Calendar.YEAR); + int m1 = c1.get(Calendar.MONTH) + 1; + + Calendar c2 = Calendar.getInstance(); + c2.setTimeInMillis(now()); + int y2 = c2.get(Calendar.YEAR); + int m2 = c2.get(Calendar.MONTH) + 1; +// int d2 = c2.get(Calendar.DAY_OF_MONTH); + + return (y1 == y2) && (m1 == m2); + } + + + /** + * 获得当天指定时间 + */ + public static long getAppointTime(int hour) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(now()); + calendar.set(Calendar.HOUR_OF_DAY, hour); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTimeInMillis(); + } + + public static long getAppointTimeInXDay(long date, int hour) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(date); + calendar.set(Calendar.HOUR_OF_DAY, hour); + calendar.set(Calendar.MINUTE, 0); + calendar.set(Calendar.SECOND, 0); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTimeInMillis(); + } + + /** + * 获得当天指定时间 + */ + public static long getAppointTime(int hour, int minute) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(now()); + calendar.set(Calendar.HOUR_OF_DAY, hour); + calendar.set(Calendar.MINUTE, minute); + calendar.set(Calendar.SECOND, 0); + + return calendar.getTimeInMillis(); + } + + + /** + * @param time 00:00:00 + * @return + */ + public static long getAppointTimeWithString(String time) { + String[] times = time.split(":"); + if (times.length < 3) { + return 0; + } + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(now()); + calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0])); + calendar.set(Calendar.MINUTE, Integer.parseInt(times[1])); + calendar.set(Calendar.SECOND, Integer.parseInt(times[2])); + calendar.set(Calendar.MILLISECOND, 0); + return calendar.getTimeInMillis(); + } + + /** + * 现在时间是否超过当天指定时间 + * hour:24 + */ + public boolean isOverTime(int hour) { + return now() - getAppointTime(hour) >= (long) (hour) * 3600 * 1000; + } + + public static int getMounth() { + Calendar c1 = Calendar.getInstance(); + c1.setTimeInMillis(now()); + return c1.get(Calendar.MONTH) + 1; + } + + public static int getMounthOfDay() { + Calendar c1 = Calendar.getInstance(); + c1.setTimeInMillis(now()); + return c1.get(Calendar.DAY_OF_MONTH); + } + + + public static long getInterval(long sometime) { + return sometime - now(); + } + + /** + * 获得目标时间是今天的几点(整点) + * + * @param time + * @return + */ + public static int getHourOfDay(long time) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(time); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + return hour; + } + + /** + * date2比date1多的天数 + * + * @param date1 + * @param date2 + * @return + */ + public static int differentDays(long date1, long date2) { + Calendar cal1 = Calendar.getInstance(); + cal1.setTimeInMillis(date1); + + Calendar cal2 = Calendar.getInstance(); + cal2.setTimeInMillis(date2); + int day1 = cal1.get(Calendar.DAY_OF_YEAR); + int day2 = cal2.get(Calendar.DAY_OF_YEAR); + + int year1 = cal1.get(Calendar.YEAR); + int year2 = cal2.get(Calendar.YEAR); + if (year1 != year2) { //不同年 + int timeDistance = 0; + for (int i = year1; i < year2; i++) { + if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { ////闰年 + timeDistance += 366; + } else { //不是闰年 + timeDistance += 365; + } + } + return timeDistance + (day2 - day1); + } else { //同一年 + return day2 - day1; + } + } + + public static int differentDaysByHour(long date1, long date2, int hour) { + Calendar cal1 = Calendar.getInstance(); + cal1.setTimeInMillis(date1); + + Calendar cal2 = Calendar.getInstance(); + cal2.setTimeInMillis(date2); + int day1 = cal1.get(Calendar.DAY_OF_YEAR); + int day2 = cal2.get(Calendar.DAY_OF_YEAR); + int hour1 = cal1.get(Calendar.HOUR_OF_DAY); + int hour2 = cal2.get(Calendar.HOUR_OF_DAY); + + int days = 0; + int year1 = cal1.get(Calendar.YEAR); + int year2 = cal2.get(Calendar.YEAR); + if (year1 != year2) { //不同年 + int timeDistance = 0; + for (int i = year1; i < year2; i++) { + if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) { ////闰年 + timeDistance += 366; + } else { //不是闰年 + timeDistance += 365; + } + } + days = timeDistance + (day2 - day1); + } else { //同一年 + days = day2 - day1; + } + if (days == 0 && hour1 < hour && hour2 >= hour) { + days++; + } else if (days > 0 && hour1 < hour && hour2 >= hour) { + days++; + } else if (days > 0 && hour1 >= hour && hour2 < hour) { + days--; + } + return days; + } + + /** + * 获得当前时间是今天的几点(整点) + * + * @return + */ + public static int getHourOfDay() { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(System.currentTimeMillis()); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + return hour; + } + +// /** +// * 获取的一个月的最大的天数 +// * +// * @return +// */ +// public int getDayOfMonth() { +// Calendar aCalendar = Calendar.getInstance(Locale.CHINA); +// int day = aCalendar.getActualMaximum(Calendar.DATE); +// return day; +// } + + /** + * 获取的一个月的最大的天数 + * + * @return + */ + public static long getFirstDayOfNextMonth() { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(now()); + cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); + cal.set(Calendar.DAY_OF_MONTH, 1); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + return cal.getTimeInMillis(); + } + + /** + * 获取周几, 周日 0; 其他正常 + * + * @return + */ + public static int getDayOfWeek() { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(now()); + return calendar.get(Calendar.DAY_OF_WEEK) - 1; + } + + /** + * 获取指定时间 周几, 周日 0; 其他正常 + * + * @return + */ + public static int getDayOfWeek(long time) { + Calendar calendar = Calendar.getInstance(); + calendar.setTimeInMillis(time); + return calendar.get(Calendar.DAY_OF_WEEK) - 1; + } + + public static boolean isOverTime(int hour, long lastTime) { + return !isSameDay(lastTime - (long) (hour) * 3600 * 1000, now() - (long) (hour) * 3600 * 1000); + } + + + // 距离下周几(周日为 1 周一为 2 ...) 零点时间 + public static long getFirstDayOfWeek(int firstDayOfWeek) { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(now()); + cal.setFirstDayOfWeek(firstDayOfWeek); + cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + return cal.getTimeInMillis() + 604800 * 1000; + } + + + /** + * 获取上(下)周周几的日期 + * + * @param firstDayOfWeek {@link Calendar} + * 值范围 SUNDAY, + * MONDAY, TUESDAY, WEDNESDAY, + * THURSDAY, FRIDAY, and SATURDAY + * @param dayOfWeek {@link Calendar} 周日为1 周一为2 --- 周六为7 + * @param weekOffset 周偏移,上周为-1,本周为0,下周为1,以此类推 + * @return + */ + public static Date getDayOfWeek(int firstDayOfWeek, int dayOfWeek, int weekOffset) { + if (dayOfWeek > Calendar.SATURDAY || dayOfWeek < Calendar.SUNDAY) { + return null; + } + if (firstDayOfWeek > Calendar.SATURDAY || firstDayOfWeek < Calendar.SUNDAY) { + return null; + } + Calendar date = Calendar.getInstance(Locale.CHINA); + date.setFirstDayOfWeek(firstDayOfWeek); + //周数减一,即上周 + date.add(Calendar.WEEK_OF_MONTH, weekOffset); + //日子设为周几 + date.set(Calendar.DAY_OF_WEEK, dayOfWeek); + //时分秒全部置0 + date.set(Calendar.HOUR_OF_DAY, 5); + date.set(Calendar.MINUTE, 0); + date.set(Calendar.SECOND, 0); + date.set(Calendar.MILLISECOND, 0); + return date.getTime(); + } + + public static int getWeekOfYear(long time) { + Calendar calendar = Calendar.getInstance(); + calendar.setFirstDayOfWeek(Calendar.MONDAY); + calendar.setTimeInMillis(time); + return calendar.get(Calendar.WEEK_OF_YEAR); + } + + /** + * 返回上个月某天某时 + * + * @param year 0为当前年 + * @param month + * @param dayOfMonth + * @param timeInDay 00:00:00 + * @return + */ + public static long getDayOfMonthTime(int year, int month, int dayOfMonth, String timeInDay) { + String[] times = timeInDay.split(":"); + if (times.length < 3) { + return 0; + } + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(now()); + cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + year); + cal.set(Calendar.MONTH, month); + cal.set(Calendar.DAY_OF_MONTH, dayOfMonth); + cal.set(Calendar.HOUR_OF_DAY, Integer.parseInt(times[0])); + cal.set(Calendar.MINUTE, Integer.parseInt(times[1])); + cal.set(Calendar.SECOND, Integer.parseInt(times[2])); + cal.set(Calendar.MILLISECOND, 0); + return cal.getTimeInMillis(); + } + + public static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (Exception e) { + LOGGER.error("sleep->msg=null", e.getMessage(), e); + } + } + +} \ No newline at end of file