11package common .base .utils ;
22
33import java .text .SimpleDateFormat ;
4+ import java .util .Calendar ;
45import java .util .Date ;
56import java .util .Locale ;
67
@@ -35,7 +36,7 @@ public static String getFormatTimeForNow(String timePattern){
3536 *以yyyy-MM-dd HH:mm:ss eg.: 1988-11-08 18:08:08的方式显示
3637 * @return
3738 */
38- public static String getCurTimeStr () {
39+ public static String geNowTimeStr () {
3940 return getFormatTimeForNow (NORMAL_TIME_FORMAT );
4041 }
4142
@@ -46,17 +47,17 @@ public static String formatMillsTimes(String millsTime, String timeFormat) {
4647 if (timeFormat == null ) {
4748 timeFormat = NORMAL_TIME_FORMAT ;
4849 }
49- int serverTimeLen = millsTime .length ();
50- long localSystemTime = System .currentTimeMillis ();
51- int localSystemTimeLen = (localSystemTime + "" ).length ();
52- int timeGap = localSystemTimeLen - serverTimeLen ;
53- String appendedZero = "" ;
54- while (timeGap -- > 0 ) {
55- appendedZero += "0" ;
56- }
57- millsTime += appendedZero ;
50+ // int serverTimeLen = millsTime.length();
51+ // long localSystemTime = System.currentTimeMillis();
52+ // int localSystemTimeLen = (localSystemTime + "").length();
53+ // int timeGap = localSystemTimeLen - serverTimeLen;
54+ // String appendedZero = "";
55+ // while (timeGap-- > 0) {
56+ // appendedZero += "0";
57+ // }
58+ // millsTime+= appendedZero;
5859 SimpleDateFormat sdf = new SimpleDateFormat (timeFormat ,Locale .getDefault ());
59- return sdf .format (convetStr2Date (millsTime ));
60+ return sdf .format (convetStr2Date (diffLocalMillisTimeAndAppend0 ( millsTime ) ));
6061 }
6162
6263 /**
@@ -71,4 +72,121 @@ public static Date convetStr2Date(String dateStr) {
7172 Date theDate = new Date (Long .parseLong (dateStr ));
7273 return theDate ;
7374 }
75+
76+ /***
77+ * 将一个毫秒表示的时间字符串与系统本地的毫秒时间字符串比较,如果参数的毫秒时间比本地的毫秒时间字符数短,则补全相差数量的“0”字符
78+ * 以供本地系统能正确解析完整时间
79+ * @param maybeShortMillisTime 字符长度可能短于本地的毫秒时间字符串的毫秒时间字符串
80+ * @return 原字符串后补全"0"的新毫秒时间字符串
81+ */
82+ private static String diffLocalMillisTimeAndAppend0 (String maybeShortMillisTime ) {
83+ String resultMillisTime = "" ;
84+ if (maybeShortMillisTime != null && maybeShortMillisTime .trim ().length () > 0 ) {
85+ String localMillisTime = System .currentTimeMillis () + "" ;
86+ resultMillisTime = maybeShortMillisTime ;
87+ int maybeShortMillisTimeStrLen = maybeShortMillisTime .length ();
88+ int timeStrLenGap = localMillisTime .length () - maybeShortMillisTimeStrLen ;
89+ String appendedZero = "" ;
90+ while (timeStrLenGap -- > 0 ) {
91+ appendedZero += "0" ;
92+ }
93+ resultMillisTime += appendedZero ;
94+ }
95+ return resultMillisTime ;
96+ }
97+
98+ /***
99+ * 根据一个毫秒时间字符串,获取这个时间的二维信息描述
100+ * 这里只获取1、该时间距离本地系统当前时间是今天、昨天、然后星期;2、月-日信息
101+ * 注:如果是服务器给的时间,则本地去获取是否为今天、昨天,有不准的风险,因为当前系统时间用户可以随便调整,导致比较时间基线变化
102+ * @param theGiveMillisTime 所给的毫秒时间串
103+ * @return new String[]{"今天","11-25"}
104+ */
105+ public static String [] getTime2DDescInfos (String theGiveMillisTime ) {
106+ String [] twoDimensionDesc = {
107+ "" ,""
108+ };
109+ String standardMillisTime = diffLocalMillisTimeAndAppend0 (theGiveMillisTime );
110+ long standardMillis = 0 ;
111+ try {
112+ standardMillis = Long .parseLong (standardMillisTime );
113+ Calendar calendar = Calendar .getInstance ();//这里拿到的也就是系统当前日历时间
114+ long nowMillisTime = System .currentTimeMillis ();//系统当前毫秒时间:距离(UTC时间)1970-1-1 00:00:00的毫秒数
115+ //本地系统的时间所在时区与UTC对比偏移的毫秒数,比如,当前系统时区为在中国,则所在时区为8,系统初始时间则为1970-1-1 08:00:00开始计算
116+ int curTimeZoneRawOffsetMillis = calendar .getTimeZone ().getRawOffset ();
117+
118+ //就是系统当前时间 距离所在时区的初始时间 偏移了多少天,说白了就是现在已经过了多少天
119+ long daysOffsetToday = (nowMillisTime + curTimeZoneRawOffsetMillis ) / 86400000 ;
120+ //所给出的时间距离 所在时区的初始时间 偏移了多少天,说白了就是所给出的时间已经过了多少天
121+ long theTimeOffsetDays = (standardMillis + curTimeZoneRawOffsetMillis )/86400000 ;
122+ long gapDays = theTimeOffsetDays - daysOffsetToday ;
123+ calendar .setTimeInMillis (standardMillis );//把日历切换到参数所给的时间
124+ String desc1 = "" ;
125+ if (gapDays == 0 ) {
126+ desc1 = "今天" ;
127+ } else if (gapDays == -1 ) {
128+ desc1 = "昨天" ;
129+ } else if (gapDays == -2 ) {
130+ desc1 = "前天" ;
131+ }
132+ else {
133+ //拿到周几的信息
134+ int dayOfWeek = calendar .get (Calendar .DAY_OF_WEEK );
135+ desc1 = dayOfWeekDesc (dayOfWeek );
136+ }
137+ int minute = calendar .get (Calendar .MINUTE );
138+ String desc2 = calendar .get (Calendar .HOUR_OF_DAY ) + ":" + (minute < 10 ? "0" + minute : minute );
139+ twoDimensionDesc [0 ] = desc1 ;
140+ twoDimensionDesc [1 ] = desc2 ;
141+ } catch (Exception ignored ) {
142+
143+ }
144+ return twoDimensionDesc ;
145+ }
146+
147+ /**
148+ * 得到所给的参数时间与当前系统今天的间隔天数
149+ * @param theMillisTime 所给的毫秒时间 注该毫秒需要与Java系统毫秒时间位数一致
150+ * @return 0:所给的时间即为【今天】;1:所给的比今天还多一天,即为【明天】;2:后天;-1:所给的时间比今天少一天即为【昨天】;-2:前天;
151+ */
152+ public static long getDiffNowDayNum (String theMillisTime ){
153+ Calendar nowCalendar = Calendar .getInstance ();//这里拿到的也就是系统当前日历时间
154+ //本地系统的时间所在时区与UTC对比偏移的毫秒数,比如,当前系统时区为在中国,则所在时区为8,系统初始时间则为1970-1-1 08:00:00开始计算
155+ //则偏移的毫秒数应该为 8(时) * 60(分) * 60(秒) * 1000(毫秒)
156+ int curTimeZoneRawOffsetMillis = nowCalendar .getTimeZone ().getRawOffset ();
157+ //就是系统当前时间 距离所在时区的初始时间 偏移了多少天,说白了就是现在已经过了多少天
158+ long daysOffsetToday = (System .currentTimeMillis () + curTimeZoneRawOffsetMillis ) / 86400000 ;
159+
160+ long theTimeMilliSecends = Long .parseLong (theMillisTime );
161+ //计算参数中所给的时间距离所在时区的初始时间 偏移了多少天,说白了就是theMillisTime这个时间已经过了多少天
162+ long theTimeOffsetDays = (theTimeMilliSecends + curTimeZoneRawOffsetMillis )/86400000 ;
163+ return theTimeOffsetDays - daysOffsetToday ;//参数时间 的已过天数 - 现在已过的天数;
164+ }
165+
166+ private static String dayOfWeekDesc (int dayOfWeek ) {
167+ String desc = "周一" ;
168+ switch (dayOfWeek ) {
169+ case Calendar .MONDAY :
170+ break ;
171+ case Calendar .TUESDAY :
172+ desc = "周二" ;
173+ break ;
174+ case Calendar .WEDNESDAY :
175+ desc = "周三" ;
176+ break ;
177+ case Calendar .THURSDAY :
178+ desc = "周四" ;
179+ break ;
180+ case Calendar .FRIDAY :
181+ desc = "周五" ;
182+ break ;
183+ case Calendar .SATURDAY :
184+ desc = "周六" ;
185+ break ;
186+ case Calendar .SUNDAY :
187+ desc = "周日" ;
188+ break ;
189+ }
190+ return desc ;
191+ }
74192}
0 commit comments