Java时间工具类

旧城等待, 2022-01-29 11:43 384阅读 0赞
该时间工具类功能:时间戳格式化至毫秒、时间戳格式化至秒、时间戳格式化至日、时间戳格式化成时和分、Date对象格式化至毫秒、Date对象格式化至秒、Date对象格式化至日、Date对象格式化成时和分、Date对象格式化成年份、时间格式为时和分转换为时间戳、时间格式为年月日转换为时间戳、时间格式为年月日时分秒转换为时间戳、获取当前时间的前一天日期。
  1. package com.blog.chen_2890.utils;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Calendar;
  5. import java.util.Date;
  6. /** * @author https://blog.csdn.net/chen_2890 * @description TimeUtil * @date 2019/5/9 14:57 */
  7. public class TimeUtil {
  8. public static String formatToMillis(Long millis){
  9. Long memberTime = millis;
  10. Date dt = new Date(memberTime);
  11. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
  12. return dateFormat.format(dt);
  13. }
  14. public static String formatToSecond(Long millis){
  15. Long memberTime = millis;
  16. Date dt = new Date(memberTime);
  17. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  18. return dateFormat.format(dt);
  19. }
  20. public static String formatToDate(Long millis){
  21. Long memberTime = millis;
  22. Date dt = new Date(memberTime);
  23. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  24. return dateFormat.format(dt);
  25. }
  26. public static String formatToHourMinute(Long millis){
  27. Long memberTime = millis;
  28. Date dt = new Date(memberTime);
  29. SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
  30. return dateFormat.format(dt);
  31. }
  32. public static String formatToMillis(Date date){
  33. Date dt = date;
  34. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
  35. return dateFormat.format(dt);
  36. }
  37. public static String formatToSecond(Date date){
  38. Date dt = date;
  39. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  40. return dateFormat.format(dt);
  41. }
  42. public static String formatToDate(Date date){
  43. Date dt = date;
  44. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  45. return dateFormat.format(dt);
  46. }
  47. public static String formatToHourMinute(Date date){
  48. Date dt = date;
  49. SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
  50. return dateFormat.format(dt);
  51. }
  52. public static String formatToYear(Date date){
  53. Date dt = date;
  54. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
  55. return dateFormat.format(dt);
  56. }
  57. public static Long fromHourMinuteToMillis(String format){
  58. SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
  59. try {
  60. Date date = dateFormat.parse(format);
  61. return date.getTime();
  62. } catch (ParseException e) {
  63. e.printStackTrace();
  64. }
  65. return null;
  66. }
  67. public static Long fromDateToMillis(String format){
  68. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  69. try {
  70. Date date = dateFormat.parse(format);
  71. return date.getTime();
  72. } catch (ParseException e) {
  73. e.printStackTrace();
  74. }
  75. return null;
  76. }
  77. public static Long fromSecondToMillis(String format){
  78. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  79. try {
  80. Date date = dateFormat.parse(format);
  81. return date.getTime();
  82. } catch (ParseException e) {
  83. e.printStackTrace();
  84. }
  85. return null;
  86. }
  87. public static String beforeOneDayDate(){
  88. //此时打印它获取的是系统当前时间
  89. Calendar calendar = Calendar.getInstance();
  90. //得到前一天
  91. calendar.add(Calendar.DATE, -1);
  92. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  93. try {
  94. return dateFormat.format(calendar.getTime());
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. }
  98. return null;
  99. }
  100. }
注:其实就是简单粗暴的分装了一下重复代码,可以有效的提高日常开发效率。

原来微信打赏还可以备注哦

在这里插入图片描述

发表评论

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

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

相关阅读

    相关 Java 时间工具

    借鉴网上资源与工作中经验整理的时间工具类(主要针对java.util.Date的使用,java8后的时间工具下期整理),欢迎大家一起完善补充。 import

    相关 Java时间工具

    该时间工具类功能:时间戳格式化至毫秒、时间戳格式化至秒、时间戳格式化至日、时间戳格式化成时和分、Date对象格式化至毫秒、Date对象格式化至秒、Date对象格式化至日、Da

    相关 java时间工具

    在项目中,很多地方需要根据时间获取相应的数据,将时间格式化,或者时间比较等相关操作。一个良好的工具类不仅可以减少代码冗余,还能促进业务处理,加快进度。 /