SimpleDateFormat
是个线程不安全的,使用的时候,只能在方法内部创建新的局部变量。而DateTimeFormatter
不但是不变对象,它还是线程安全的。所以DateTimeFormatter
可以只创建一个实例,全局引用。
日期时间转字符串 和 字符串转日期时间
public static void main(String[] args) throws Exception {
LocalDate now = LocalDate.now();//当前日期
System.out.println(now);//2022-06-22
LocalDateTime dateTime = LocalDateTime.now();//当前日期和时间
System.out.println(dateTime);//2022-06-22T10:00:45.659
LocalDate localDate = LocalDate.now().plusDays(1);//明天
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMdd");
//日期时间转字符串
String dateStr = localDate.format(fmt) + "0930";//明天09:30
System.out.println(dateStr);//202206230930
//字符串转时间
LocalDateTime date = LocalDateTime.parse(dateStr, DateTimeFormatter.ofPattern("yyyyMMddHHmm"));
System.out.println(date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));//2022-06-23 09:30:00
//日期时间加减 t1.until(t2, x) == t2 - t1
long until = LocalDateTime.now().until(LocalDateTime.now().plusDays(1), ChronoUnit.MINUTES);//距离明天有多少分钟
System.out.println("距离明天有多少分钟:" + until);//1440
}
加一天、减一天
DateTimeFormatter fmtful = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime now = LocalDateTime.now();
// 今天
System.out.println(now.format(fmtful));
//加一天
LocalDateTime tomorrow = now.plusDays(1);//明天
System.out.println(tomorrow.format(fmtful));
//减一天
LocalDateTime yesterday = now.minusDays(1);//昨天
System.out.println(yesterday.format(fmtful));
加一分钟、减一分钟
DateTimeFormatter fmtful = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime now = LocalDateTime.now();
// 今天
System.out.println(now.format(fmtful));
//加一分钟
LocalDateTime plus = now.plusMinutes(1);
System.out.println(plus.format(fmtful));
//减一分钟
LocalDateTime minus = now.minusMinutes(1);
System.out.println(minus.format(fmtful));
LocalDateTime 类型的时间大小比较
compareTo 前者减去后者,返回比较器值,如果小于则为负值,如果大于则为正值,
public static void main(String[] args) {
// 当前日期时间
LocalDateTime now = LocalDateTime.now();
// compareTo 返回比较器值,如果小于则为负值,如果大于则为正值
System.out.println(LocalDate.now().compareTo(LocalDate.now().minusDays(3)));//结果:3
System.out.println(LocalDate.now().compareTo(LocalDate.now().plusDays(5)));//结果:-1
int i = now.compareTo(now);
System.out.println(i);// 相等 i==0
// 加3分钟
LocalDateTime nowAdd = now.plusMinutes(3);
i = now.compareTo(nowAdd);
System.out.println(i);// 小于 i<0
// 减3分钟
LocalDateTime nowMinus = now.minusMinutes(3);
i = now.compareTo(nowMinus);
System.out.println(i);// 大于 i>0
}
LocalDateTime 转 LocalDate
LocalDate 转 LocalDateTime
public static void main(String[] args) {
// LocalDateTime 转 LocalDate
LocalDate localDate = LocalDateTime.now().toLocalDate();
System.out.println(localDate);//2022-12-29
// LocalDate 转 LocalDateTime
LocalDateTime localDateTime1 = LocalDate.now().atStartOfDay();
System.out.println(localDateTime1);//2022-12-29T00:00
LocalDateTime localDateTime2 = LocalDate.now().atTime(00,01,02);
System.out.println(localDateTime2);//2022-12-29T00:01:02
LocalDateTime localDateTime3 = LocalDate.now().atTime(LocalTime.now());
System.out.println(localDateTime3);//2022-12-29T12:43:04.564
}
时间戳转 LocalDateTime、 LocalDate
long l = System.currentTimeMillis();//毫秒
LocalDateTime localDateTime = Instant.ofEpochMilli(l).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
LocalDate localDate = Instant.ofEpochMilli(l).atZone(ZoneOffset.ofHours(8)).toLocalDate()
Timestamp与LocalDateTime互转
long time = System.currentTimeMillis();
// 用 long 类型时间戳
LocalDateTime localDateTime = Instant.ofEpochMilli(time).atZone(ZoneId.systemDefault()).toLocalDateTime();
// 用 Timestamp
LocalDateTime localDateTime1 = new Timestamp(time).toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// 用 Timestamp,
LocalDateTime localDateTime2 = new Timestamp(time).toLocalDateTime();
// 用 long 类型时间戳创建 Timestamp
Timestamp timestamp = new Timestamp(time);
// 用 LocalDateTime 创建 Timestamp
Timestamp timestamp1 = Timestamp.valueOf(localDateTime);
// 返回 long 类型时间戳
long newTime = timestamp.getTime();
LocalDateTime 、 LocalDate 转时间戳转
long l = LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();//毫秒
System.out.println(l==System.currentTimeMillis());
Date转LocalDateTime
// 方法一
LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
// 方法二
LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
LocalDateTime转Date
Date d = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());