1.格式化常量字符串public class TimeFormatter {
//構(gòu)造方法私有化:該類本身地方除外的其他地方無法實(shí)例化該類對象
private TimeFormatter() {
}
public static final String DATETIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMATTER = "yyyy-MM-dd";
public static final String TIME_FORMATTER = "HH:mm:ss";
public static final String DATETIME_T_FORMATTER = "yyyy-MM-dd'T'HH:mm:ss";
} 2.DateTimeUtil日期時(shí)間工具類public class DateTimeUtil {
/**
* 獲取當(dāng)前時(shí)間時(shí)間戳(long)
* @return
*/
public static long currentTimeMillis() {
return System.currentTimeMillis();
}
/**
* 獲取當(dāng)前日期(yyyy-MM-dd)
* @return
*/
public static LocalDate currentLocalDate() {
return LocalDate.now();
}
/**
* 獲取當(dāng)前時(shí)間(HH:mm:ss.SSS)
* @return
*/
public static LocalTime currentLocalTime() {
return LocalTime.now();
}
/**
* 獲取當(dāng)前日期時(shí)間(yyyy-MM-dd'T'HH:mm:ss.SSS)
* @return
*/
public static LocalDateTime currentLocalDateTime() {
return LocalDateTime.now();
}
/**
* 獲取當(dāng)前日期字符串(yyyy-MM-dd)
* @return
*/
public static String getCurrentDateStr() {
return DateTimeFormatter.ofPattern(TimeFormatter.DATE_FORMATTER).format(currentLocalDateTime());
}
/**
* 獲取當(dāng)前時(shí)間字符串(HH:mm:ss)
* @return
*/
public static String getCurrentTimeStr() {
return DateTimeFormatter.ofPattern(TimeFormatter.TIME_FORMATTER).format(currentLocalDateTime());
}
/**
* 獲取當(dāng)前日期時(shí)間字符串(yyyy-MM-dd HH:mm:ss)
* @return
*/
public static String getCurrentDateTimeStr() {
return DateTimeFormatter.ofPattern(TimeFormatter.DATETIME_FORMATTER).format(currentLocalDateTime());
}
/**
* 將時(shí)間字符串轉(zhuǎn)為自定義時(shí)間格式的LocalDateTime
* @param time 需要轉(zhuǎn)化的時(shí)間字符串
* @param format 自定義的時(shí)間格式
* @return
*/
public static LocalDateTime convertStringToLocalDateTime(String time, String format) {
return LocalDateTime.parse(time,DateTimeFormatter.ofPattern(format));
}
/**
* 將LocalDateTime轉(zhuǎn)為自定義的時(shí)間格式的字符串
* @param localDateTime 需要轉(zhuǎn)化的LocalDateTime
* @param format 自定義的時(shí)間格式
* @return
*/
public static String convertLocalDateTimeToString(LocalDateTime localDateTime, String format) {
return localDateTime.format(DateTimeFormatter.ofPattern(format));
}
/**
* 將long類型的timestamp轉(zhuǎn)為LocalDateTime
* @param timestamp
* @return
*/
public static LocalDateTime convertTimestampToLocalDateTime(long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp),ZoneId.systemDefault());
}
/**
* 將LocalDateTime轉(zhuǎn)為long類型的timestamp
* @param localDateTime
* @return
*/
public static long convertLocalDateTimeToTimestamp(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 獲取LocalDateTime的最大時(shí)間的字符串格式(yyyy-MM-dd HH:mm:ss)
* @param localDateTime
* @return
*/
public static String getMaxDateTime(LocalDateTime localDateTime) {
return convertLocalDateTimeToString(localDateTime.with(LocalTime.MAX),TimeFormatter.DATETIME_FORMATTER);
}
/**
* 獲取LocalDateTime的最小時(shí)間的字符串格式(yyyy-MM-dd HH:mm:ss)
* @param localDateTime
* @return
*/
public static String getMinDateTime(LocalDateTime localDateTime) {
return convertLocalDateTimeToString(localDateTime.with(LocalTime.MIN),TimeFormatter.DATETIME_FORMATTER);
}
/**
* 獲取LocalDate的最大時(shí)間的字符串格式(yyyy-MM-dd HH:mm:ss)
* @param localDate
* @return
*/
public static String getMaxDateTime(LocalDate localDate) {
return convertLocalDateTimeToString(localDate.atTime(LocalTime.MAX),TimeFormatter.DATETIME_FORMATTER);
}
/**
* 獲取LocalDate的最小時(shí)間的字符串格式(yyyy-MM-dd HH:mm:ss)
* @param localDate
* @return
*/
public static String getMinDateTime(LocalDate localDate) {
return convertLocalDateTimeToString(localDate.atTime(LocalTime.MIN),TimeFormatter.DATETIME_FORMATTER);
}
} 3.常見方法1. adjustInto 調(diào)整指定的Temporal和當(dāng)前LocalDateTime對
2. atOffset 結(jié)合LocalDateTime和ZoneOffset創(chuàng)建一個(gè)
3. atZone 結(jié)合LocalDateTime和指定時(shí)區(qū)創(chuàng)建一個(gè)ZonedD
4. compareTo 比較兩個(gè)LocalDateTime
5. format 格式化LocalDateTime生成一個(gè)字符串
6. from 轉(zhuǎn)換TemporalAccessor為LocalDateTi
7. get 得到LocalDateTime的指定字段的值
8. getDayOfMonth 得到LocalDateTime是月的第幾天
9. getDayOfWeek 得到LocalDateTime是星期幾
10. getDayOfYear 得到LocalDateTime是年的第幾天
11. getHour 得到LocalDateTime的小時(shí)
12. getLong 得到LocalDateTime指定字段的值
13. getMinute 得到LocalDateTime的分鐘
14. getMonth 得到LocalDateTime的月份
15. getMonthValue 得到LocalDateTime的月份,從1到12
16. getNano 得到LocalDateTime的納秒數(shù)
17. getSecond 得到LocalDateTime的秒數(shù)
18. getYear 得到LocalDateTime的年份
19. isAfter 判斷LocalDateTime是否在指定LocalDateT
20. isBefore 判斷LocalDateTime是否在指定LocalDateT
21. isEqual 判斷兩個(gè)LocalDateTime是否相等
22. isSupported 判斷LocalDateTime是否支持指定時(shí)間字段或單元
23. minus 返回LocalDateTime減去指定數(shù)量的時(shí)間得到的值
24. minusDays 返回LocalDateTime減去指定天數(shù)得到的值
25. minusHours 返回LocalDateTime減去指定小時(shí)數(shù)得到的值
26. minusMinutes 返回LocalDateTime減去指定分鐘數(shù)得到的值
27. minusMonths 返回LocalDateTime減去指定月數(shù)得到的值
28. minusNanos 返回LocalDateTime減去指定納秒數(shù)得到的值
29. minusSeconds 返回LocalDateTime減去指定秒數(shù)得到的值
30. minusWeeks 返回LocalDateTime減去指定星期數(shù)得到的值
31. minusYears 返回LocalDateTime減去指定年數(shù)得到的值
32. now 返回指定時(shí)鐘的當(dāng)前LocalDateTime
33. of 根據(jù)年、月、日、時(shí)、分、秒、納秒等創(chuàng)建LocalDateTi
34. ofEpochSecond 根據(jù)秒數(shù)(從1970-01-0100:00:00開始)創(chuàng)建L
35. ofInstant 根據(jù)Instant和ZoneId創(chuàng)建LocalDateTim
36. parse 解析字符串得到LocalDateTime
37. plus 返回LocalDateTime加上指定數(shù)量的時(shí)間得到的值
38. plusDays 返回LocalDateTime加上指定天數(shù)得到的值
39. plusHours 返回LocalDateTime加上指定小時(shí)數(shù)得到的值
40. plusMinutes 返回LocalDateTime加上指定分鐘數(shù)得到的值
41. plusMonths 返回LocalDateTime加上指定月數(shù)得到的值
42. plusNanos 返回LocalDateTime加上指定納秒數(shù)得到的值
43. plusSeconds 返回LocalDateTime加上指定秒數(shù)得到的值
44. plusWeeks 返回LocalDateTime加上指定星期數(shù)得到的值
45. plusYears 返回LocalDateTime加上指定年數(shù)得到的值
46. query 查詢LocalDateTime
47. range 返回指定時(shí)間字段的范圍
48. toLocalDate 返回LocalDateTime的LocalDate部分
49. toLocalTime 返回LocalDateTime的LocalTime部分
50. toString 返回LocalDateTime的字符串表示
51. truncatedTo 返回LocalDateTime截取到指定時(shí)間單位的拷貝
52. until 計(jì)算LocalDateTime和另一個(gè)LocalDateTi
53. with 返回LocalDateTime指定字段更改為新值后的拷貝
54. withDayOfMonth 返回LocalDateTime月的第幾天更改為新值后的拷貝
55. withDayOfYear 返回LocalDateTime年的第幾天更改為新值后的拷貝
56. withHour 返回LocalDateTime的小時(shí)數(shù)更改為新值后的拷貝
57. withMinute 返回LocalDateTime的分鐘數(shù)更改為新值后的拷貝
58. withMonth 返回LocalDateTime的月份更改為新值后的拷貝
59. withNano 返回LocalDateTime的納秒數(shù)更改為新值后的拷貝
60. withSecond 返回LocalDateTime的秒數(shù)更改為新值后的拷貝
61. withYear 返回LocalDateTime年份更改為新值后的拷貝 來源:https://www./content-1-699001.html
|