Java中时间戳和时间的转换
不论是什么编程语言,无论是刚开始学习,还是做了多长时间的猿,都离不开时间戳和时间的转换。Java中也是这样,现在接触到最低的java版本是Java8.其中的时间
- 获取时间戳
//使用系统时间戳 long timestamp = System.currentTimeMillis(); System.out.println("timestamp:" + timestamp); //>>timestamp:1733907943319 Long timestamp1 = Instant.now().toEpochMilli(); System.out.println("timestamp1:"+timestamp1); //>>1733908000856 //获取秒级时间戳 long timestamp2 = System.currentTimeMillis() / 1000; System.out.println("timestamp2:"+timestamp2); //>>1733908113 long timestamp3 = Instant.now().getEpochSecond(); System.out.println("timestamp3:"+timestamp3); //>>1733908113
- 日期时间对象转时间戳
LocalDateTime localDateTime = LocalDateTime.now(); System.out.println("localDateTime:"+localDateTime); //>>localDateTime:2024-12-11T17:10:45.800 //获取我们常见的时间格式 String dateTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); System.out.println("dateTime:"+dateTime); //>>dateTime:2023-04-07 14:06:53 //localDateTime获取时间戳 long timestamp4 = localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli(); System.out.println("timestamp4:"+timestamp4); //>>1733908245800 long timestamp5 = localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); System.out.println("timestamp5:"+timestamp5); //>> 1733908324740 long timestamp6 = ZonedDateTime.now().toInstant().toEpochMilli(); System.out.println("timestamp6:"+timestamp6); //>>1733908374866 long timestamp7 = ZonedDateTime.now().toEpochSecond(); System.out.println("timestamp7:"+timestamp7); //>>1733908374 long timestamp8 = ZonedDateTime.now().toLocalDateTime().toEpochSecond(ZoneOffset.of("+8")); System.out.println("timestamp8:"+timestamp8);
- 时间戳转时间对象
long time = System.currentTimeMillis(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("time:"+time); // 将毫秒时间戳转换为 Instant 对象 Instant instant=Instant.ofEpochMilli(time); System.out.println("instant:"+instant); // 将 Instant 对象转换为 LocalDateTime 对象,使用系统默认时区 LocalDateTime localDateTime=LocalDateTime.ofInstant(instant, ZoneId.systemDefault()); System.out.println("localDateTime:"+localDateTime.format(formatter)); LocalDateTime localDateTime1 =LocalDateTime.ofEpochSecond(instant.getEpochSecond(),0, ZoneOffset.of("+8")); System.out.println("localDateTime1:"+localDateTime1.format(formatter));
-
通过时间字符串获取时间对象
String dt = "2024-12-11"; String dtt = "2024-12-11 17:44:28"; String format_DateTime = "yyyy-MM-dd HH:mm:ss"; DateTimeFormatter df = DateTimeFormatter.ofPattern(format_DateTime); LocalDateTime localDateTime = LocalDateTime.parse(dtt, df); System.out.println("localDateTime:"+localDateTime); //转为Instant Instant instant = localDateTime.toInstant(ZoneOffset.of("+8")); System.out.println("instant:"+instant); System.out.println("毫秒级时间戳:"+instant.toEpochMilli()); System.out.println("秒级时间戳:"+instant.getEpochSecond()); LocalDate localDate = LocalDate.parse(dt); System.out.println("localDate:"+localDate); System.out.println("localDate.atStartOfDay():"+localDate.atStartOfDay().format(df)); //转为Instant Instant instant1 = localDate.atStartOfDay().toInstant(ZoneOffset.of("+8")); System.out.println("毫秒级时间戳:"+instant.toEpochMilli()); System.out.println("秒级时间戳:"+instant.getEpochSecond());
以上就是java8中获取时间戳以及通过实践对象获取时间戳和通过时间戳获取时间对象。
作者:思思文