这篇文章主要介绍“java long类型存储时间戳的详细介绍”,在日常操作中,相信很多人在java long类型存储时间戳的详细介绍问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”java long类型存储时间戳的详细介绍”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
存储时间打算用时间戳来存储,打算用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();
}
}
输出如下 :
到此,关于“java long类型存储时间戳的详细介绍”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。