java时间处理工具类

一时失言乱红尘 2022-05-13 04:58 346阅读 0赞
  1. public class TimeHelper {
  2. private static Logger logger = LoggerFactory.getLogger(TimeHelper.class);
  3. private static final String DATE_STR = "yyyy-MM-dd";
  4. private static final SimpleDateFormat FORMAT = new SimpleDateFormat(DATE_STR);
  5. private static final long DAY_TIME = 24*60*60*1000;
  6. /**
  7. * 得到当前时间字符串
  8. * @return 日期字符串
  9. */
  10. public static String getNowTime(){
  11. return FORMAT.format(new Date());
  12. }
  13. /**
  14. * 默认当前时间 精确到秒
  15. * @return 当前时间
  16. */
  17. public static String getNow(){
  18. return getNowTime("yyyy-MM-dd HH:mm:ss");
  19. }
  20. /**
  21. * 通过指定的格式获取当前时间字符串
  22. * @param format 日期字符串格式
  23. * @return 当前时间字符串
  24. */
  25. public static String getNowTime(String format){
  26. return new SimpleDateFormat(format).format(new Date());
  27. }
  28. /**
  29. * 获取两个时间之间的秒数
  30. * @param date1 开始
  31. * @param date2 结束
  32. * @param format 日期格式
  33. * @return 间隔秒
  34. */
  35. public static long betweenSeconds(String date1,String date2,String ...format){
  36. try{
  37. Date start = getFormat(format).parse(date1);
  38. Date end = getFormat(format).parse(date2);
  39. return (end.getTime()-start.getTime())/1000;
  40. }catch (Exception e){
  41. logger.info(e.getMessage());
  42. }
  43. return 0;
  44. }
  45. /**
  46. * 获取两个日期之间的天数
  47. * @param date1 开始
  48. * @param date2 结束
  49. * @param format 日期格式
  50. * @return 天数
  51. */
  52. public static long betweenDays(String date1,String date2,String...format){
  53. long seconds = betweenSeconds(date1,date2,format);
  54. if(seconds==0){
  55. return 0;
  56. }
  57. return (seconds/60/60/24)+1;
  58. }
  59. private static boolean isNull(String ...objs){
  60. return objs==null || objs.length==0;
  61. }
  62. private static SimpleDateFormat getFormat(String...format){
  63. return isNull(format) ? FORMAT : new SimpleDateFormat(format[0]);
  64. }
  65. /**
  66. * 获取两个日期之间的工作日
  67. * @param date1 开始
  68. * @param date2 结束
  69. * @param format 日期格式
  70. * @return 所有工作日信息
  71. */
  72. public static List<String> betweenWorkDays(String date1, String date2, String...format){
  73. List<String> days = getBetweenDays(date1,date2,format);
  74. List<String> workDays = new ArrayList<>();
  75. for(String day : days){
  76. int week = getWeek(day);
  77. if(week!=0 && week!=6){
  78. workDays.add(day);
  79. }
  80. }
  81. return workDays;
  82. }
  83. /**
  84. * 获取两个日期之间的所有日期信息
  85. * @param date1 开始
  86. * @param date2 结束
  87. * @param format 日期格式
  88. * @return 所有日期信息
  89. */
  90. public static List<String> getBetweenDays(String date1,String date2,String...format){
  91. List<String> dates = new ArrayList<>();
  92. try{
  93. long d1 = getFormat(format).parse(date1).getTime();
  94. long d2 = getFormat(format).parse(date2).getTime();
  95. while(d2>=d1){
  96. String time = getFormat(format).format(d1);
  97. dates.add(time);
  98. d1+=DAY_TIME;
  99. }
  100. return dates;
  101. }catch (Exception e){
  102. e.printStackTrace();
  103. }
  104. return dates;
  105. }
  106. /**
  107. * 获取指定日期为周几
  108. * @param date 指定日期
  109. * @param format 日期格式
  110. * @return 数字
  111. */
  112. public static int getWeek(String date,String...format){
  113. return searchTime(date, Calendar.DAY_OF_WEEK,format)-1;
  114. }
  115. private static int searchTime(String date,Integer search,String...format){
  116. try{
  117. Date d = getFormat(format).parse(date);
  118. Calendar calendar = Calendar.getInstance();
  119. calendar.setTime(d);
  120. return calendar.get(search);
  121. }catch (Exception e){
  122. logger.info(e.getMessage());
  123. }
  124. return -1;
  125. }
  126. /**
  127. * 获取当日为周几
  128. * @return 数字
  129. */
  130. public static int getNowWeek(){
  131. return searchTime(getNowTime(),Calendar.DAY_OF_WEEK)-1;
  132. }
  133. /**
  134. * 获取当日为周几 中文
  135. * @return 中文
  136. */
  137. public static String getNowWeekSimp(){
  138. return new SimpleDateFormat("E").format(new Date());
  139. }
  140. /**
  141. * 获取当日为当前月的第几天
  142. * @return 数字
  143. */
  144. public static int getNowDayOfMonth(){
  145. return searchTime(getNowTime(),Calendar.DAY_OF_MONTH);
  146. }
  147. /**
  148. * 获取当月为第几月
  149. * @return 数字
  150. */
  151. public static int getNowMonth(){
  152. return searchTime(getNowTime(),Calendar.MONTH)+1;
  153. }
  154. /**
  155. * 获取年份
  156. * @return 数字
  157. */
  158. public static int getNowYear(){
  159. return searchTime(getNowTime(),Calendar.YEAR);
  160. }
  161. public static void main(String[] args) {
  162. int a = 0;
  163. int b = 1;
  164. boolean c = a==b;
  165. a = 1;
  166. System.out.println(c);
  167. }
  168. }

简单的日期处理类,可以借鉴一下哦

发表评论

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

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

相关阅读

    相关 Java时间工具

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