什么是时间戳
时间戳是指表示特定时间点的数字值。
它通常是一个整数,表示自某个固定的参考时间点(通常是UNIX纪元起始时间,即1970年1月1日00:00:00 UTC以来经过的秒数或毫秒数)
时间戳有什么用
时间戳在计算机系统中广泛使用,用于记录、比较和计算时间。
通过将时间表示为数字,可以方便地进行时间计算、排序和存储,同时也可以方便地进行跨时区和跨平台的时间交互。
注:在不同编程语言和操作系统中,时间戳的表示方式可能会有所不同。
常用获取时间戳的方法(Java)
①使用System.currentTimeMillis()方法获取当前时间的毫秒数时间戳:
long timestamp = System.currentTimeMillis();
System.out.println("当前时间戳:" + timestamp);
②使用Date类获取当前时间的时间戳:
Date date = new Date();
long timestamp = date.getTime();
System.out.println("当前时间戳:" + timestamp);
③使用Instant类获取当前时间的时间戳(Java 8及以上版本):
Instant instant = Instant.now();
long timestamp = instant.toEpochMilli();
System.out.println("当前时间戳:" + timestamp);
④使用Calendar类获取当前时间的时间戳:
Calendar calendar = Calendar.getInstance();
long timestamp = calendar.getTimeInMillis();
System.out.println("当前时间戳:" + timestamp);