目录
部分参考: https://juejin.cn/post/6969933701153030174
格式化日期
SimpleDateFormat
- Date格式化 ==SimpleDateFormat.parse()方法将String转为Date==
-
格式 释义 举例 yyyy 年 2019 MM 月 02 dd 日 18 HH 小时(24小时制) 13,下午一点 mm 分钟 53 ss 秒 42 SSS 毫秒 629
日期转换为字符串
Date date = new Date();
System.out.println(date);
//使用SimpleDateFormat,自定义格式
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = dateFormat.format(date);
System.out.println(format);
字符串转日期
Date date = new Date();
System.out.println(date);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = dateFormat.format(date);
//转化后是String,将String 转换成Date
try {
Date parse = dateFormat.parse(format);
} catch (ParseException e) {
e.printStackTrace();
}
//显示如下
Tue Nov 26 15:00:04 CST 2019
2019-11-26 15:00:04
Tue Nov 26 15:00:04 CST 2019
Calendar格式化
//getTime方法将Calendar转换成Date后在格式化
Calendar instance = Calendar.getInstance();
Date date = instance.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = dateFormat.format(date);
System.out.println(format);
LocalDate格式化
- //只获取日期
- LocalDate
- //只获取时间
LocalTime
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
LocalDateTime dateTime = LocalDateTime.now();
System.out.println(date);
System.out.println(time);
System.out.println(dateTime);
//显示如下
2019-11-26
15:15:23.722
2019-11-26T15:15:23.722
格式化 localDate 自带format()方法,需要传参DateTimeFormatter
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String format = date.format(dateTimeFormatter);
System.out.println(format);
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("HH:mm:ss");
String format2 = time.format(dateTimeFormatter2);
System.out.println(format2);
//显示如下
20191126
16:08:52
时间戳(timestamp)
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
获取当前时间戳
//获取时间戳,单位毫秒
long l = System.currentTimeMillis();
System.out.println(l);
//
Date date = new Date();
long time = date.getTime();
System.out.println(time);
//
long timeInMillis = Calendar.getInstance().getTimeInMillis();
System.out.println(timeInMillis);
时间戳转日期
//Date构造可直接转换
Date date1 = new Date(1574758172536L);
System.out.println(date1);
计算时间间隔
Duration
import java.time.Duration;
import java.time.LocalDateTime;
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2022,1,1,8,0,0);
System.out.println("计算两个时间的差:");
LocalDateTime end = LocalDateTime.of(2022,1,2,8,30,30);
//创建时间区间 between
//Duration duration = Duration.between(start, end);
//创建时间区间 parse("PnDTnHnMn.nS")
//Duration duration = Duration.parse("PT15M");
//创建时间区间 from
//Duration du1 = Duration.ofHours(10);
//Duration duration = Duration.from(du1);
//创建时间区间 Duration.of(long amount, TemporalUnit unit);unit 详见ChronoUnit枚举类
//Duration duration = Duration.of(2, ChronoUnit.DAYS);
//创建时间区间 Duration.ofDays(long days)
Duration duration = Duration.ofDays(2);
//区间加减运算:plus minus (days、hours、Minutes、Seconds、Millis、Nanos)
duration = duration.plus(Duration.ofDays(2));
duration = duration.minus(Duration.ofDays(1));
duration = duration.plusDays(2);
duration = duration.minusDays(1);
long days = duration.toDays();
System.out.println("相差:"+days+"天");
long hours = duration.toHours();
System.out.println("相差:"+hours+"小时");
long minutes = duration.toMinutes();
System.out.println("相差:"+minutes+"分");
long millis = duration.toMillis();
System.out.println("相差:"+millis+"毫秒");
long nanos = duration.toNanos();
System.out.println("相差:"+nanos+"纳秒");
}