From fdfa70c76514ecea67cb2ca971fc69a6fe1128fe Mon Sep 17 00:00:00 2001 From: redkale Date: Sat, 11 Nov 2023 11:00:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Times?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/boot/Application.java | 2 +- .../org/redkale/boot/LoggingFileHandler.java | 8 +- .../redkale/boot/LoggingSearchHandler.java | 6 +- src/main/java/org/redkale/source/Range.java | 6 +- src/main/java/org/redkale/util/Times.java | 372 ++++++++++++++++++ src/main/java/org/redkale/util/Utility.java | 151 +++---- .../org/redkale/test/service/CService.java | 8 +- .../org/redkale/test/source/LoginRecord.java | 3 +- 8 files changed, 435 insertions(+), 121 deletions(-) create mode 100644 src/main/java/org/redkale/util/Times.java diff --git a/src/main/java/org/redkale/boot/Application.java b/src/main/java/org/redkale/boot/Application.java index d2abb28e9..a3108c513 100644 --- a/src/main/java/org/redkale/boot/Application.java +++ b/src/main/java/org/redkale/boot/Application.java @@ -2014,7 +2014,7 @@ public final class Application { } public static void main(String[] args) throws Exception { - Utility.midnight(); //先初始化一下Utility + Times.midnight(); //先初始化一下Utility Thread.currentThread().setName("Redkale-Application-Main-Thread"); //运行主程序 { diff --git a/src/main/java/org/redkale/boot/LoggingFileHandler.java b/src/main/java/org/redkale/boot/LoggingFileHandler.java index 3d4a509c9..bf55d6001 100644 --- a/src/main/java/org/redkale/boot/LoggingFileHandler.java +++ b/src/main/java/org/redkale/boot/LoggingFileHandler.java @@ -180,14 +180,14 @@ public class LoggingFileHandler extends LoggingBaseHandler { } if (logstream == null) { logindex.incrementAndGet(); - logfile = new File(patternDateFormat == null ? pattern : Utility.formatTime(patternDateFormat, -1, System.currentTimeMillis())); + logfile = new File(patternDateFormat == null ? pattern : Times.formatTime(patternDateFormat, -1, System.currentTimeMillis())); logfile.getParentFile().mkdirs(); logLength.set(logfile.length()); logstream = new FileOutputStream(logfile, append); } if (unusual != null && logunusualstream == null) { logunusualindex.incrementAndGet(); - logunusualfile = new File(unusualDateFormat == null ? unusual : Utility.formatTime(unusualDateFormat, -1, System.currentTimeMillis())); + logunusualfile = new File(unusualDateFormat == null ? unusual : Times.formatTime(unusualDateFormat, -1, System.currentTimeMillis())); logunusualfile.getParentFile().mkdirs(); logUnusualLength.set(logunusualfile.length()); logunusualstream = new FileOutputStream(logunusualfile, append); @@ -234,7 +234,7 @@ public class LoggingFileHandler extends LoggingBaseHandler { } if (this.pattern != null && this.pattern.contains("%")) { //需要时间格式化 this.patternDateFormat = this.pattern; - Utility.formatTime(this.patternDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确 + Times.formatTime(this.patternDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确 } String unusualstr = manager.getProperty(cname + ".unusual"); if (unusualstr != null) { @@ -247,7 +247,7 @@ public class LoggingFileHandler extends LoggingBaseHandler { } if (this.unusual != null && this.unusual.contains("%")) { //需要时间格式化 this.unusualDateFormat = this.unusual; - Utility.formatTime(this.unusualDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确 + Times.formatTime(this.unusualDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确 } String limitstr = manager.getProperty(cname + ".limit"); try { diff --git a/src/main/java/org/redkale/boot/LoggingSearchHandler.java b/src/main/java/org/redkale/boot/LoggingSearchHandler.java index d22a6387a..ed357b3ef 100644 --- a/src/main/java/org/redkale/boot/LoggingSearchHandler.java +++ b/src/main/java/org/redkale/boot/LoggingSearchHandler.java @@ -163,7 +163,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler { this.tag = tagstr.replace("${" + RESNAME_APP_NAME + "}", System.getProperty(RESNAME_APP_NAME, "")); if (this.tag.contains("%")) { this.tagDateFormat = this.tag; - Utility.formatTime(this.tagDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确 + Times.formatTime(this.tagDateFormat, -1, System.currentTimeMillis()); //测试时间格式是否正确 } } @@ -233,7 +233,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler { break; } } - String rawTag = tagDateFormat == null ? tag : Utility.formatTime(tagDateFormat, -1, log.getInstant().toEpochMilli()); + String rawTag = tagDateFormat == null ? tag : Times.formatTime(tagDateFormat, -1, log.getInstant().toEpochMilli()); fillLogRecord(log); logqueue.offer(new SearchLogRecord(rawTag, log)); } @@ -317,7 +317,7 @@ public class LoggingSearchHandler extends LoggingBaseHandler { this.loggerName = log.getLoggerName(); this.methodName = log.getSourceClassName() + " " + log.getSourceMethodName(); this.timestamp = log.getInstant().toEpochMilli(); - this.logid = Utility.format36time(timestamp) + "_" + Utility.uuid(); + this.logid = Times.format36time(timestamp) + "_" + Utility.uuid(); } @Override diff --git a/src/main/java/org/redkale/source/Range.java b/src/main/java/org/redkale/source/Range.java index 69cb83278..f694c000a 100644 --- a/src/main/java/org/redkale/source/Range.java +++ b/src/main/java/org/redkale/source/Range.java @@ -7,7 +7,7 @@ package org.redkale.source; import java.util.function.*; import org.redkale.convert.ConvertColumn; -import org.redkale.util.Utility; +import org.redkale.util.Times; /** * @@ -209,12 +209,12 @@ public interface Range extends java.io.Serializable, Predi } public static LongRange todayRange() { - long min = Utility.midnight(); + long min = Times.midnight(); return new LongRange(min, min + 24 * 60 * 60 * 1000 - 1); } public static LongRange yesterdayRange() { - long min = Utility.midnight(System.currentTimeMillis() - 24 * 60 * 60 * 1000); + long min = Times.midnight(System.currentTimeMillis() - 24 * 60 * 60 * 1000); return new LongRange(min, min + 24 * 60 * 60 * 1000 - 1); } diff --git a/src/main/java/org/redkale/util/Times.java b/src/main/java/org/redkale/util/Times.java new file mode 100644 index 000000000..2ad535af6 --- /dev/null +++ b/src/main/java/org/redkale/util/Times.java @@ -0,0 +1,372 @@ +/* + * + */ +package org.redkale.util; + +import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneId; +import java.util.Calendar; +import java.util.TimeZone; + +/** + * 时间日期工具类 + * + *

+ * 详情见: https://redkale.org + * + * @author zhangjx + * @since 2.8.0 + */ +public class Times { + + private static final int ZONE_RAW_OFFSET = TimeZone.getDefault().getRawOffset(); + + static final String FORMAT_SECONDS = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"; //yyyy-MM-dd HH:mm:ss + + static final String FORMAT_MILLS = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL"; //yyyy-MM-dd HH:mm:ss.fff + + private Times() { + //do nothind + } + + /** + * 获取格式为yyyy-MM-dd HH:mm:ss的当前时间 + * + * @return 格式为yyyy-MM-dd HH:mm:ss的时间值 + */ + public static String now() { + return String.format(FORMAT_SECONDS, System.currentTimeMillis()); + } + + /** + * 获取格式为yyyy-MM-dd HH:mm:ss.fff的当前时间 + * + * @return 格式为yyyy-MM-dd HH:mm:ss.fff的时间值 + */ + public static String nowMillis() { + return String.format(FORMAT_MILLS, System.currentTimeMillis()); + } + + /** + * 将指定时间格式化为 yyyy-MM-dd HH:mm:ss + * + * @param time 待格式化的时间 + * + * @return 格式为yyyy-MM-dd HH:mm:ss的时间值 + */ + public static String formatTime(long time) { + return String.format(FORMAT_SECONDS, time); + } + + /** + * 将时间值转换为长度为9的36进制值 + * + * @param time 时间值 + * + * @return 36进制时间值 + */ + public static String format36time(long time) { + return Long.toString(time, 36); + } + + /** + * 获取当天凌晨零点的格林时间 + * + * @return 毫秒数 + */ + public static long midnight() { + return midnight(System.currentTimeMillis()); + } + + /** + * 获取指定时间当天凌晨零点的格林时间 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static long midnight(long time) { + return (time + ZONE_RAW_OFFSET) / 86400000 * 86400000 - ZONE_RAW_OFFSET; + } + + /** + * 获取当天20151231格式的int值 + * + * @return 20151231格式的int值 + */ + public static int today() { + java.time.LocalDate today = java.time.LocalDate.now(); + return today.getYear() * 10000 + + today.getMonthValue() * 100 + + today.getDayOfMonth(); + } + + /** + * 获取当天151231格式的int值 + * + * @return 151231格式的int值 + */ + public static int todayYYMMDD() { + java.time.LocalDate today = java.time.LocalDate.now(); + return today.getYear() % 100 * 10000 + + today.getMonthValue() * 100 + + today.getDayOfMonth(); + } + + /** + * 获取当天1512312359格式的int值 + * + * @return 1512312359格式的int值 + */ + public static int todayYYMMDDHHmm() { + java.time.LocalDateTime today = java.time.LocalDateTime.now(); + return today.getYear() % 100 * 100_00_00_00 + + today.getMonthValue() * 100_00_00 + + today.getDayOfMonth() * 100_00 + + today.getHour() * 100 + + today.getMinute(); + } + + /** + * 获取当天20151231235959格式的int值 + * + * @return 20151231235959格式的int值 + */ + public static long todayYYYYMMDDHHmmss() { + java.time.LocalDateTime today = java.time.LocalDateTime.now(); + return today.getYear() * 100_00_00_00_00L + + today.getMonthValue() * 100_00_00_00 + + today.getDayOfMonth() * 100_00_00 + + today.getHour() * 100_00 + + today.getMinute() * 100 + + today.getSecond(); + } + + /** + * 获取当天151231235959格式的int值 + * + * @return 151231235959格式的int值 + */ + public static long todayYYMMDDHHmmss() { + java.time.LocalDateTime today = java.time.LocalDateTime.now(); + return today.getYear() % 100 * 100_00_00_00_00L + + today.getMonthValue() * 100_00_00_00 + + today.getDayOfMonth() * 100_00_00 + + today.getHour() * 100_00 + + today.getMinute() * 100 + + today.getSecond(); + } + + /** + * 获取明天20151230格式的int值 + * + * @return 20151230格式的int值 + */ + public static int tomorrow() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_YEAR, 1); + return cal.get(Calendar.YEAR) * 10000 + + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 获取明天151230格式的int值 + * + * @return 151230格式的int值 + */ + public static int tomorrowYYMMDD() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_YEAR, 1); + return cal.get(Calendar.YEAR) % 100 * 10000 + + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 获取昨天20151230格式的int值 + * + * @return 20151230格式的int值 + */ + public static int yesterday() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_YEAR, -1); + return cal.get(Calendar.YEAR) * 10000 + + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 获取昨天151230格式的int值 + * + * @return 151230格式的int值 + */ + public static int yesterdayYYMMDD() { + Calendar cal = Calendar.getInstance(); + cal.add(Calendar.DAY_OF_YEAR, -1); + return cal.get(Calendar.YEAR) % 100 * 10000 + + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 获取指定时间的20160202格式的int值 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static int yyyyMMdd(long time) { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(time); + return cal.get(Calendar.YEAR) * 10000 + + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 获取指定时间的160202格式的int值 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static int yyMMdd(long time) { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(time); + return cal.get(Calendar.YEAR) % 100 * 10000 + + (cal.get(Calendar.MONTH) + 1) * 100 + + cal.get(Calendar.DAY_OF_MONTH); + } + + /** + * 获取当天16020223格式的int值 + * + * @param time 指定时间 + * + * @return 16020223格式的int值 + */ + public static int yyMMDDHHmm(long time) { + Calendar cal = Calendar.getInstance(); + cal.setTimeInMillis(time); + return cal.get(Calendar.YEAR) % 100 * 100_00_00 + + (cal.get(Calendar.MONTH) + 1) * 100_00 + + cal.get(Calendar.DAY_OF_MONTH) * 100 + + cal.get(Calendar.HOUR_OF_DAY); + } + + /** + * 获取时间点所在星期的周一 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static long monday(long time) { + ZoneId zid = ZoneId.systemDefault(); + Instant instant = Instant.ofEpochMilli(time); + LocalDate ld = instant.atZone(zid).toLocalDate(); + ld = ld.minusDays(ld.getDayOfWeek().getValue() - 1); + return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + } + + /** + * 获取时间点所在星期的周日 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static long sunday(long time) { + ZoneId zid = ZoneId.systemDefault(); + Instant instant = Instant.ofEpochMilli(time); + LocalDate ld = instant.atZone(zid).toLocalDate(); + ld = ld.plusDays(7 - ld.getDayOfWeek().getValue()); + return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + } + + /** + * 获取时间点所在月份的1号 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static long monthFirstDay(long time) { + ZoneId zid = ZoneId.systemDefault(); + Instant instant = Instant.ofEpochMilli(time); + LocalDate ld = instant.atZone(zid).toLocalDate().withDayOfMonth(1); + return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + } + + /** + * 获取时间点所在月份的最后一天 + * + * @param time 指定时间 + * + * @return 毫秒数 + */ + public static long monthLastDay(long time) { + ZoneId zid = ZoneId.systemDefault(); + Instant instant = Instant.ofEpochMilli(time); + LocalDate ld = instant.atZone(zid).toLocalDate(); + ld = ld.withDayOfMonth(ld.lengthOfMonth()); + return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + } + + /** + * 将时间格式化, 支持%1$ty 和 %ty两种格式 + * + * @param format 格式 + * @param size 带%t的个数,值小于0则需要计算 + * @param time 时间 + * + * @since 2.7.0 + * + * @return 时间格式化 + */ + public static String formatTime(String format, int size, Object time) { + if (size < 0) { + int c = 0; + if (!format.contains("%1$")) { + for (char ch : format.toCharArray()) { + if (ch == '%') { + c++; + } + } + } + size = c; + } + if (size <= 1) { + return String.format(format, time); + } + if (size == 2) { + return String.format(format, time, time); + } + if (size == 3) { + return String.format(format, time, time, time); + } + if (size == 4) { + return String.format(format, time, time, time, time); + } + if (size == 5) { + return String.format(format, time, time, time, time, time); + } + if (size == 6) { + return String.format(format, time, time, time, time, time, time); + } + if (size == 7) { + return String.format(format, time, time, time, time, time, time, time); + } + if (size == 8) { + return String.format(format, time, time, time, time, time, time, time); + } + Object[] args = new Object[size]; + for (int i = 0; i < size; i++) { + args[i] = time; + } + return String.format(format, args); + } + +} diff --git a/src/main/java/org/redkale/util/Utility.java b/src/main/java/org/redkale/util/Utility.java index 2b89d2957..7d10362de 100644 --- a/src/main/java/org/redkale/util/Utility.java +++ b/src/main/java/org/redkale/util/Utility.java @@ -39,12 +39,6 @@ import org.redkale.convert.json.JsonConvert; */ public final class Utility { - private static final int zoneRawOffset = TimeZone.getDefault().getRawOffset(); - - static final String format1 = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS"; //yyyy-MM-dd HH:mm:ss - - static final String format2 = "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL"; //yyyy-MM-dd HH:mm:ss.fff - private static final char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; private static final int cpus = Integer.getInteger("redkale.cpus", Runtime.getRuntime().availableProcessors()); @@ -3504,8 +3498,9 @@ public final class Utility { * * @return 格式为yyyy-MM-dd HH:mm:ss的时间值 */ + @Deprecated(since = "2.8.0") public static String now() { - return String.format(format1, System.currentTimeMillis()); + return Times.now(); } /** @@ -3513,8 +3508,9 @@ public final class Utility { * * @return 格式为yyyy-MM-dd HH:mm:ss.fff的时间值 */ + @Deprecated(since = "2.8.0") public static String nowMillis() { - return String.format(format2, System.currentTimeMillis()); + return Times.nowMillis(); } /** @@ -3524,8 +3520,9 @@ public final class Utility { * * @return 格式为yyyy-MM-dd HH:mm:ss的时间值 */ + @Deprecated(since = "2.8.0") public static String formatTime(long time) { - return String.format(format1, time); + return Times.formatTime(time); } /** @@ -3535,8 +3532,9 @@ public final class Utility { * * @return 36进制时间值 */ + @Deprecated(since = "2.8.0") public static String format36time(long time) { - return Long.toString(time, 36); + return Times.format36time(time); } /** @@ -3544,8 +3542,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static long midnight() { - return midnight(System.currentTimeMillis()); + return Times.midnight(); } /** @@ -3555,8 +3554,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static long midnight(long time) { - return (time + zoneRawOffset) / 86400000 * 86400000 - zoneRawOffset; + return Times.midnight(time); } /** @@ -3564,9 +3564,9 @@ public final class Utility { * * @return 20151231格式的int值 */ + @Deprecated(since = "2.8.0") public static int today() { - java.time.LocalDate today = java.time.LocalDate.now(); - return today.getYear() * 10000 + today.getMonthValue() * 100 + today.getDayOfMonth(); + return Times.today(); } /** @@ -3574,9 +3574,9 @@ public final class Utility { * * @return 151231格式的int值 */ + @Deprecated(since = "2.8.0") public static int todayYYMMDD() { - java.time.LocalDate today = java.time.LocalDate.now(); - return today.getYear() % 100 * 10000 + today.getMonthValue() * 100 + today.getDayOfMonth(); + return Times.todayYYMMDD(); } /** @@ -3584,10 +3584,9 @@ public final class Utility { * * @return 1512312359格式的int值 */ + @Deprecated(since = "2.8.0") public static int todayYYMMDDHHmm() { - java.time.LocalDateTime today = java.time.LocalDateTime.now(); - return today.getYear() % 100 * 100_00_00_00 + today.getMonthValue() * 100_00_00 + today.getDayOfMonth() * 100_00 - + today.getHour() * 100 + today.getMinute(); + return Times.todayYYMMDDHHmm(); } /** @@ -3595,10 +3594,9 @@ public final class Utility { * * @return 20151231235959格式的int值 */ + @Deprecated(since = "2.8.0") public static long todayYYYYMMDDHHmmss() { - java.time.LocalDateTime today = java.time.LocalDateTime.now(); - return today.getYear() * 100_00_00_00_00L + today.getMonthValue() * 100_00_00_00 + today.getDayOfMonth() * 100_00_00 - + today.getHour() * 100_00 + today.getMinute() * 100 + today.getSecond(); + return Times.todayYYYYMMDDHHmmss(); } /** @@ -3606,10 +3604,9 @@ public final class Utility { * * @return 151231235959格式的int值 */ + @Deprecated(since = "2.8.0") public static long todayYYMMDDHHmmss() { - java.time.LocalDateTime today = java.time.LocalDateTime.now(); - return today.getYear() % 100 * 100_00_00_00_00L + today.getMonthValue() * 100_00_00_00 + today.getDayOfMonth() * 100_00_00 - + today.getHour() * 100_00 + today.getMinute() * 100 + today.getSecond(); + return Times.todayYYMMDDHHmmss(); } /** @@ -3617,10 +3614,9 @@ public final class Utility { * * @return 20151230格式的int值 */ + @Deprecated(since = "2.8.0") public static int tomorrow() { - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_YEAR, 1); - return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + return Times.tomorrow(); } /** @@ -3628,10 +3624,9 @@ public final class Utility { * * @return 151230格式的int值 */ + @Deprecated(since = "2.8.0") public static int tomorrowYYMMDD() { - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_YEAR, 1); - return cal.get(Calendar.YEAR) % 100 * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + return Times.tomorrowYYMMDD(); } /** @@ -3639,10 +3634,9 @@ public final class Utility { * * @return 20151230格式的int值 */ + @Deprecated(since = "2.8.0") public static int yesterday() { - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_YEAR, -1); - return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + return Times.yesterday(); } /** @@ -3650,10 +3644,9 @@ public final class Utility { * * @return 151230格式的int值 */ + @Deprecated(since = "2.8.0") public static int yesterdayYYMMDD() { - Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_YEAR, -1); - return cal.get(Calendar.YEAR) % 100 * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + return Times.yesterdayYYMMDD(); } /** @@ -3663,10 +3656,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static int yyyyMMdd(long time) { - Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(time); - return cal.get(Calendar.YEAR) * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + return Times.yyyyMMdd(time); } /** @@ -3676,10 +3668,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static int yyMMdd(long time) { - Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(time); - return cal.get(Calendar.YEAR) % 100 * 10000 + (cal.get(Calendar.MONTH) + 1) * 100 + cal.get(Calendar.DAY_OF_MONTH); + return Times.yyMMdd(time); } /** @@ -3689,10 +3680,9 @@ public final class Utility { * * @return 16020223格式的int值 */ + @Deprecated(since = "2.8.0") public static int yyMMDDHHmm(long time) { - Calendar cal = Calendar.getInstance(); - cal.setTimeInMillis(time); - return cal.get(Calendar.YEAR) % 100 * 100_00_00 + (cal.get(Calendar.MONTH) + 1) * 100_00 + cal.get(Calendar.DAY_OF_MONTH) * 100 + cal.get(Calendar.HOUR_OF_DAY); + return Times.yyMMDDHHmm(time); } /** @@ -3702,12 +3692,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static long monday(long time) { - ZoneId zid = ZoneId.systemDefault(); - Instant instant = Instant.ofEpochMilli(time); - LocalDate ld = instant.atZone(zid).toLocalDate(); - ld = ld.minusDays(ld.getDayOfWeek().getValue() - 1); - return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + return Times.monday(time); } /** @@ -3717,12 +3704,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static long sunday(long time) { - ZoneId zid = ZoneId.systemDefault(); - Instant instant = Instant.ofEpochMilli(time); - LocalDate ld = instant.atZone(zid).toLocalDate(); - ld = ld.plusDays(7 - ld.getDayOfWeek().getValue()); - return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + return Times.sunday(time); } /** @@ -3732,11 +3716,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static long monthFirstDay(long time) { - ZoneId zid = ZoneId.systemDefault(); - Instant instant = Instant.ofEpochMilli(time); - LocalDate ld = instant.atZone(zid).toLocalDate().withDayOfMonth(1); - return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + return Times.monthFirstDay(time); } /** @@ -3746,12 +3728,9 @@ public final class Utility { * * @return 毫秒数 */ + @Deprecated(since = "2.8.0") public static long monthLastDay(long time) { - ZoneId zid = ZoneId.systemDefault(); - Instant instant = Instant.ofEpochMilli(time); - LocalDate ld = instant.atZone(zid).toLocalDate(); - ld = ld.withDayOfMonth(ld.lengthOfMonth()); - return ld.atStartOfDay(zid).toInstant().toEpochMilli(); + return Times.monthLastDay(time); } /** @@ -3765,47 +3744,9 @@ public final class Utility { * * @return 时间格式化 */ + @Deprecated(since = "2.8.0") public static String formatTime(String format, int size, Object time) { - if (size < 0) { - int c = 0; - if (!format.contains("%1$")) { - for (char ch : format.toCharArray()) { - if (ch == '%') { - c++; - } - } - } - size = c; - } - if (size <= 1) { - return String.format(format, time); - } - if (size == 2) { - return String.format(format, time, time); - } - if (size == 3) { - return String.format(format, time, time, time); - } - if (size == 4) { - return String.format(format, time, time, time, time); - } - if (size == 5) { - return String.format(format, time, time, time, time, time); - } - if (size == 6) { - return String.format(format, time, time, time, time, time, time); - } - if (size == 7) { - return String.format(format, time, time, time, time, time, time, time); - } - if (size == 8) { - return String.format(format, time, time, time, time, time, time, time); - } - Object[] args = new Object[size]; - for (int i = 0; i < size; i++) { - args[i] = time; - } - return String.format(format, args); + return Times.formatTime(format, size, time); } /** diff --git a/src/test/java/org/redkale/test/service/CService.java b/src/test/java/org/redkale/test/service/CService.java index 82c4e6c92..61fdf26f5 100644 --- a/src/test/java/org/redkale/test/service/CService.java +++ b/src/test/java/org/redkale/test/service/CService.java @@ -8,7 +8,7 @@ package org.redkale.test.service; import java.nio.channels.CompletionHandler; import org.redkale.annotation.Resource; import org.redkale.service.*; -import org.redkale.util.Utility; +import org.redkale.util.Times; /** * @@ -31,19 +31,19 @@ public class CService implements Service { } public RetResult ccCurrentTime1(final String name) { - String rs = "同步ccCurrentTime1: " + name + ": " + Utility.formatTime(System.currentTimeMillis()); + String rs = "同步ccCurrentTime1: " + name + ": " + Times.formatTime(System.currentTimeMillis()); System.out.println("执行了 CService.ccCurrentTime1++++同步方法1"); return new RetResult(rs); } public void ccCurrentTime2(final CompletionHandler, Void> handler, final String name) { - String rs = "异步ccCurrentTime2: " + name + ": " + Utility.formatTime(System.currentTimeMillis()); + String rs = "异步ccCurrentTime2: " + name + ": " + Times.formatTime(System.currentTimeMillis()); System.out.println("执行了 CService.ccCurrentTime2----异步方法2"); if (handler != null) handler.completed(new RetResult(rs), null); } public void mcCurrentTime3(final MyAsyncHandler, Void> handler, final String name) { - String rs = "异步mcCurrentTime3: " + name + ": " + Utility.formatTime(System.currentTimeMillis()); + String rs = "异步mcCurrentTime3: " + name + ": " + Times.formatTime(System.currentTimeMillis()); System.out.println("执行了 CService.mcCurrentTime3----异步方法3"); if (handler != null) handler.completed(new RetResult(rs), null); } diff --git a/src/test/java/org/redkale/test/source/LoginRecord.java b/src/test/java/org/redkale/test/source/LoginRecord.java index a98e6177d..556435ddd 100644 --- a/src/test/java/org/redkale/test/source/LoginRecord.java +++ b/src/test/java/org/redkale/test/source/LoginRecord.java @@ -8,6 +8,7 @@ package org.redkale.test.source; import java.io.Serializable; import org.redkale.persistence.*; import org.redkale.source.*; +import org.redkale.util.Times; import org.redkale.util.Utility; /** @@ -115,7 +116,7 @@ public class LoginRecord extends BaseEntity { LoginRecord record = new LoginRecord(); long now = System.currentTimeMillis(); record.setCreatetime(now); //设置创建时间 - record.setLoginid(Utility.format36time(now) + "-" + Utility.uuid()); //主键的生成规则 + record.setLoginid(Times.format36time(now) + "-" + Utility.uuid()); //主键的生成规则 //.... 填充其他字段 source.insert(record); }