Java8 日期时间操作
2020-07-17 本文已影响0人
xiaogp
操作包括
- java.util.Date().getTime()返回13位时间戳
- System.currentTimeMillis 计算毫秒差
- SimpleDateFormat 解析和格式化日期
- LocalDate
- LocalTime
- LocalDateTime
- Duration
- Period
- 计算日间隔 toEpochDay
- 日期相互加减
- 日期时间和日期相互转换
- 时间类型和时间戳互相转化
java.util.Date().getTime()
返回13位时间戳
long a = new java.util.Date().getTime();
System.out.println(a); // 1602243261784
System.currentTimeMillis
常用来计时, 单位是毫秒
// 使用 System.currentTimeMillis
// 主要用来计算时间差
public static void getDiffTime() {
try {
long start = System.currentTimeMillis();
Thread.sleep(1000);
long end = System.currentTimeMillis();
System.out.println(end - start);
} catch (Exception e) {
e.printStackTrace();
}
}
SimpleDateFormat
常用来解析和格式化日期
// SimpleDateFormat 常用来输出标准格式日期
// yyyy-MM-dd HH:mm:ss
public static void getFormatDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(simpleDateFormat.format(new Date()));
}
public static void getParseDate() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = simpleDateFormat.parse("2020-02-02");
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
}
}
LocalDate
获得日期类型的时间, 并且获得年月日
// LocalDate 获得当前日期, 并且获得年. 月, 日
public static void getNowDate() {
LocalDate date = LocalDate.now();
System.out.println(date);
System.out.println(date.getDayOfMonth());
System.out.println(date.getMonthValue());
System.out.println(date.getYear());
// 可以format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
String formatDate = date.format(formatter);
System.out.println(formatDate);
}
LocalTime
获得当前的日期时间, 并且获得时,分,秒
// LocalTime, 获得当前的日期时间, 并且获得时,分,秒
public static void getNowTime() {
LocalTime time = LocalTime.now(); // 20:13:56.241
System.out.println(time);
System.out.println(time.getHour());
System.out.println(time.getMinute());
System.out.println(time.getSecond());
// 可以format
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
String formatTime = time.format(formatter);
System.out.println(formatTime);
}
LocalDateTime
获得当前的日期时间, 并且获得年月日时分秒
// LocalDateTime, 获得当前的日期时间, 并且获得年月日时分秒
public static void getNowDateTime() {
LocalDateTime datetime = LocalDateTime.now();
System.out.println(datetime);
System.out.println(datetime.getYear());
System.out.println(datetime.getDayOfMonth());
System.out.println(datetime.getMonthValue());
System.out.println(datetime.getHour());
System.out.println(datetime.getMinute());
System.out.println(datetime.getSecond());
// format
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatDatetime = datetime.format(dateTimeFormatter);
System.out.println(formatDatetime); // 2020-07-17 09:22:54
}
获得指定时间类型的数据
可以使用parse解析输入的字符串
// 指定日期时间
public static void getTimeDefine() {
String time1 = "2020-02-02";
String time2 = "2020-02-02 15:03";
String time3 = "16:20:20";
LocalDate time1Parse = LocalDate.parse(time1);
System.out.println(time1Parse);
System.out.println(time1Parse.getDayOfMonth());
LocalDateTime time2Parse = LocalDateTime.parse(time2, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
System.out.println(time2Parse);
System.out.println(time2Parse.getHour());
System.out.println(time2Parse.getSecond()); // 默认是0
// 解析之后再格式化输出
System.out.println(time2Parse.format(DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss")));
LocalTime time3Parse = LocalTime.parse(time3);
System.out.println(time3Parse);
System.out.println(time3Parse.getHour());
System.out.println(time3Parse.format(DateTimeFormatter.ofPattern("HH:mm")));
}
或者使用of方法
public static void getTimeDefine2() {
LocalDateTime time1 = LocalDateTime.of(2020, 2, 3, 13, 2, 33);
System.out.println(time1);
System.out.println(time1.getYear());
System.out.println(time1.getSecond());
LocalDate time2 = LocalDate.of(2020, 2, 3);
System.out.println(time2);
System.out.println(time2.getDayOfMonth());
System.out.println(time2.getYear());
}
Duration
时间区间, 使用秒和纳秒衡量, 只能用DateTime
// 计算日期差
public static void getDiffDate() {
// Duration用秒和纳秒衡量时间, 所以只能用LocalDateTime
LocalDateTime date1 = LocalDateTime.parse("2020-02-02 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
LocalDateTime date2 = LocalDateTime.of(2020, 4, 2, 0, 0, 0);
Duration duration = Duration.between(date1, date2);
System.out.println(duration.toString()); // PT-744H
System.out.println(duration.toDays()); // 后 - 前
System.out.println(duration.toHours());
System.out.println(duration.toMillis());
}
Period
时间区间, 衡量年月日和几天之间的时间值
// 计算日期差Period
public static void getDiddDate2() {
// Period
// 计算间隔多少年月天, 以大单位先开始排, toTotalMonths可以获得总共间隔多少月
LocalDate date1 = LocalDate.of(2018, 2, 2);
LocalDate date2 = LocalDate.of(2020, 1, 10);
Period period = Period.between(date1, date2);
System.out.println(period.getYears());
System.out.println(period.getDays());
System.out.println(period.getMonths());
System.out.println(period.toTotalMonths());
System.out.printf("区间间隔%d年%d月%d天\n", period.getYears(), period.getMonths(), period.getDays());
}
toEpochDay 计算相差多少日
// 计算日期相差多少天
public static void getDiffDay() {
LocalDate date1 = LocalDate.of(2020, 2, 3);
LocalDate date2 = LocalDate.of(2020, 1, 19);
long date1EpochDay = date1.toEpochDay(); // 返回的是距离1970-01-01的天数
System.out.println(date1EpochDay);
long diffDays = date1.toEpochDay() - date2.toEpochDay();
System.out.println(diffDays);
}
日期相加减
// 日期相加减
public static void plusMinDay() {
LocalDate date1 = LocalDate.of(2020, 2, 3);
LocalDate date2 = date1.plusDays(3);
System.out.println(date2);
LocalDate date3 = date1.plusYears(2);
System.out.println(date3);
LocalDate date4 = date1.minusDays(10);
System.out.println(date4);
LocalDate date5 = date1.minusDays(-10);
System.out.println(date5);
}
时间, 日期, 日期时间之间的互相转化
// time, date, datetime之间互相转化
public static void dateToTimeStamp() {
// date => datetime asTime
LocalDate date1 = LocalDate.of(2020, 1, 2);
LocalDateTime time1 = date1.atTime(LocalTime.parse("06:30:20"));
LocalDateTime time2 = date1.atTime(LocalTime.of(6,30,30));
System.out.println(time1.toString());
System.out.println(time2.toString());
// time => datetime
LocalTime time6 = LocalTime.of(13, 13, 13);
LocalDateTime date3 = time6.atDate(LocalDate.parse("2020-02-02"));
System.out.println(date3);
// datetime => date / time
LocalDateTime time3 = LocalDateTime.of(2020, 2, 2, 13, 1, 1);
LocalDate date2 = time3.toLocalDate();
System.out.println(date2.toString());
LocalTime time4 = time3.toLocalTime();
System.out.println(time4);
}
时间类型和时间戳互相转化
// 转化为时间戳
public static void dateTimeToTimestamp() {
// datetime转时间戳
LocalDateTime time1 = LocalDateTime.of(2020,2,2,2,2,2);
long timestrap = time1.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
System.out.println(timestrap); // 1580580122000 13位时间戳
// 时间戳转datetime
Instant instant = Instant.ofEpochMilli(1580580122000L);
LocalDateTime time2 = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
System.out.println(time2.toString());
}