Java日期时间处理总结

java.util.Date、java.util.Calendar(jdk1.8前)、java.time.LocalDate(jdk1.8后线程安全)

Date类

// 1626624005531  时间戳为Long类型,适用于计算时间差
Long time = System.currentTimeMillis();

//时间戳和Date类互相转换
Date date = new Date();
Long time = date.getTime();
// 时间戳转换为 Date 类型
Long long = 1626624005531L;
Date nowTime = new Date(long);

//java.util.Date 类与 java.text.SimpleDateFormat 类一起用,用于格式化日期
//getDateTimeInstance(),getDateInstance(),getTimeInstance()静态方法获取标准日期格式
System.out.println(new Date());                   // Thu Jul 29 10:30:04 CST 2021  
String s = SimpleDateFormat.getDateTimeInstance().format(new Date());   //2021-7-29 10:30:04
//将字符串转换为日期
String timeOne = "2021-07-18 22:53:22";
Date date = SimpleDateFormat.getDateTimeInstance().parse(timeOne);

Calendar类

java.util.Calendar:日历类,是个抽象类
Calendar 实例化的方法:

  1. 创建其子类 GregorianCalendar的对象
  2. 调用其静态方法 getInstance()
//两种方法本质上相同,调用静态方法获取的对象就是子类的对象
Calendar calendar = Calendar.getInstance();
// class java.util.GregorianCalendar
System.out.println(calendar.getClass());

常用方法:
● get(int field):获取某个时间
● set():设置某个时间
● add():在某个时间上,加上某个时间
● getTime():将 Calendar 类转换为 Date 类
● setTime():将 Date 类转换为 Calendar 类
field 取值有:
● Calendar.DAY_OF_MONTH:这个月的第几天
● Calendar.DAY_OF_YEAR:这年的第几天

//get()获取当前时间
int i = calendar.get(Calendar.DAY_OF_MONTH);
//set(int field, int value)设置当前时间
calendar.set(Calendar.DAY_OF_MONTH, 23);
//add(int field, int value)给当前时间添加时间
calendar.add(Calendar.DAY_OF_MONTH, 5);

//和Date类相互转化
Date time = calendar.getTime();
calendar.setTime(new Date());

java.time.LocalDate(jdk1.8后线程安全)

https://blog.csdn.net/sco5282/article/details/118914681
java.time 中包含了:
● LocalDate类:本地日期。yyyy-MM-dd 格式的日期。可以存储生日、纪念日等
● LocalTime类:本地时间。代表的是时间,不是日期
● LocalDateTime类:本地日期时间
● ZonedDateTime类:时区
● Duration类:持续时间
java.time常用API:
● now():获取当前时间、日期
● of():获取指定的日期、时间
● getXxx():获取值
● withXxx():设置值
● plusXxx():加
● minusXxx():减
格式化:先获取格式对象
format对象.format() 时间对象转换为字符串
format对象.parse() 字符串转化为时间对象

System.out.println(LocalDateTime.now());   //2021-07-29T10:53:35.429
LocalDateTime dateTime = LocalDateTime.of(2021, 7, 19, 21, 50, 0);  //2021-07-19T21:50

System.out.println(localDateTime.getDayOfMonth());     // 19
System.out.println(localDateTime.getMonth());          // JULY

//具有不可变性,设置后返回对象为新对象
LocalDateTime dayOfMonth = localDateTime.withDayOfMonth(25);    //2021-07-25T10:58:10.903
System.out.println(localDateTime);                              //2021-07-29T10:58:10.903
LocalDateTime l = now.withYear(2002).withDayOfMonth(2).withMonth(2);

LocalDateTime months = localDateTime.plusMonths(5);

//将时间转换为字符串格式   FormatStyle.MEDIUM表示格式 2021-7-29 11:08:03 
String format = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM).format(LocalDateTime.now());
//自定义格式    2021-07-29 11:08:03  
String format1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
//字符串转化为时间
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2021-07-29 11:27:08", dateTimeFormatter);

mysql中比较时间大小

前端传来的都是字符串格式,如果类似于2020-01-13T16:00:00.000Z,是世界标准时间,使用DateFormatUtils转换
● 时间类型:直接用 between and
● 字符串 ->时间: STR_TO_DATE(report_time,'%Y-%m-%d %H:%i:%s')
● 时间戳 ->时间: from_unixtime(1592755199)

原文地址:https://www.cnblogs.com/centralpark/p/15420151.html