DateUserUtil.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.fdkankan.ucenter.util;
  2. import cn.hutool.core.date.DateUnit;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.fdkankan.ucenter.entity.IncrementType;
  5. import org.joda.time.DateTime;
  6. import java.text.SimpleDateFormat;
  7. import java.time.LocalDateTime;
  8. import java.time.ZoneId;
  9. import java.util.Calendar;
  10. import java.util.Date;
  11. import java.util.GregorianCalendar;
  12. public class DateUserUtil {
  13. public static final String DEF_FMT = "yyyy-MM-dd HH:mm:ss";
  14. public static final String DAY_FMT = "yyyy-MM-dd";
  15. public static final String M_FMT = "yyyy-MM-dd HH:mm";
  16. public static String getLastTime(String time1,String time2){
  17. return getDate(time1).getTime() >getDate(time2).getTime() ? time1 : time2;
  18. }
  19. public static Date getDate(String time){
  20. try {
  21. return new SimpleDateFormat(DEF_FMT).parse(time);
  22. }catch (Exception e){
  23. e.printStackTrace();
  24. }
  25. return new Date();
  26. }
  27. public static String getDate(Date time){
  28. return new SimpleDateFormat(DEF_FMT).format(time);
  29. }
  30. public static String getZeroDate(){
  31. return new SimpleDateFormat(DAY_FMT).format(new Date()) +" 00:00:00";
  32. }
  33. public static String getLastDate(){
  34. return new SimpleDateFormat(DAY_FMT).format(new Date()) +" 23:59:59";
  35. }
  36. public static String getDayTime(String time) {
  37. try {
  38. Date date = new SimpleDateFormat(DEF_FMT).parse(time);
  39. return new SimpleDateFormat(DAY_FMT).format(date);
  40. }catch (Exception e){
  41. e.printStackTrace();
  42. }
  43. return time;
  44. }
  45. public static DateTime getDateTime(Date userTime,IncrementType incrementType,Integer count) {
  46. DateTime dateTime = new DateTime();
  47. if(userTime.getTime() >new Date().getTime()){
  48. dateTime = new DateTime(userTime);
  49. }
  50. if(count == null){
  51. count = 1;
  52. }
  53. switch (incrementType.getValidTimeType()){
  54. case 0 : return dateTime.plusYears(incrementType.getValidTime() * count);
  55. case 1 : return dateTime.plusMonths(incrementType.getValidTime() * count);
  56. case 2 : return dateTime.plusDays(incrementType.getValidTime() * count);
  57. default: return dateTime;
  58. }
  59. }
  60. public static Long getLastNowTime() {
  61. Date date1 = new Date();
  62. Date date2 = DateUserUtil.getDate(getLastDate());
  63. return ( date2.getTime() - date1.getTime() ) /1000;
  64. }
  65. public static String getMDate(String time) {
  66. try {
  67. Date date = new SimpleDateFormat(DEF_FMT).parse(time);
  68. return new SimpleDateFormat(M_FMT).format(date);
  69. }catch (Exception e){
  70. e.printStackTrace();
  71. }
  72. return time;
  73. }
  74. public static Long between(Date endDate, Date newDate) {
  75. long between = DateUtil.between(endDate, newDate, DateUnit.DAY);
  76. if(between == 0 && endDate.getTime() > newDate.getTime()){
  77. return 1L;
  78. }
  79. if(endDate.getTime() < newDate.getTime()){
  80. return 0L;
  81. }
  82. return between;
  83. }
  84. public static Date getMonthDate(Date endDate, Integer month) {
  85. LocalDateTime localDateTime = endDate.toInstant()
  86. .atZone(ZoneId.systemDefault())
  87. .toLocalDateTime().plusMonths(month);
  88. return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
  89. }
  90. /*日期加+1年*/
  91. public static Date dateAddOneYear(Date date,Integer i) {
  92. Calendar calendar = new GregorianCalendar();
  93. calendar.setTime(date);
  94. calendar.add(Calendar.YEAR,i); //把日期往后增加一天,整数 往后推,负数往前移动
  95. date=calendar.getTime(); //这个时间就是日期往后推一天的结果
  96. return date;
  97. }
  98. }