java8时间日期
获取当前时间日期 now、指定时间日期等操作
@Test
public void test01(){
//获取当前时间日期 now
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//指定时间日期 of
LocalDateTime ldt = LocalDateTime.of(2020, 05, 17, 16, 24, 33);
System.out.println(ldt);
//加 plus
LocalDateTime ldt2 = ldt.plusYears(2);
System.out.println(ldt2);
//减 minus
LocalDateTime ldt3 = ldt.minusMonths(3);
System.out.println(ldt3);
//获取指定的你年月日时分秒
System.out.println(ldt.getDayOfYear()); //一年的第几天
System.out.println(ldt.getHour());
System.out.println(ldt.getSecond());
}
结果:
Instant与LocalDateTime、ZoneDateTime互相转换
@Test
public void test02(){
Instant ins = Instant.now(); //世界标准时间
// Instant转为对应的系统LocalDateTime:当前世界标准时间转为本地当前时间
ZoneId zone = ZoneId.systemDefault(); //首先获取当前系统时区
LocalDateTime localDateTime = LocalDateTime.ofInstant(ins, zone); // 根据世界标准时和当前系统时区进行转换
System.out.println("世界标准时间转为当前系统时区时间:"+localDateTime);
// LocalDateTime转Instant: 将本地时间转为对应的世界标准时间
LocalDateTime now = LocalDateTime.now(); // 获取系统当前时间
ZoneId zoneId = ZoneId.systemDefault(); // 获取当前系统时区
Instant localDateTimeToInstant = now.atZone(zoneId).toInstant(); // 根据当前系统时区转为世界标准时间
System.out.println("当前时间转为世界标准时间:"+localDateTimeToInstant);
// Instant转为ZoneDateTime
Instant instantZ=Instant.now();
ZonedDateTime zonedDateTime=ZonedDateTime.ofInstant(instantZ,ZoneId.systemDefault());
System.out.println("Instant转为ZoneDateTime:"+zonedDateTime);
// ZoneDateTime转为Instant
Instant instant = zonedDateTime.toInstant();
System.out.println("ZoneDateTime转为Instant:"+instant);
// ZoneDateTime转为LocalDateTime
LocalDateTime localDateTime2 = zonedDateTime.toLocalDateTime();
System.out.println("ZoneDateTime转为LocalDateTime:"+localDateTime2);
// LocalDateTime 转为 ZonedDateTime
ZonedDateTime zonedDateTime1 = now.atZone(ZoneId.systemDefault());
System.out.println("LocalDateTime 转为 ZonedDateTime:"+zonedDateTime1);
}
结果:
一个时区的时间转为另一个时区的时间
@Test
public void test03(){
LocalDateTime now1 = LocalDateTime.now();
// ZonedDateTime now = LocalDateTime.now().atZone(ZoneId.of("Asia/Shanghai"));
// 1:LocalDateTime转Instant: 将本地时间转为对应的世界标准时间
// Instant ins= now.toInstant();
Instant ins= now1.atZone(ZoneId.systemDefault()).toInstant();
System.out.println("instant:"+ins);
// 2:世界标准时间转为对应时区的当前时间: 上海时间转为东京时间
ZonedDateTime zonedDateTime1 = ins.atZone(ZoneId.of("Asia/Tokyo"));
System.out.println("世界标准时间转为对应时区的当前时间:"+zonedDateTime1);
}
结果:
时间日期格式化
LocalDateTime与String互相转换
@Test
public void test04(){
// 默认时间格式化(世界标准时间格式:2021-09-27T17:44:24.552)
DateTimeFormatter dtf1 = DateTimeFormatter.ISO_DATE_TIME;
// 自定义时间格式化
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
// LocalDateTime转为String
String localTime = df.format(time);
System.out.println("使用自定义时间格式化转为字符串:"+localTime);
String localTime2 = dtf1.format(time);
System.out.println("使用默认时间格式化转为字符串:"+localTime2);
// String转为LocalDateTime(格式必须为:yyyy-MM-dd HH:mm:ss,才能使用上述日期时间格式器)
LocalDateTime ldt = LocalDateTime.parse("2017-09-28 17:07:05",df);
System.out.println("使用自定义时间格式化将字符串转为时间:"+ldt);
}
结果:
Instant与String互相转化
@Test
public void test05(){
Instant instant = Instant.now();
// Instant转为String
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault())
.withLocale(Locale.CHINA );
String instantStr = formatter.format( instant );
System.out.println("Instant转为String:"+instantStr);
// 字符串转为Instant格式,但是时间不会变为世界标准时间
Instant strToInstant = LocalDateTime.parse("2017-06-23 15:34:20", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
.withZone(ZoneId.systemDefault())
.withLocale(Locale.CHINA ))
.toInstant(ZoneOffset.UTC);
System.out.println("字符串转为Instant格式,但是时间不会变为世界标准时间:"+strToInstant);
// 字符串转为Instant, 本地时间变为世界标准时间
LocalDateTime localDateTime1 = LocalDateTime.parse("2017-06-23 15:34:20", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Instant instant1 = localDateTime1.atZone(ZoneId.systemDefault()).toInstant();
System.out.println("字符串转为Instant, 本地时间变为世界标准时间:"+instant1);
}
结果:
获取系统支持的所有时区
@Test
public void test06(){
// 获取 JVM 启动时获取的时区
TimeZone aDefault = TimeZone.getDefault();
// 获取 JVM 启动时获取的时区ID
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
System.out.println("---------------------------------");
// 获取系统支持的所有时区
String[] zoneIDs = TimeZone.getAvailableIDs();
for(String zoneID: zoneIDs) {
TimeZone timeZone = TimeZone.getTimeZone(zoneID);
System.out.println(timeZone.getID());
}
}
结果:
Date转为LocalDateTime
@Test
public void test07(){
Date date = new Date();
Instant instant = date.toInstant(); // Date先转为Instant,世界标准时间
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); // Instant转为LocalDateTime
System.out.println("Date转为LocalDateTime:"+localDateTime);
LocalDate localDate = localDateTime.toLocalDate();// 只获取日期
LocalTime localTime = localDateTime.toLocalTime();// 只获取时间
System.out.println("只获取日期"+localDate);
System.out.println("只获取时间"+localTime);
}
结果:
LocalDateTime 转换为 Date
@Test
public void test08(){
LocalDateTime localDateTime = LocalDateTime.now();
// LocalDateTime转为Instant
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
// Instant转为Date
Date date = Date.from(instant);
System.out.println("LocalDateTime转为date:"+date);
}
结果:
LocalDate转为Date以及LocalTime 转换为 Date
@Test
public void test09(){
// LocalDate转为Date
LocalDate localDate = LocalDate.now();
// LocalDate转Instant
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
// Instant转为Date
Date date = Date.from(instant);
System.out.println("LocalDate转为Date:"+date);
// LocalTime 转换为 Date
LocalTime localTime = LocalTime.now();
LocalDate localDate2 = LocalDate.now();
LocalDateTime localDateTime = LocalDateTime.of(localDate2, localTime);
Instant instant2 = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date date2 = Date.from(instant2);
System.out.println("LocalTime 转换为 Date:"+date2);
}
结果:
时间戳转 LocalDateTime
LocalDateTime localDateTime = Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
结果:
LocalDateTime 转时间戳
LocalDateTime now = LocalDateTime.now();
long timestamp = now.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
System.out.println(timestamp);
结果: