工具类篇【三】日期Date转换

缺乏、安全感 2022-12-09 14:59 248阅读 0赞

工具类篇大全

工具类篇【一】String字符串

工具类篇【二】BigDecimal计算

工具类篇【三】日期Date转换

工具类篇【四】日志脱敏

工具类篇【五】Random随机生成字符串

工具类篇【六】克隆对象的2种常用方法


前言

日期Date是编程中最常使用的util类之一,毫无疑问日期就是代表时间,数据的创建时间、更新时间、交易时间、记录时间等;是一个尤为重要的字段和属性。通常在前端展示、数据库存储、数据传输和程序中转换,格式也有多种:Date、String、Long;操作也分为时间格式转换、大小(先后)比较。

一、格式转换大全

  1. public static final String TIME_TEMPLATE = "yyyy-MM-dd HH:mm:ss";
  2. public static final String TIME_TEMPLATE2 = "yyyyMMddHHmmss";
  3. public static final String PERIOD_TEMPLATE = "yyyy-MM";
  4. public static final String DATE_TEMPLATE_Y_M_D = "yyyy-MM-dd";
  5. public static final String DATE_TEMPLATE_YMD = "yyyyMMdd";
  6. /**
  7. * 代替SimpleDateFormat DATETIME_PATTERN 线程不安全方案
  8. */
  9. protected static final FastDateFormat TIME_FORMAT = FastDateFormat.getInstance(TIME_TEMPLATE);
  10. protected static final FastDateFormat TIME_FORMAT2 = FastDateFormat.getInstance(TIME_TEMPLATE2);
  11. /**
  12. * 代替SimpleDateFormat DATE_PATTERN 线程不安全方案
  13. */
  14. protected static final FastDateFormat DATE_FORMAT = FastDateFormat.getInstance(DATE_TEMPLATE_Y_M_D);
  15. /**
  16. * 代替SimpleDateFormat DATE_PATTERN 线程不安全方案
  17. */
  18. protected static final FastDateFormat DATE_FORMAT2 = FastDateFormat.getInstance(DATE_TEMPLATE_YMD);
  19. /**
  20. * 代替SimpleDateFormat PERIOD_PATTERN 线程不安全方案
  21. */
  22. protected static final FastDateFormat PERIOD_FORMAT = FastDateFormat.getInstance(PERIOD_TEMPLATE);
  • Date转成String格式的时间

    /**

    1. * TITIL 日期转换成字符串 yyyy/MM/dd HH:mm:ss
    2. * @DateTime 2017623 下午2:50:27
    3. *
    4. * @param date
    5. * @return
    6. */
    7. public static String convertDateToStringFormatTime(Date date) {
    8. if (date == null) {
    9. return null;
    10. }
    11. return TIME_FORMAT.format(date);
    12. }
    13. /**
    14. * TITIL 日期转换成字符串 yyyymmddhhmiss
    15. * @DateTime 2017年6月23日 下午2:50:27
    16. *
    17. * @param date
    18. * @return
    19. */
    20. public static String convertDateToStringFormatTime2(Date date) {
    21. if (date == null) {
    22. return null;
    23. }
    24. return TIME_FORMAT2.format(date);
    25. }
    26. /**
    27. * TITIL 日期转换成字符串 yyyy-MM-dd
    28. * @DateTime 2017年6月23日 下午2:50:27
    29. *
    30. * @param date
    31. * @return
    32. */
    33. public static String convertDateToString(Date date) {
    34. if (date == null) {
    35. return null;
    36. }
    37. return DATE_FORMAT.format(date);
    38. }
    39. /**
    40. * TITIL 日期转换成字符串 yyyyMMdd
    41. * @DateTime 2017年6月23日 下午2:50:27
    42. *
    43. * @param date
    44. * @return
    45. */
    46. public static String convertDateToString2(Date date) {
    47. if (date == null) {
    48. return null;
    49. }
    50. return DATE_FORMAT2.format(date);
    51. }
    52. /**
    53. * @author
    54. * @DateTime 2018年7月16日 下午8:47:34
    55. *
    56. * @param date
    57. * @return
    58. */
    59. public static String convertDateToPeriod(Date date) {
    60. if (date == null) {
    61. return null;
    62. }
    63. return PERIOD_FORMAT.format(date);
    64. }
  • String转成Date格式的时间

    /**

    1. * titil : 字符串(YYYY/MM/DD)转日期
    2. * @DateTime 2018118 下午3:23:00
    3. *
    4. * @param strDate
    5. * @return
    6. * @throws ParseException
    7. */
    8. public static Date convertStringToDate(String strDate) {
    9. try {
    10. return TIME_FORMAT.parse(strDate);
    11. } catch (ParseException pe) {
    12. throw new JobRuntimeException(pe.getMessage(), pe);
    13. }
    14. }
    15. public static Date convertStringToDate2(String strDate) {
    16. try {
    17. return TIME_FORMAT2.parse(strDate);
    18. } catch (ParseException pe) {
    19. throw new JobRuntimeException(pe.getMessage(), pe);
    20. }
    21. }
    22. /**
    23. * titil : 字符串(YYYY/MM/DD)转日期
    24. * @author
    25. * @DateTime 2018年1月18日 下午3:23:00
    26. *
    27. * @param strDate
    28. * @return
    29. * @throws ParseException
    30. */
    31. public static Date convertStringDateToDate(String strDate) {
    32. try {
    33. return DATE_FORMAT2.parse(strDate);
    34. } catch (ParseException pe) {
    35. throw new JobRuntimeException(pe.getMessage(), pe);
    36. }
    37. }
    38. public static Date convertStringToDateFormatYmd(String strDate) {
    39. try {
    40. return DATE_FORMAT.parse(strDate);
    41. } catch (ParseException pe) {
    42. throw new JobRuntimeException(pe.getMessage(), pe);
    43. }
    44. }
  • Long转成Date格式的时间

    public static Date convertSecondLongToDate(Long secondLong) {

    1. Date date = null;
    2. if (secondLong == null || secondLong.longValue() == 0) {
    3. return date;
    4. }
    5. date = new Date(secondLong * 1000);
    6. return date;
    7. }
  • Date转成Long格式时间

    public static Long convertSecondLongToDate(Date date) {

    1. Long long = null;
    2. if (date == null) {
    3. return null;
    4. }
    5. long = date.getTime();
    6. return long;
    7. }

