日期转换格式工具、String、Date格式互转

左手的ㄟ右手 2023-10-04 08:33 108阅读 0赞

日期转换格式工具

  1. package com.xxxxx.smartcity.gov.fgw.meeting.utils;
  2. import org.apache.commons.lang3.ObjectUtils;
  3. import org.apache.commons.lang3.StringUtils;
  4. import org.apache.commons.lang3.time.DateFormatUtils;
  5. import java.text.ParseException;
  6. import java.text.SimpleDateFormat;
  7. import java.time.LocalDate;
  8. import java.time.LocalDateTime;
  9. import java.time.Month;
  10. import java.time.ZoneId;
  11. import java.time.format.DateTimeFormatter;
  12. import java.util.Calendar;
  13. import java.util.Date;
  14. import java.util.Objects;
  15. /**
  16. * @author WANGHONGWEI319
  17. * @date 2019/8/16 18:13
  18. */
  19. public final class DateUtil {
  20. private final static String YEAR_MONTH_DAY = "yyyyMMdd";
  21. private final static String QUARTER_FIRST = "Q1";
  22. private final static String QUARTER_SECOND = "Q2";
  23. private final static String QUARTER_THIRD = "Q3";
  24. private final static String QUARTER_FOURTH = "Q4";
  25. private DateUtil() {
  26. }
  27. /**
  28. * 判断传入日期是否为本季度
  29. *
  30. * @param date
  31. * @return
  32. */
  33. public static Boolean inCurrentQuarter(Date date) {
  34. if (null == date) {
  35. return false;
  36. }
  37. Calendar calendar = Calendar.getInstance();
  38. calendar.setTime(date);
  39. int year = calendar.get(Calendar.YEAR);
  40. LocalDate now = LocalDate.now();
  41. if (now.getYear() != year) {
  42. return false;
  43. }
  44. LocalDate localDate = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
  45. int i = localDate.getMonthValue() - now.getMonth().firstMonthOfQuarter().getValue();
  46. return i <= 2 && i >= 0;
  47. }
  48. /**
  49. * 获取指定日期前后一段时间日期
  50. *
  51. * @param date
  52. * @param day
  53. * @param calendarType
  54. * @return
  55. */
  56. public static Date getDateBefore(Date date, int day, int calendarType) {
  57. Calendar calendar = Calendar.getInstance();
  58. calendar.setTime(date);
  59. calendar.add(calendarType, -day);
  60. return calendar.getTime();
  61. }
  62. /**
  63. * 比较日期相等
  64. *
  65. * @param date1
  66. * @param date2
  67. * @return
  68. */
  69. public static int dateCompare(Date date1, Date date2) {
  70. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  71. Date dateFirst = null;
  72. try {
  73. dateFirst = dateFormat.parse(dateFormat.format(date1));
  74. Date dateLast = dateFormat.parse(dateFormat.format(date2));
  75. if (dateFirst.after(dateLast)) {
  76. return 1;
  77. } else if (dateFirst.before(dateLast)) {
  78. return -1;
  79. }
  80. return 0;
  81. } catch (ParseException e) {
  82. return 0;
  83. }
  84. }
  85. /**
  86. * 获取当前时间
  87. *
  88. * @return
  89. */
  90. public static Date getCurrentDate() {
  91. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  92. try {
  93. return dateFormat.parse(dateFormat.format(new Date()));
  94. } catch (ParseException e) {
  95. return null;
  96. }
  97. }
  98. /**
  99. * 获取当前时间串
  100. *
  101. * @return
  102. */
  103. public static String getCurrentDateStr(String pattern) {
  104. SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
  105. return dateFormat.format(new Date());
  106. }
  107. /**
  108. * 获取指定时间串
  109. *
  110. * @return
  111. */
  112. public static String getDateStr(Date date,String pattern) {
  113. if (ObjectUtils.isEmpty(date)){
  114. return "";
  115. }
  116. SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
  117. return dateFormat.format(date);
  118. }
  119. /**
  120. * @param date
  121. * @return
  122. */
  123. public static Date dateFormat(Date date) {
  124. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  125. Date dateFirst = null;
  126. try {
  127. dateFirst = dateFormat.parse(dateFormat.format(date));
  128. } catch (ParseException e) {
  129. e.printStackTrace();
  130. }
  131. return dateFirst;
  132. }
  133. /**
  134. * @param dateStr
  135. * @return
  136. */
  137. public static Date dateParse(String dateStr) {
  138. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  139. Date dateFirst = null;
  140. try {
  141. dateFirst = dateFormat.parse(dateStr);
  142. } catch (ParseException e) {
  143. e.printStackTrace();
  144. }
  145. return dateFirst;
  146. }
  147. /**
  148. * date转String
  149. * @param dateStr
  150. * @return
  151. */
  152. public static String dateParse2(Date dateStr) {
  153. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  154. String dateFirst = null;
  155. try {
  156. dateFirst = dateFormat.format(dateStr);
  157. } catch (Exception e) {
  158. e.printStackTrace();
  159. }
  160. return dateFirst;
  161. }
  162. /**
  163. * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
  164. */
  165. public static String formatDate(Date date, Object... pattern) {
  166. String formatDate = null;
  167. if (pattern != null && pattern.length > 0) {
  168. formatDate = DateFormatUtils.format(date, pattern[0].toString());
  169. } else {
  170. formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
  171. }
  172. return formatDate;
  173. }
  174. /**
  175. * 将日期格式字符串转化为指定格式的日期
  176. *
  177. * @param timeStr - 日期字符串
  178. * @param format - 具体格式,传空则默认为"yyyy-MM-dd"
  179. * @return
  180. */
  181. public static Date str2Date(String timeStr, String format) {
  182. if (null == format || format.isEmpty()) {
  183. format = "yyyy-MM-dd";
  184. }
  185. SimpleDateFormat sdf = new SimpleDateFormat(format);
  186. Date date = null;
  187. try {
  188. date = sdf.parse(timeStr);
  189. } catch (ParseException e) {
  190. e.printStackTrace();
  191. }
  192. return date;
  193. }
  194. /**
  195. * 获取当月第一天
  196. *
  197. * @param year
  198. * @param month
  199. * @return
  200. */
  201. public static String getFirstDayOfMonth(int year, int month) {
  202. Calendar cal = Calendar.getInstance();
  203. cal.set(Calendar.YEAR, year);
  204. cal.set(Calendar.MONTH, month - 1);
  205. cal.set(Calendar.DAY_OF_MONTH, 1);
  206. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  207. }
  208. /**
  209. * 获取当月最后一天
  210. *
  211. * @param year
  212. * @param month
  213. * @return
  214. */
  215. public static String getLastDayOfMonth(int year, int month) {
  216. Calendar cal = Calendar.getInstance();
  217. cal.set(Calendar.YEAR, year);
  218. cal.set(Calendar.MONTH, month - 1);
  219. cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
  220. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  221. }
  222. /**
  223. * 设置时间
  224. *
  225. * @param year
  226. * @param month
  227. * @param day
  228. * @return
  229. */
  230. public static String setDate(int year, int month, int day) {
  231. Calendar cal = Calendar.getInstance();
  232. cal.set(Calendar.YEAR, year);
  233. cal.set(Calendar.MONTH, month - 1);
  234. cal.set(Calendar.DAY_OF_MONTH, day);
  235. return new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
  236. }
  237. /**
  238. * 取上一年
  239. *
  240. * @param year 年份,格式yyyy
  241. * @return 年份,格式yyyy
  242. */
  243. public static String getPreYear(String year) {
  244. if (StringUtils.isBlank(year) || year.length() != 4 || !StringUtils.isNumericSpace(year)) {
  245. return null;
  246. }
  247. return StringUtils.leftPad(String.valueOf(Integer.parseInt(year) - 1), 4, "0");
  248. }
  249. /**
  250. * 取上几个月
  251. *
  252. * @param date, 格式yyyyMMdd
  253. * @param preFew,上几个月
  254. * @return 月份,格式yyyyMMdd
  255. */
  256. public static String getPreFewMonth(String date, int preFew) {
  257. if (StringUtils.isBlank(date) || date.length() != 8) {
  258. return null;
  259. }
  260. LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(YEAR_MONTH_DAY));
  261. return DateTimeFormatter.ofPattern(YEAR_MONTH_DAY).format(localDate.minusMonths(preFew));
  262. }
  263. public static String convertDate2Quarter(LocalDate localDate) {
  264. return convertDate2Quarter(localDate, null);
  265. }
  266. public static String convertDate2Quarter(LocalDate localDate, String splitStr) {
  267. String quarter = null;
  268. switch (localDate.getMonth()) {
  269. case MARCH:
  270. quarter = QUARTER_FIRST;
  271. break;
  272. case JUNE:
  273. quarter = QUARTER_SECOND;
  274. break;
  275. case SEPTEMBER:
  276. quarter = QUARTER_THIRD;
  277. break;
  278. case DECEMBER:
  279. quarter = QUARTER_FOURTH;
  280. break;
  281. }
  282. if (Objects.isNull(splitStr)) {
  283. splitStr = "";
  284. }
  285. return Objects.nonNull(quarter) ?
  286. StringUtils.leftPad(localDate.getYear() + splitStr + quarter, 6 + splitStr.length(), "0") : null;
  287. }
  288. /**
  289. * 转换季度
  290. *
  291. * @param date
  292. * @return
  293. */
  294. public static String convertDate2Quarter(String date) {
  295. return convertDate2Quarter(date, null);
  296. }
  297. /**
  298. * 转换季度
  299. *
  300. * @param date
  301. * @return
  302. */
  303. public static String convertDate2Quarter(String date, String splitStr) {
  304. if (StringUtils.isBlank(date) || date.length() != 8) {
  305. return null;
  306. }
  307. LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(YEAR_MONTH_DAY));
  308. return convertDate2Quarter(localDate, splitStr);
  309. }
  310. public static String convertQuarter2Date(String quarter, String splitStr) {
  311. if (StringUtils.isBlank(quarter) || quarter.length() < 6) {
  312. return null;
  313. }
  314. String yearValue = quarter.substring(0, 4);
  315. int splitLen = Objects.nonNull(splitStr) ? splitStr.length() : 0;
  316. String quarterValue = quarter.substring(4 + splitLen);
  317. String monthValue = null;
  318. switch (quarterValue) {
  319. case QUARTER_FIRST:
  320. monthValue = "0" + Month.MARCH.getValue();
  321. break;
  322. case QUARTER_SECOND:
  323. monthValue = "0" + Month.JUNE.getValue();
  324. break;
  325. case QUARTER_THIRD:
  326. monthValue = "0" + Month.SEPTEMBER.getValue();
  327. break;
  328. case QUARTER_FOURTH:
  329. monthValue = "" + Month.DECEMBER.getValue();
  330. break;
  331. }
  332. return Objects.nonNull(monthValue) ? StringUtils.leftPad(yearValue + monthValue + "01", 8, "0") : null;
  333. }
  334. public static LocalDate convertQuarterDate(LocalDate localDate) {
  335. int monthValue = localDate.getMonthValue();
  336. if (monthValue <= 3) {
  337. return localDate.withMonth(Month.MARCH.getValue()).withDayOfMonth(1);
  338. } else if (monthValue <= 6) {
  339. return localDate.withMonth(Month.JUNE.getValue()).withDayOfMonth(1);
  340. } else if (monthValue <= 9) {
  341. return localDate.withMonth(Month.SEPTEMBER.getValue()).withDayOfMonth(1);
  342. } else {
  343. return localDate.withMonth(Month.DECEMBER.getValue()).withDayOfMonth(1);
  344. }
  345. }
  346. /**
  347. * 转换月份
  348. *
  349. * @param date
  350. * @return
  351. */
  352. public static String convertDate2Month(String date) {
  353. if (StringUtils.isBlank(date) || date.length() != 8) {
  354. return null;
  355. }
  356. LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern(YEAR_MONTH_DAY));
  357. return StringUtils.leftPad(String.valueOf(localDate.getMonthValue()), 2, "0");
  358. }
  359. }

发表评论

表情:
评论列表 (有 0 条评论,108人围观)

还没有评论,来说两句吧...

相关阅读

    相关 日期格式转换

    日期格式转换   在java或js中 我们常常会进行日期格式的转换  然而每次都去写很麻烦 所以在这里 我整理了一个dateformat的工具类 希望对大家有用!![