DateUtil.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * Copyright (c) 2015-2016, Chill Zhuang 庄骞 (smallchill@163.com).
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.example.xiaoshiweixinback.business.utils;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.apache.commons.lang3.time.DateFormatUtils;
  19. import org.apache.commons.lang3.time.DateUtils;
  20. import java.sql.Timestamp;
  21. import java.text.DateFormat;
  22. import java.text.ParseException;
  23. import java.text.SimpleDateFormat;
  24. import java.time.LocalDateTime;
  25. import java.time.ZoneId;
  26. import java.time.ZonedDateTime;
  27. import java.time.temporal.ChronoUnit;
  28. import java.util.Calendar;
  29. import java.util.Date;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import java.util.concurrent.TimeUnit;
  33. public class DateUtil {
  34. /**
  35. * 获取YYYY格式
  36. *
  37. * @return
  38. */
  39. public static String getYear() {
  40. return formatDate(new Date(), "yyyy");
  41. }
  42. /**
  43. * 获取YYYY格式
  44. *
  45. * @return
  46. */
  47. public static String getYear(Date date) {
  48. return formatDate(date, "yyyy");
  49. }
  50. public static String getCurrentServerDate(String format) {
  51. return formatDate(new Date(), format);
  52. }
  53. /**
  54. * 获取YYYY-MM-DD格式
  55. *
  56. * @return
  57. */
  58. public static String getDay() {
  59. return formatDate(new Date(), "yyyy-MM-dd");
  60. }
  61. /**
  62. * 获取YYYY-MM-DD格式
  63. *
  64. * @return
  65. */
  66. public static String getDay(Date date) {
  67. return formatDate(date, "yyyy-MM-dd");
  68. }
  69. /**
  70. * 获取YYYYMMDD格式
  71. *
  72. * @return
  73. */
  74. public static String getDays() {
  75. return formatDate(new Date(), "yyyyMMdd");
  76. }
  77. /**
  78. * 获取YYYYMMDD格式
  79. *
  80. * @return
  81. */
  82. public static String getDays(Date date) {
  83. return formatDate(date, "yyyyMMdd");
  84. }
  85. /**
  86. * 获取YYYY-MM-DD HH:mm:ss格式
  87. *
  88. * @return
  89. */
  90. public static String getTime() {
  91. return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
  92. }
  93. /**
  94. * 获取YYYY-MM-DD HH:mm:ss.SSS格式
  95. *
  96. * @return
  97. */
  98. public static String getMsTime() {
  99. return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
  100. }
  101. /**
  102. * 获取YYYYMMDDHHmmss格式
  103. *
  104. * @return
  105. */
  106. public static String getAllTime() {
  107. return formatDate(new Date(), "yyyyMMddHHmmss");
  108. }
  109. /**
  110. * 获取YYYY-MM-DD HH:mm:ss格式
  111. *
  112. * @return
  113. */
  114. public static String getTime(Date date) {
  115. return formatDate(date, "yyyy-MM-dd HH:mm:ss");
  116. }
  117. public static String formatDate(Date date, String pattern) {
  118. String formatDate = null;
  119. if (StringUtils.isNotBlank(pattern)) {
  120. formatDate = DateFormatUtils.format(date, pattern);
  121. } else {
  122. formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
  123. }
  124. return formatDate;
  125. }
  126. /**
  127. * @Title: compareDate
  128. * @Description:(日期比较,如果s>=e 返回true 否则返回false)
  129. * @param s
  130. * @param e
  131. * @return boolean
  132. * @throws
  133. * @author luguosui
  134. */
  135. public static boolean compareDate(String s, String e) {
  136. if (parseDate(s) == null || parseDate(e) == null) {
  137. return false;
  138. }
  139. return parseDate(s).getTime() >= parseDate(e).getTime();
  140. }
  141. /**
  142. * 格式化日期
  143. *
  144. * @return
  145. */
  146. public static Date parseDate(String date) {
  147. return parse(date,"yyyy-MM-dd");
  148. }
  149. /**
  150. * 格式化日期
  151. *
  152. * @return
  153. */
  154. public static Date parseTime(String date) {
  155. return parse(date,"yyyy-MM-dd HH:mm:ss");
  156. }
  157. /**
  158. * 格式化日期
  159. *
  160. * @return
  161. */
  162. public static Date parse(String date, String pattern) {
  163. try {
  164. return DateUtils.parseDate(date,pattern);
  165. } catch (ParseException e) {
  166. e.printStackTrace();
  167. return null;
  168. }
  169. }
  170. /**
  171. * 格式化日期
  172. *
  173. * @return
  174. */
  175. public static String format(Date date, String pattern) {
  176. return DateFormatUtils.format(date, pattern);
  177. }
  178. /**
  179. * 把日期转换为Timestamp
  180. *
  181. * @param date
  182. * @return
  183. */
  184. public static Timestamp format(Date date) {
  185. return new Timestamp(date.getTime());
  186. }
  187. /**
  188. * 校验日期是否合法
  189. *
  190. * @return
  191. */
  192. public static boolean isValidDate(String s) {
  193. return parse(s, "yyyy-MM-dd HH:mm:ss") != null;
  194. }
  195. /**
  196. * 校验日期是否合法
  197. *
  198. * @return
  199. */
  200. public static boolean isValidDate(String s, String pattern) {
  201. return parse(s, pattern) != null;
  202. }
  203. public static int getDiffYear(String startTime, String endTime) {
  204. DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
  205. try {
  206. int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
  207. startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
  208. return years;
  209. } catch (Exception e) {
  210. // 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
  211. return 0;
  212. }
  213. }
  214. /**
  215. * <li>功能描述:时间相减得到天数
  216. *
  217. * @param beginDateStr
  218. * @param endDateStr
  219. * @return long
  220. * @author Administrator
  221. */
  222. public static long getDaySub(String beginDateStr, String endDateStr) {
  223. long day = 0;
  224. SimpleDateFormat format = new SimpleDateFormat(
  225. "yyyy-MM-dd");
  226. Date beginDate = null;
  227. Date endDate = null;
  228. try {
  229. beginDate = format.parse(beginDateStr);
  230. endDate = format.parse(endDateStr);
  231. } catch (ParseException e) {
  232. e.printStackTrace();
  233. }
  234. day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
  235. // System.out.println("相隔的天数="+day);
  236. return day;
  237. }
  238. /**
  239. * 得到n天之后的日期
  240. *
  241. * @param days
  242. * @return
  243. */
  244. public static String getAfterDayDate(String days) {
  245. int daysInt = Integer.parseInt(days);
  246. Calendar canlendar = Calendar.getInstance(); // java.util包
  247. canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
  248. Date date = canlendar.getTime();
  249. SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  250. String dateStr = sdfd.format(date);
  251. return dateStr;
  252. }
  253. /**
  254. * 得到n天之后是周几
  255. *
  256. * @param days
  257. * @return
  258. */
  259. public static String getAfterDayWeek(String days) {
  260. int daysInt = Integer.parseInt(days);
  261. Calendar canlendar = Calendar.getInstance(); // java.util包
  262. canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
  263. Date date = canlendar.getTime();
  264. SimpleDateFormat sdf = new SimpleDateFormat("E");
  265. String dateStr = sdf.format(date);
  266. return dateStr;
  267. }
  268. /**
  269. * 格式化Oracle Date
  270. * @param value
  271. * @return
  272. */
  273. // public static String buildDateValue(Object value){
  274. // if(Func.isOracle()){
  275. // return "to_date('"+ value +"','yyyy-mm-dd HH24:MI:SS')";
  276. // }else{
  277. // return Func.toStr(value);
  278. // }
  279. // }
  280. /**
  281. * @Description: 获取当前时间的周一及周日
  282. * @Param: Date
  283. * @Author: LHX
  284. * @Date: 9:59 2018/11/19
  285. * @return: java.util.Map<java.lang.String,java.lang.String>
  286. */
  287. public static Map<String,String> getWeekDate(Date date) {
  288. Map<String,String> map = new HashMap();
  289. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  290. Calendar cal = Calendar.getInstance();
  291. cal.setTime(date);
  292. // 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
  293. cal.setFirstDayOfWeek(Calendar.MONDAY);
  294. // 获得当前日期是一个星期的第几天
  295. int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
  296. if(dayWeek==1){
  297. dayWeek = 8;
  298. }
  299. // 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
  300. cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);
  301. Date mondayDate = cal.getTime();
  302. String weekBegin = sdf.format(mondayDate);
  303. //获取星期日
  304. cal.add(Calendar.DATE, 4 +cal.getFirstDayOfWeek());
  305. Date sundayDate = cal.getTime();
  306. String weekEnd = sdf.format(sundayDate);
  307. map.put("mondayDate", weekBegin);
  308. map.put("sundayDate", weekEnd);
  309. return map;
  310. }
  311. public static void main(String[] args) {
  312. System.out.println(getTime(new Date()));
  313. System.out.println(getAfterDayWeek("3"));
  314. }
  315. public static Long getRemainingSecond() {
  316. // 获取当前时间
  317. LocalDateTime now = LocalDateTime.now();
  318. // 计算当天零点的时间
  319. LocalDateTime midnight = now.plusDays(1).truncatedTo(ChronoUnit.DAYS);
  320. // 转换为ZonedDateTime以获取时区信息
  321. ZonedDateTime zonedDateTime = midnight.atZone(ZoneId.systemDefault());
  322. // 转换为Unix时间戳(秒)
  323. long midnightTimestamp = TimeUnit.MILLISECONDS.toSeconds(zonedDateTime.toInstant().toEpochMilli());
  324. // 获取当前时间的Unix时间戳(秒)
  325. long currentTimestamp = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
  326. // 计算剩余秒数直到当天零点
  327. // // 如果已经过了零点,则设置过期时间为0或者重新计算逻辑
  328. // if (ttlSeconds <= 0) {
  329. // ttlSeconds = 0; // 或者你可以设置为其他逻辑,比如下一个天的零点
  330. // }
  331. return midnightTimestamp - currentTimestamp;
  332. }
  333. }