二、SimpleDateFormat 包

  1. /**
  2. * 日期型转换为YYYY-MM-DD类型
  3. * @DateTime 2017年8月11日 下午2:07:41
  4. *
  5. * @param glDate
  6. * @return
  7. * @throws ParseException
  8. * formatDateToDay
  9. */
  10. public static Date convertByDate(Date glDate) throws ParseException {
  11. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  12. String s = simpleDateFormat.format(glDate);
  13. Date date = simpleDateFormat.parse(s);
  14. return date;
  15. }
  16. /**
  17. * 日期型转换为YYYY-MM-DD类型
  18. * @DateTime 2017年8月11日 下午2:07:41
  19. *
  20. * @param glDate
  21. * @return
  22. * @throws ParseException
  23. * formatDateToDay
  24. */
  25. public static Date convertByDateTime(Date glDate) throws ParseException {
  26. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  27. String s = simpleDateFormat.format(glDate);
  28. Date date = simpleDateFormat.parse(s);
  29. return date;
  30. }
  31. /**
  32. * 查询期间的最后一天
  33. * @DateTime 2017年8月16日 下午2:15:53
  34. *
  35. * @param periodName
  36. * @return
  37. * @throws ParseException
  38. * getLastDayOfPeriod
  39. */
  40. public static Date getPeriodLastDay(String periodName) throws ParseException {
  41. Calendar calendar = Calendar.getInstance();
  42. DateFormat format1 = new SimpleDateFormat("yyyy-MM");
  43. calendar.setTime(format1.parse(periodName));
  44. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  45. calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
  46. calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
  47. calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
  48. calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
  49. return calendar.getTime();
  50. }

三、Calendar 包

  1. /**
  2. * 查询期间的最后一天
  3. * @DateTime 2017年8月16日 下午2:15:53
  4. *
  5. * @param periodName
  6. * @return
  7. * @throws ParseException
  8. * getLastDayOfPeriod
  9. */
  10. public static Date getPeriodLastDay(String periodName) throws ParseException {
  11. Calendar calendar = Calendar.getInstance();
  12. DateFormat format1 = new SimpleDateFormat("yyyy-MM");
  13. calendar.setTime(format1.parse(periodName));
  14. calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
  15. calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
  16. calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
  17. calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
  18. calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
  19. return calendar.getTime();
  20. }
  21. /**
  22. * 查询期间的第一天
  23. * @DateTime 2017年8月16日 下午2:15:53
  24. *
  25. * @param periodName
  26. * @return
  27. * @throws ParseException
  28. */
  29. public static Date getPeriodFirstDay(String periodName) throws ParseException {
  30. Calendar calendar = Calendar.getInstance();
  31. DateFormat format1 = new SimpleDateFormat("yyyy-MM");
  32. calendar.setTime(format1.parse(periodName));
  33. return calendar.getTime();
  34. }
  35. /**获取具体时间
  36. * @DateTime 2017年8月11日 下午4:47:16
  37. *
  38. * @param glDate
  39. * @param dayNum
  40. * @return
  41. */
  42. public static Date addDayByDate(Date glDate, int dayNum) {
  43. Calendar cl = Calendar.getInstance();
  44. cl.setTime(glDate);
  45. cl.add(Calendar.DATE, dayNum);
  46. Date date = cl.getTime();
  47. return date;
  48. }
  49. /**获取具体时间
  50. * @DateTime 2017年9月19日 下午5:12:57
  51. *
  52. * @param date
  53. * @return
  54. */
  55. public static Date getUpperLastDay(Date date) {
  56. Calendar calendar = Calendar.getInstance();
  57. calendar.setTime(date);
  58. calendar.set(Calendar.DAY_OF_MONTH, 1);
  59. calendar.add(Calendar.DAY_OF_MONTH, -1);
  60. return calendar.getTime();
  61. }

四、时间大小比较

  1. /**
  2. * @author
  3. * @DateTime 2018年2月8日 下午8:57:16
  4. *
  5. * @param d1
  6. * @param d2
  7. * @return
  8. */
  9. public static int compareDate(Date d1, Date d2) {
  10. try {
  11. if (d1 == null && d2 == null) {
  12. return 0;
  13. } else if (d1.getTime() > d2.getTime()) {
  14. return 1;
  15. } else if (d1.getTime() < d2.getTime()) {
  16. return -1;
  17. } else {
  18. return 0;
  19. }
  20. } catch (Exception exception) {
  21. Log.error(exception.getMessage());
  22. throw new RuntimeException(exception.getMessage(), exception);
  23. }
  24. }

既然都看完了整篇文章,相信对你一定有所帮助。原创不易,勿做伸手党。

点击下方【打赏】小编,或者关注公众号给予支持,你们的每一份鼓励都将是小编伟大的动力。


watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0ZvbGxvd18yNA_size_16_color_FFFFFF_t_70 同名原创公众号: 程序大视界

发表评论

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

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

相关阅读