java long 存储时间戳 深入理解java long 存储时间戳

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java long 存储时间戳 深入理解java long 存储时间戳

鲸冬香   2021-03-30 我要评论

存储时间打算用时间戳来存储,打算用long类型来代表时间戳,但是在用long类型存储时间戳的时候出了点问提。

在写单元测试的时候,用一个long类型来存储时间戳,发现编译器报错了

刚开始猜想可能是因为long不够大,存储不了。然后用double类型来存:

发现还是报错了,仔细想想不对,double存储的数量应该时很大的,不会连时间戳都存储不了。
在后面加上小数点之后,居然可以存了:

加了小数点之后就能存了,仔细一想,之前没加小数点的时候他是整数,加了小数点之后变成了浮点数,猜测之前没加上小数点的时间戳是一个整形的字面值,加上小数点后是一个浮点数的字面值,之前的报错应该不是存储类型的问题,而是字面值范围超出了。

用字符串来测试:

把时间戳变成字符串的字面值,在将他解析成long类型的和int类型的,然后在把他们输出:

发现第一个long类型的成功输出了,而int类型的却报了一个number的错误.

这说明long是可以存储时间戳的,而int存储不了时间戳,所以判断之前不能存储时因为字面值为整形超出了范围。

Java 各种日期/时间 对象转Long时间戳

package cn.xbz;
 
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
 
/**
 * @title 各种日期/时间对象转时间戳
 * @author Xingbz
 * @createDate 2018-5-18
 */
public class DateTime2MillisDemo {
 
 private static final String FORMAT_STR = "yyyy-MM-dd HH:mm:ss";
 
 public static void main(String[] args) {
  System.out.println("====== JDK7及之前 ======");
  Long l1 = getMillis1();
  Long l2 = date2Millis(new Date());
  Long l3 = calendar2Millis(Calendar.getInstance());
  Long l4 = string2Millis(new SimpleDateFormat(FORMAT_STR).format(new Date()) , FORMAT_STR);//为了与以上几个保持一致
  System.out.println(l1 + "\n" + l2 + "\n" + l3 + "\n" + l4);//会有几毫秒的差别
 
  System.out.println("====== JDK8 ======");
  Long l5 = getMillis2();
  Long l6 = localDateTime2Millis(LocalDateTime.now());
  Long l7 = localDate2Millis(LocalDate.now());
  Long l8 = clock2Millis(Clock.systemUTC());
  Long l9 = zoneDateTime2Millis(ZonedDateTime.now());
  Long l10 = string2MillisWithJDK8(LocalDateTime.now().format(DateTimeFormatter.ofPattern(FORMAT_STR)) , FORMAT_STR);//为了与以上几个保持一致
  System.out.println(l5 + "\n" + l6 + "\n" + l7 + "\n" + l8 + "\n" + l9 + "\n" + l10);//会有几毫秒的差别
 }
 
 /* JDK7及之前 */
 
 /** 获取时间戳 */
 public static Long getMillis1() {
  return System.currentTimeMillis();
 }
 
 /** Date转时间戳 */
 public static Long date2Millis(Date date) {
  return date.getTime();
 }
 
 /** Calendar转时间戳 */
 public static Long calendar2Millis(Calendar calendar) {
  return calendar.getTime().getTime();
 }
 
 /** 日期字符串转时间戳 */
 public static Long string2Millis(String dateStr, String formatStr) {
  try {
   SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
   return simpleDateFormat.parse(dateStr).getTime();
  } catch (Exception e) {
   return 0L;
  }
 }
 
 /* JDK8 */
 
 /** 获取时间戳 */
 public static Long getMillis2() {
  return Instant.now().toEpochMilli();
 }
 
 /** LocalDateTime转时间戳 */
 public static Long localDateTime2Millis(LocalDateTime localDateTime) {
  return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
 }
 
 /** LocalDate转时间戳 */
 public static Long localDate2Millis(LocalDate localDate) {
  return LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
 }
 
 /** Clock转时间戳 */
 public static Long clock2Millis(Clock clock) {
  return clock.millis();
 }
 
 /** ZoneDateTIme转时间戳(这个不常用吧~) */
 public static Long zoneDateTime2Millis(ZonedDateTime zonedDateTime) {
  return zonedDateTime.toLocalDateTime().toInstant(ZoneOffset.ofHours(8)).toEpochMilli();//!!!好费劲
 }
 
 /** String转时间戳(JDK8) */
 public static Long string2MillisWithJDK8(String dateStr , String formatStr) {
  return LocalDateTime.parse(dateStr , DateTimeFormatter.ofPattern(formatStr)).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
 }
}
 

输出如下 :

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们