Date和LocaDateTime之间使用
一、Date使用
Date可读性差、易用性差、使用起来冗余繁琐。java.util.Date
是非线程安全的。java.util.Date
不支持国际化和时区、故大部分方法被声明为过时、不建议再使用。
- date使用
构造方法 | 获取时间 | 设置时间 | 时间比较 | 格式转换 |
---|---|---|---|---|
Date (): 创建一个表示当前时间的Date 对象。Date(long date) : 根据指定的毫秒数创建一个Date对象。 | getTime (): 返回从1970年1月1日00:00:00以来的毫秒数。 | setTime(long time) : 设置Date对象的时间。 | before(Date when) : 判断当前日期是否在指定日期之前。after(Date when) : 判断当前日期是否在指定日期之后。equals(Object obj) : 判断两个日期是否相等。 | toString() : 将日期对象转换为字符串。toLocaleString() : 将日期对象转换为本地化的字符串。 |
- 格式化输出需要用到
SimpleDateFormat
这个类
y 年
M 年中的月份
w 年中的周数
W 月份中的周数
D 年中的天数
d 月份中的天数
F 月份中的星期
E 星期中的天数
a Am/pm 标记
H 一天中的小时数(0-23)
k 一天中的小时数(1-24)
K am/pm 中的小时数(0-11)
h am/pm 中的小时数(1-12)
m 小时中的分钟数
s 分钟中的秒数
S 毫秒数
z 时区
import java.text.SimpleDateFormat;
import java.util.Date;
public class datetest {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sd=new SimpleDateFormat("现在时间: "+"yyyy 年 MM 月 dd 日 E HH 点 mm 分 ss 秒");
System.out.println(sd.format(now));
sd = new SimpleDateFormat("一年中的第 D 天,一个月中的第 F 个星期 ,一年中的第 w 个星期,一个月中的第 W 个星期");
String str=sd.format(now);
Date date=sd.parse(str);//抛异常,字符串转换为date类
System.out.println(sd.format(now));
}
}
二、LocalDateTime使用
Java8
新增的日期类主要有三个,这些类使用了final来修饰,使得这些类是不可变的,一旦实例化,值就固定了,有点类似于String类。
LocalDate
:表示日期(年月日)LocalTime
:表示时间(时分秒)LocalDateTime
:表示时间+ 日期 (年月日时分秒),是java8
最常用的日期类
使用方法
- 创建方法
LocalDate now1 = LocalDate.now(); // 当前日期
LocalDate now2 = LocalDate.now(ZoneId.of("Asia/Shanghai")); // 当前日期 (指定时区)
LocalDate now3 = LocalDate.now(Clock.systemDefaultZone()); // 当前日期 (指定时钟)
LocalDate localDate = LocalDate.of(2023, 1, 1); // 指定日期 2023-01-01
- 获取方法
LocalDate now = LocalDate.now();
int year = now.getYear(); // 获取年份
int month = now.getMonthValue(); // 获取月份(1-12)
Month monthEnum = now.getMonth(); // 获取月份的枚举值
int dayOfMonth = now.getDayOfMonth(); // 获取月份中的第几天(1-31)
int dayOfYear = now.getDayOfYear(); // 获取一年中的第几天(1-366)
DayOfWeek dayOfWeek = now.getDayOfWeek(); // 获取现在是星期几
int dayOfYear = now.getDayOfYear(); // 获取一年中的第几天(1-366)
int lengthOfYear = now.lengthOfYear(); // 获得当年总天数
int lengthOfMonth = now.lengthOfMonth(); // 获得当月总天数
long epochDay = now.toEpochDay(); // 与时间纪元(1970年1月1日)相差的天数
- 运算方法
LocalDate now = LocalDate.now();
LocalDate localDate1 = now.plusDays(1); // 给当前时间加一天
LocalDate localDate2 = now.plusDays(1); // 给当前时间加一周
LocalDate localDate3 = now.plusMonths(1); // 给当前时间加一月
LocalDate localDate4 = now.plusYears(1); // 给当前时间加一年
LocalDate localDate5 = now.minusDays(1); // 给当前时间减一天
LocalDate localDate6 = now.minusWeeks(1); // 给当前时间减一周
LocalDate localDate7 = now.minusMonths(1); // 给当前时间减一月
LocalDate localDate8 = now.minusYears(1); // 给当前时间减一年
- 比较方法
LocalDate now = LocalDate.now();
LocalDate localDate = LocalDate.of(2023, 1, 1)
boolean isBefore = localDate.isBefore(now); // localDate是否在当天之前
boolean isAfter = localDate.isAfter(now); // localDate是否在当天之后
boolean isEqual = localDate.isEqual(now); // localDate是否在当天
boolean isLeapYear = localDate.isLeapYear(); // localDate是否是闰年
- Date与LocalDate之间转换
/**
* LocalDate转Date
* @param localDate
* @return
*/
public static Date toDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
}
/**
* Date转LocalDate
* @param Date
* @return
*/
public static Date toLocalDate(Date date) {
return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
}
- 转换代码示例
public static void main(String[] args) throws ParseException {
Date date=new Date();
//Date格式转化
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dayEndStr = sdf.format(date) + " 23:59:59";
System.out.println("Date格式转化: "+dayEndStr);
Date dayEnd = sdf.parse(dayEndStr);//会抛异常
System.out.println("date: "+dayEnd);
System.out.println("---------------------------------");
/**
Date格式转化: 2024-07-25 14:50:47 23:59:59
date: Thu Jul 25 14:50:47 CST 2024
---------------------------------
*/
//LocalDateTime
LocalDateTime localDateTime=LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
LocalTime localTime = localDateTime.toLocalTime();
System.out.println("localDateTime: "+localDateTime);
System.out.println("localDate: "+localDate);
System.out.println("localTime: "+localTime);
System.out.println("---------------------------------");
/**
localDateTime: 2024-07-25T14:50:47.961
localDate: 2024-07-25
localTime: 14:50:47.961
---------------------------------
*/
//LocalDateTime格式转化
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format1 = format.format(localDateTime);
//String转Date
Date parse = sdf.parse(format1);
System.out.println("LocalDateTime格式转化: "+format1);
System.out.println("String转Date: "+parse);
//转化为数字时间戳
Instant instant = localDateTime.atZone(ZoneId.of("Asia/Shanghai")).toInstant();
System.out.println("毫秒值:"+instant.toEpochMilli());
System.out.println("秒值:"+instant.getEpochSecond());
System.out.println("---------------------------------");
/**
LocalDateTime格式转化: 2024-07-25 14:50:47
String转Date: Thu Jul 25 14:50:47 CST 2024
毫秒值:1721890247961
秒值:1721890247
---------------------------------
*/
// 设置各部分时间
LocalDateTime newYear = localDateTime.withYear(4040); // 改为4040年
LocalDateTime newMonth = localDateTime.withMonth(10); // 改为10月份
LocalDateTime newDay = localDateTime.withDayOfMonth(12).withHour(10).withMinute(0).withSecond(0).withNano(0); // 改为12日 10:0:0:0
System.out.println("newYear: "+newYear);
System.out.println("newMonth: "+newMonth);
System.out.println("newDay: "+newDay);
System.out.println("---------------------------------");
/**
newYear: 4040-07-25T14:50:47.961
newMonth: 2024-10-25T14:50:47.961
newDay: 2024-07-12T10:00
---------------------------------
*/
//Date转LocalDateTime
LocalDateTime localDateTime1 = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("Date转LocalDateTime: "+localDateTime1);
//LocalDateTime转Date
Date from = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("LocalDateTime转Date: "+from);
System.out.println("---------------------------------");
/**
Date转LocalDateTime: 2024-07-25T14:50:47.890
LocalDateTime转Date: Thu Jul 25 14:50:47 CST 2024
---------------------------------
*/
//日期计算
LocalDateTime tomorrow = localDateTime.plusDays(1L);
tomorrow = tomorrow.plusHours(2L);
tomorrow = tomorrow.plusMinutes(10L);
LocalDateTime yesterday = localDateTime.minus(Duration.ofDays(1));
yesterday = yesterday.plusHours(2L);
yesterday = yesterday.plusMinutes(10L);
System.out.println("tomorrow: "+tomorrow);
System.out.println("yesterday: "+yesterday);
// tomorrow: 2024-07-26T17:00:47.961
// yesterday: 2024-07-24T17:00:47.961
/**
Date格式转化: 2024-07-25 14:50:47 23:59:59
date: Thu Jul 25 14:50:47 CST 2024
---------------------------------
localDateTime: 2024-07-25T14:50:47.961
localDate: 2024-07-25
localTime: 14:50:47.961
---------------------------------
LocalDateTime格式转化: 2024-07-25 14:50:47
String转Date: Thu Jul 25 14:50:47 CST 2024
毫秒值:1721890247961
微秒值:1721890247
---------------------------------
newYear: 4040-07-25T14:50:47.961
newMonth: 2024-10-25T14:50:47.961
newDay: 2024-07-12T10:00
---------------------------------
Date转LocalDateTime: 2024-07-25T14:50:47.890
LocalDateTime转Date: Thu Jul 25 14:50:47 CST 2024
---------------------------------
tomorrow: 2024-07-26T17:00:47.961
yesterday: 2024-07-24T17:00:47.961
*/
}