关于时间戳和LocalDateTime和Date互转
时间戳与LocalDateTime互转
方式一 :
这边值得一提的是在中国的时区偏移是8小时,本次示例转的时间戳是秒级别,得到的值是一个long值;知识追寻者这边是当前时间,故读者得到的结果与知识追寻者得到的结果不一致;读者可以使用站长工具进行测试校验
public void localDateTimeToSecond(){
// 获得当前时间
LocalDateTime now= LocalDateTime.now();
// 将当前时间转为时间戳
long second = now.toEpochSecond(ZoneOffset.ofHours(8));
// 1580706475
System.out.println(second);
}
方式二 :
public void localDateTimeToSecond(){
// 获得当前时间
LocalDateTime now = LocalDateTime.now();
// 将当前时间转为时间戳
long second = now.toInstant(ZoneOffset.ofHours(8)).getEpochSecond();
// 1580707001
System.out.println(second);
}
方式三 :
此方式转的将是毫秒级别,应该将其除1000取商获得正确的秒级时间戳;
public void localDateTimeToMilliseconds (){
// 获得当前时间
LocalDateTime now = LocalDateTime.now();
// 将当前时间转为时间戳
long milliseconds = now.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
// 1580707268
System.out.println(milliseconds/1000);
}
时间戳 转LocalDateTime
以下几种获取的LocalDateTime方式按需求进行获取,不同的精确值,将获取不同的结果;
方式一 :
先获取时间戳为秒级别,然后通过转换为LocalDateTime
public void secondToLocalDateTime(){
//获得时间戳
long second = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
// 将时间戳转为当前时间
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
// 2020-02-03T13:30:44
System.out.println(localDateTime);
}
方式二 :
本次获取的时间搓将是毫秒级别故要除以1000
public void millisecondToLocalDateTime(){
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(milliseconds/1000, 0, ZoneOffset.ofHours(8));
// 2020-02-03T13:35:53
System.out.println(localDateTime);
}
方式三 :
本方式精确值是毫秒级别,故得到的结果会存在三位小数点;
public void millisecondToLocalDateTime(){
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
// 将时间戳转为当前时间
LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
// 2020-02-03T13:38:35.799
System.out.println(localDateTime);
}
时间戳与LocalDate互转
学会时间戳与LocalDate互转,同理就可以推出时间戳与LocalTime 互转
方式一 :
毫秒级的时间戳;
public void millisecondToLocalDate(){
//获得时间戳
long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
//将时间戳转为当前时间
LocalDate localDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
// 2020-02-03
System.out.println(localDate);
}
方式二 :
秒级时间戳
public void secondToLocalDate(){
//获得时间戳
long seconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
// 将时间戳转为当前时间
LocalDate localDate = Instant.ofEpochSecond(seconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
// 2020-02-03
System.out.println(localDate);
}
LocalDate 转 时间戳
方式一 :
秒计时间戳
public void localDateToSecond(){
LocalDate localDate = LocalDate.now();
//获得时间戳
long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().getEpochSecond();
// 1580659200
System.out.println(seconds);
}
方式二 :
毫秒级时间戳
public void localDateToSecond(){
LocalDate localDate = LocalDate.now();
//获得时间戳
long seconds = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
// 1580659200000
System.out.println(seconds);
}
LocalDateTime与Date互转
Date转LocalDateTime
方式一 :
得出结果有小数点,毫秒级精确
public void dateToLocalDateTime(){
// 创建时间
Date date = new Date();
// 将时间转为 LocalDateTime
LocalDateTime localDateTime = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime();
// 2020-02-03T14:07:49.833
System.out.println(localDateTime);
}
方式二 :
秒级精确;
public void dateToLocalDateTime(){
// 创建时间
Date date = new Date();
// 将时间转为 秒级时间戳
long second = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toEpochSecond();
LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
// 2020-02-03T14:11:39
System.out.println(localDateTime);
}
LocalDateTime 转 Date
方式一 :
秒级
public void localDateTimeToDate (){
//当前时间
LocalDateTime now = LocalDateTime.now();
// 获得 Instant
Instant instant = Instant.ofEpochSecond(now.toEpochSecond(ZoneOffset.ofHours(8)));
// 获得 Date
Date date = Date.from(instant);
// Mon Feb 03 14:16:27 CST 2020
System.out.println(date);
}
方式二 :
public void localDateTimeToDate (){
//当前时间
LocalDateTime now = LocalDateTime.now();
// 获得 Instant
Instant instant = now.atZone(ZoneOffset.ofHours(8)).toInstant();
// 获得 Date
Date date = Date.from(instant);
// Mon Feb 03 14:20:32 CST 2020
System.out.println(date);
}
LocalDate与Date互转
LocalDate 转 Date
public void localDateToDate(){
//当前时间
LocalDate localDate = LocalDate.now();
// 获得 Instant
Instant instant = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant();
// 获得 Date
Date date = Date.from(instant);
// Mon Feb 03 00:00:00 CST 2020
System.out.println(date);
}
Date 转LocalDate
public void dateToLocalDate(){
// 获得 date
Date date = new Date();
// 获得 LocalDate
LocalDate localDate = date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDate();
// 2020-02-03
System.out.println(localDate);
}
LocalDateTime格式化
LocalDateTime 转字符串
public void localDateTimeToString(){
// 获得 localDateTime
LocalDateTime now = LocalDateTime.now();
// 指定模式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss");
// 将 now 格式化为字符串
String format = now.format(dateTimeFormatter);
// 2020/02/03 14/38/54
System.out.println(format);
}
字符串 转LocalDateTime
public void stringToLocalDateTime(){
String time = "2020/02/03 14/38/54";
// 指定模式
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH/mm/ss");
// 将字符串格式化为 LocalDateTime
LocalDateTime localDateTime = LocalDateTime.parse(time, dateTimeFormatter);
// 2020-02-03T14:38:54
System.out.println(localDateTime);
}
LocalDateTime时间的加减
public void stringToLocalDateTime(){
LocalDateTime now = LocalDateTime.now();
//加年数
now.plusYears(1L);
//加月数
now.plusMonths(1L);
//加周数
now.plusWeeks(1L);
//加天数
now.plusDays(1L);
//加小时数
now.plusHours(1L);
//加分钟数
now.plusMinutes(1L);
//加秒数
now.plusSeconds(1L);
}