时间处理工具类

搜集整理比较常用的时间工具类,记录一下。

  1 /**
  2  * LocalDateTime 转 时间
  3  */
  4 public static String localDateTimeToDateTime(LocalDateTime localDateTime) {
  5     return DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDateTime);
  6 }
  7 
  8 /**
  9  * LocalDateTime 转 日期
 10  */
 11 public static String localDateTimeToDate(LocalDateTime localDateTime) {
 12     return DateTimeFormatter.ofPattern("yyyy-MM-dd").format(localDateTime);
 13 }
 14 
 15 /**
 16  * 转时间戳
 17  */
 18 public static String parseTimestamp(Date date) {
 19     SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
 20     return format.format(date);
 21 }
 22 
 23 /**
 24  * 转时间格式
 25  */
 26 public static String parseYYYYMMDDHHMMSS(String date) {
 27     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 28     return simpleDateFormat.format(date);
 29 }
 30 
 31 /**
 32  * Date转ISO8601
 33  */
 34 public static String datetimeParseISO8601(Date date) {
 35     SimpleDateFormat iso8601DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
 36     return iso8601DateFormat.format(date);
 37 }
 38 
 39 /**
 40  * iso8601转时间格式
 41  */
 42 public static String iso8601ToDateTime(String date) {
 43     SimpleDateFormat iso8601DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
 44     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 45     String d = "";
 46     try {
 47         d = simpleDateFormat.format(iso8601DateFormat.parse(date));
 48     } catch (ParseException e) {
 49         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 50         e.printStackTrace(new PrintStream(bos));
 51         log.warn("--->时间转换异常:" + bos);
 52     }
 53     return d;
 54 }
 55 
 56 /**
 57  * iso8601(UTC)转时间格式
 58  *
 59  * @param date
 60  * @return
 61  */
 62 public static String iso8601UTCToDateTime(String date) {
 63     SimpleDateFormat iso8601DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
 64     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 65     String d = "";
 66     try {
 67         d = simpleDateFormat.format(iso8601DateFormat.parse(date));
 68     } catch (ParseException e) {
 69         ByteArrayOutputStream bos = new ByteArrayOutputStream();
 70         e.printStackTrace(new PrintStream(bos));
 71         log.warn("--->时间转换异常:" + bos);
 72     }
 73     return d;
 74 }
 75 
 76 /**
 77  * 时间转换
 78  */
 79 public static String parseYMD(Date date) {
 80     SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
 81     return formatYMD.format(date);
 82 }
 83 
 84 /**
 85  * 将Date转String
 86  */
 87 public static String dateFormatZh(Date date) {
 88     SimpleDateFormat simpleDateFormatZh = new SimpleDateFormat("yyyy年M月d日 HH:mm:ss");
 89     return simpleDateFormatZh.format(date);
 90 }
 91 
 92 /**
 93  * 将String转Date
 94  */
 95 public static Date toDate(String date) {
 96     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 97     Date d = null;
 98     try {
 99         d = simpleDateFormat.parse(date);
100     } catch (ParseException e) {
101         ByteArrayOutputStream bos = new ByteArrayOutputStream();
102         e.printStackTrace(new PrintStream(bos));
103         log.warn("--->时间转换异常:" + " " + bos);
104     }
105     return d;
106 }
107 
108 /**
109  * 将Date转String
110  */
111 public static String dateFormatMD(Date date) {
112     SimpleDateFormat simpleDateFormatZhMin = new SimpleDateFormat("M月d日");
113     return simpleDateFormatZhMin.format(date);
114 }
115 
116 /**
117  * 根据时间获取当月天数
118  */
119 public static int getDayByMonth(String time) {
120     int day;
121     int year = Integer.parseInt(time.split("-")[0]);
122     int month = Integer.parseInt(time.split("-")[1]);
123     if (2 == month) {
124         day = year % 4 == 0 ? 29 : 28;
125     } else {
126         if (month == 8 || month == 12) {
127             day = 31;
128         } else if (month == 9 || month == 11) {
129             day = 30;
130         } else {
131             day = month % 2 == 0 ? 30 : 31;
132         }
133     }
134     return day;
135 }
136 
137 /**
138  * 获取时间所在星期第几天
139  */
140 public static int getDayOfWeek(String time) {
141     LocalDate localDate = LocalDate.fromDateFields(CalendarUtils.toDate(time));
142     return localDate.dayOfWeek().get();
143 }
144 
145 /**
146  * 获取时间所在月份第几天
147  */
148 public static int getDayOfMonth(String time) {
149     LocalDate localDate = LocalDate.fromDateFields(CalendarUtils.toDate(time));
150     return localDate.dayOfMonth().get();
151 }
152 
153 /**
154  * 获取本周第一天
155  */
156 public static String getFirstOfWeek() {
157     SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
158     Calendar calendar = Calendar.getInstance();
159     calendar.add(Calendar.WEEK_OF_MONTH, 0);
160     calendar.set(Calendar.DAY_OF_WEEK, 2);
161     Date time = calendar.getTime();
162     return formatYMD.format(time);
163 }
164 
165 /**
166  * 获取本周最后一天
167  */
168 public static String getLastOfWeek() {
169     SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
170     Calendar calendar = Calendar.getInstance();
171     calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
172     calendar.add(Calendar.DAY_OF_WEEK, 1);
173     Date time = calendar.getTime();
174     return formatYMD.format(time);
175 }
176 
177 
178 /**
179  * 获取时间所在星期的第一天
180  *
181  * @param time yyyy-MM-dd
182  * @return
183  */
184 public static String getFirstOfWeek(String time) throws ParseException {
185     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
186     SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
187     time = time + " " + "23:59:59";
188     Calendar calendar = Calendar.getInstance();
189     calendar.setTime(simpleDateFormat.parse(time));
190     calendar.add(Calendar.WEEK_OF_MONTH, 0);
191     calendar.set(Calendar.DAY_OF_WEEK, 2);
192     Date date = calendar.getTime();
193     return formatYMD.format(date);
194 }
195 
196 /**
197  * 获取时间所在星期的最后一天
198  *
199  * @param time yyyy-MM-dd
200  * @return
201  */
202 public static String getLastOfWeek(String time) throws ParseException {
203     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
204     SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");
205     time = time + " " + "00:00:00";
206     Calendar calendar = Calendar.getInstance();
207     calendar.setTime(simpleDateFormat.parse(time));
208     calendar.set(Calendar.DAY_OF_WEEK, calendar.getActualMaximum(Calendar.DAY_OF_WEEK));
209     calendar.add(Calendar.DAY_OF_WEEK, 1);
210     Date date = calendar.getTime();
211     return formatYMD.format(date);
212 }
213 
214 /**
215  * 获取时间所在月份的第一天
216  */
217 public static String getFirstDayOfMonth(String time) throws ParseException {
218     String year = time.split("-")[0];
219     String month = time.split("-")[1];
220     return year + "-" + month + "-" + "01";
221 }
222 
223 
224 /**
225  * 获取时间所在月份的最后一天
226  */
227 public static String getLastDayOfMonth(String time) throws ParseException {
228     String year = time.split("-")[0];
229     String month = time.split("-")[1];
230     int days = CalendarUtils.getDayByMonth(time);
231     return year + "-" + month + "-" + days;
232 }
233 
234 /**
235  * 获取时间戳(UTC)
236  *
237  * @return
238  */
239 public long getTimestamp() {
240     return LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
241 }
242 
243 
244 /**
245  * 指定日期x分钟以前的时间
246  *
247  * @param minutes      分钟
248  * @param current_time 日期
249  * @return
250  */
251 public static Date getBefordDateTime(int minutes, String current_time) throws ParseException {
252     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
253     String before_time = simpleDateFormat.format(CalendarUtils.toDate(current_time).getTime() - (long) minutes * 60 * 1000);
254     return simpleDateFormat.parse(before_time);
255 }

 

上一篇:SimpleDateFormat类


下一篇:更正SimpleDateFormat类线程不安全问题分析的错误