1. package com.hq.schoolcj.util;
    2. import java.text.ParseException;
    3. import java.text.ParsePosition;
    4. import java.text.SimpleDateFormat;
    5. import java.time.Instant;
    6. import java.time.LocalDate;
    7. import java.time.LocalDateTime;
    8. import java.time.ZoneId;
    9. import java.util.Calendar;
    10. import java.util.Date;
    11. import java.util.Locale;
    12. public class DateUtils {
    13. /**
    14. * 时间格式(yyyy-MM-dd)
    15. */
    16. public final static String DATE_PATTERN = "yyyy-MM-dd";
    17. public final static String DATE_PATTERN_FOR_SECOND = "yyyy-MM-dd HH:mm:ss";
    18. public final static String MM_DD_HH_MM = "MM月dd日 HH:mm";
    19. public final static String MM_DD = "MM-dd";
    20. public final static String HH_MM = "HH:mm";
    21. public static final String DEFUALT_LONG_TIME_FORMAT = "yyyyMMddHHmmss";
    22. public static String format(Date date) {
    23. return format(date, DATE_PATTERN);
    24. }
    25. /**
    26. * 计算两个日期的相差月份
    27. * @param startDate 开始时间
    28. * @param endDate 结束时间
    29. * @return
    30. */
    31. public static Integer getDifMonth(Date startDate, Date endDate){
    32. if (startDate == null || endDate == null) {
    33. return 0;
    34. }
    35. Calendar start = Calendar.getInstance();
    36. Calendar end = Calendar.getInstance();
    37. start.setTime(startDate);
    38. end.setTime(endDate);
    39. int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
    40. int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
    41. return Math.abs(month + result);
    42. }
    43. /**
    44. * 日期转字符串
    45. *
    46. * @param date
    47. * @param pattern
    48. * @return
    49. */
    50. public static String format(Date date, String pattern) {
    51. if (date != null) {
    52. SimpleDateFormat df = new SimpleDateFormat(pattern);
    53. return df.format(date);
    54. }
    55. return null;
    56. }
    57. /**
    58. * 日期字符串转时间戳
    59. *
    60. * @param dateStr
    61. * @return
    62. */
    63. public static Integer transForMilliSecond(String dateStr) {
    64. Date date = DateUtils.formatDate(dateStr);
    65. return date == null ? null : DateUtils.transForMilliSecond(date);
    66. }
    67. /**
    68. * 日期字符串转日期
    69. *
    70. * @param string
    71. * @param pattern
    72. * @return
    73. */
    74. public static Date formatDate(String string, String pattern) {
    75. if (StringUtils.isNotBlank(string)) {
    76. try {
    77. return new SimpleDateFormat(pattern).parse(string);
    78. } catch (ParseException e) {
    79. e.printStackTrace();
    80. }
    81. }
    82. return null;
    83. }
    84. /**
    85. * 字符串转日期
    86. *
    87. * @param string
    88. * @return
    89. */
    90. public static Date formatDate(String string) {
    91. return formatDate(string, DATE_PATTERN);
    92. }
    93. /**
    94. * 日期转时间戳 (秒)
    95. *
    96. * @param date
    97. * @return
    98. */
    99. public static Integer transForMilliSecond(Date date) {
    100. if (date == null) {
    101. return null;
    102. }
    103. return (int) (date.getTime() / 1000);
    104. }
    105. /**
    106. * 时间戳转日期字符串"yyyy-MM-dd"
    107. *
    108. * @param ms
    109. * @return
    110. */
    111. public static String transForDate(Integer ms) {
    112. String str = "";
    113. if (ms != null) {
    114. long msl = (long) ms * 1000;
    115. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    116. if (ms != null) {
    117. try {
    118. str = sdf.format(msl);
    119. } catch (Exception e) {
    120. e.printStackTrace();
    121. }
    122. }
    123. }
    124. return str;
    125. }
    126. /**
    127. * 时间戳转日期字符串"yyyy-MM-dd"或"yyyy-MM-dd HH:mm:ss"
    128. *精确到毫秒的字符串
    129. * @param seconds
    130. * @return
    131. */
    132. public static String transForDate(String seconds, String pattern) {
    133. if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
    134. return "";
    135. }
    136. if(pattern == null || pattern.isEmpty()){
    137. pattern = "yyyy-MM-dd HH:mm:ss";
    138. }
    139. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    140. return sdf.format(new Date(Long.valueOf(seconds)));
    141. }
    142. /**
    143. * 订单有效期,获取当前时间完后day天后的时间
    144. *
    145. * @param day 推移时间
    146. * @return 推移后的时间
    147. */
    148. public static Date orderDateValidity(Long day) {
    149. return getDateAfter(new Date(), day.intValue());
    150. }
    151. /***
    152. *
    153. * @param d
    154. * :Base Date
    155. * @param day
    156. * :Delayed days
    157. * @return
    158. */
    159. public static java.util.Date getDateAfter(Date d, int day) {
    160. Calendar now = Calendar.getInstance();
    161. now.setTime(d);
    162. now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
    163. return now.getTime();
    164. }
    165. /**
    166. * 给时间加上几个小时
    167. *
    168. * @param date 当前时间 格式:yyyy-MM-dd HH:mm:ss
    169. * @param hour 需要加的时间
    170. * @return
    171. */
    172. public static String addDateHour(Date date, int hour) {
    173. if (date == null) {
    174. return null;
    175. }
    176. Calendar cal = Calendar.getInstance();
    177. cal.setTime(date);
    178. cal.add(Calendar.HOUR_OF_DAY, hour);
    179. date = cal.getTime();
    180. return format(date, DEFUALT_LONG_TIME_FORMAT);
    181. }
    182. /**
    183. * 获取现在时间
    184. *
    185. * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
    186. */
    187. public static Date getNowDate() {
    188. Date currentTime = new Date();
    189. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    190. String dateString = formatter.format(currentTime);
    191. ParsePosition pos = new ParsePosition(8);
    192. Date currentTime_2 = formatter.parse(dateString, pos);
    193. return currentTime_2;
    194. }
    195. /**
    196. * 得到现在时间
    197. *
    198. * @return
    199. */
    200. public static Date getNow() {
    201. Date currentTime = new Date();
    202. return currentTime;
    203. }
    204. /*
    205. * 把时间戳装换为时间字符串
    206. *
    207. * */
    208. public static String getDateString4LongTime(Long timeStamp) {
    209. SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    210. return formatter.format(new Date(timeStamp));
    211. }
    212. public static String dateToString(Date date) {
    213. SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_FOR_SECOND);
    214. return sdf.format(date);
    215. }
    216. public static Date stringToDate(String date, String pattern) {
    217. SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    218. try {
    219. return sdf.parse(date);
    220. } catch (ParseException e) {
    221. e.printStackTrace();
    222. }
    223. return new Date();
    224. }
    225. /***
    226. *
    227. * @param d : 基准时间
    228. * @param day : 几天前
    229. * @return
    230. */
    231. public static Date getDateBefore(Date d, int day) {
    232. Calendar now = Calendar.getInstance();
    233. now.setTime(d);
    234. now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
    235. return now.getTime();
    236. }
    237. public static Date parse(String string, String pattern) {
    238. if (StringUtils.isNotBlank(string)) {
    239. try {
    240. return new SimpleDateFormat(pattern).parse(string);
    241. } catch (ParseException e) {
    242. e.printStackTrace();
    243. }
    244. }
    245. return null;
    246. }
    247. public static Date parse(String string) {
    248. return parse(string, DATE_PATTERN);
    249. }
    250. /**
    251. * 毫秒转化为xx小时xx分钟
    252. *
    253. * @param longTime
    254. * @return
    255. */
    256. public static String longTimeToString(Long longTime) {
    257. if (longTime == null || longTime == 0L) {
    258. return "";
    259. }
    260. int hour = (int) (longTime / (1000 * 60 * 60));
    261. int min = (int) ((longTime % (1000 * 60 * 60)) / (1000 * 60));
    262. if (hour < 1) {
    263. return min + "分钟";
    264. } else if (hour >= 1 && min == 0) {
    265. return hour + "小时";
    266. } else if (hour >= 1 && min > 0) {
    267. return hour + "小时" + min + "分钟";
    268. }
    269. return "";
    270. }
    271. /**
    272. * 日期转化为String
    273. *
    274. * @param date
    275. * @return date string
    276. * @author xuyidong
    277. */
    278. public static String toString(Date date) {
    279. if (null == date) {
    280. return null;
    281. }
    282. try {
    283. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
    284. return sdf.format(date);
    285. } catch (Exception e) {
    286. return null;
    287. }
    288. }
    289. /**
    290. * 指定日期加指定天数
    291. *
    292. * @param date
    293. * @param dayNum
    294. * @return
    295. */
    296. public static String convertDate(Date date, int dayNum) {
    297. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    298. Calendar calendar = Calendar.getInstance();
    299. calendar.setTime(date);
    300. calendar.add(Calendar.DATE, dayNum);
    301. Date date2 = calendar.getTime();
    302. String dateFormat = sdf.format(date2);
    303. return dateFormat;
    304. }
    305. /**
    306. * 当前时间添加时间
    307. * @param type 1代表 日 2代表 月 3代表 年
    308. * @param num 数量
    309. * @return
    310. */
    311. public static Date convertDate(int type,Integer num){
    312. if(num==null){
    313. num=0;
    314. }
    315. SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_FOR_SECOND);
    316. Date nowDate = new Date();
    317. try{
    318. nowDate=sdf.parse(sdf.format(nowDate));
    319. }catch (Exception e){
    320. }
    321. Calendar calendar = Calendar.getInstance();
    322. calendar.setTime(nowDate);
    323. if(type==1){
    324. calendar.add(Calendar.DATE, num);
    325. }else if(type==2){
    326. calendar.add(Calendar.MONTH, num);
    327. }else if(type==3){
    328. calendar.add(Calendar.YEAR, num);
    329. }
    330. Date date2 = calendar.getTime();
    331. return date2;
    332. }
    333. public static void main(String[] args) {
    334. // Date a = formatDate("2019-12-27", "yyyy-MM-dd");
    335. // Date b = formatDate("2020-01-11", "yyyy-MM-dd");
    336. // Integer difMonth = getDifMonth(a, b);
    337. // System.out.println(difMonth);
    338. // int i = new Date() - formatDate("2019-12-07 17:30:);
    339. }
    340. /**
    341. * Date 转化为jdk8 LocalDate
    342. *
    343. * @param date
    344. * @return LocalDate
    345. * @author chen xin yu
    346. * @date 2019-09-02 20:05
    347. */
    348. public static LocalDate dateToLocalDate(Date date) {
    349. Instant instant = date.toInstant();
    350. ZoneId zone = ZoneId.systemDefault();
    351. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    352. return localDateTime.toLocalDate();
    353. }
    354. /**
    355. * jdk8 LocalDate 转化为 Date
    356. *
    357. * @param localDate
    358. * @return Date
    359. * @author chen xin yu
    360. * @date 2019-09-02 20:06
    361. */
    362. public static Date LocalDateToDate(LocalDate localDate) {
    363. ZoneId zone = ZoneId.systemDefault();
    364. Instant instant = localDate.atStartOfDay().atZone(zone).toInstant();
    365. return Date.from(instant);
    366. }
    367. /**
    368. * jdk8 LocalDateTime 转化为 Date
    369. *
    370. * @param localDateTime
    371. * @return Date
    372. * @author chen xin yu
    373. * @date 2019-09-02 20:06
    374. */
    375. public static Date LocalDateTimeToDate(LocalDateTime localDateTime) {
    376. ZoneId zone = ZoneId.systemDefault();
    377. Instant instant = localDateTime.atZone(zone).toInstant();
    378. return Date.from(instant);
    379. }
    380. /**
    381. * Date 转化为jdk8 LocalDateTime
    382. *
    383. * @param date
    384. * @return LocalDate
    385. * @author chen xin yu
    386. * @date 2019-09-02 20:05
    387. */
    388. public static LocalDateTime dateToLocalDateTime(Date date) {
    389. Instant instant = date.toInstant();
    390. ZoneId zone = ZoneId.systemDefault();
    391. LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
    392. return localDateTime;
    393. }
    394. /**
    395. * 时间加上指定天数后的日期
    396. *
    397. * @param date 时间
    398. * @param day 增加的天数
    399. * @return date 返回最终时间零点的毫秒值
    400. */
    401. public static Date datePlusDays(Date date, long day) {
    402. //支付时间
    403. LocalDateTime localDateTime = DateUtils.dateToLocalDateTime(date);
    404. //订单有效期截止时间
    405. LocalDate localDate = localDateTime.plusDays(day).toLocalDate();
    406. return DateUtils.LocalDateToDate(localDate);
    407. }
    408. /**
    409. * 天数转化为xx年xx月
    410. *
    411. * @param day 天数
    412. * @return
    413. */
    414. public static String dayToYearMonth(Long day) {
    415. if (day == null || day == 0L) {
    416. return "";
    417. }
    418. int year = (int) (day / 365);
    419. int month = (int) ((day % 365)/30);
    420. if (year < 1) {
    421. return month + "个月";
    422. } else if (year >= 1 && month == 0) {
    423. return year + "年";
    424. } else if (year >= 1 && month > 0) {
    425. return year + "年" + month + "个月";
    426. }
    427. return "";
    428. }
    429. /**
    430. * 按照指定格式返回当前时间,如果格式为空,则默认为yyyy-MM-dd
    431. *
    432. * @param dateFormat
    433. * @return
    434. */
    435. public static Date strToDate(String date, String dateFormat) {
    436. if (date == null) {
    437. return null;
    438. }
    439. if (org.apache.commons.lang.StringUtils.isBlank(dateFormat)) {
    440. if (date.length() > 10) {
    441. dateFormat = DEFUALT_LONG_TIME_FORMAT;
    442. } else {
    443. dateFormat = DATE_PATTERN;
    444. }
    445. }
    446. try {
    447. SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
    448. return sdf.parse(date);
    449. } catch (Exception e) {
    450. return null;
    451. }
    452. }
    453. }