1、获得时间戳
为了统一其他语言的如php和unix系统获取的时间戳是10位长度的,精确到秒。
java时间戳长度是13位,精确到毫秒 我们获取时间戳需要相应处理。
//获取当前时间戳,除以1000,获取到长度为10位的,精确到秒 public static long now() { return System.currentTimeMillis() / 1000; }
2、时间戳转化为固定模式的日期yyyy-MM-dd HH:mm:ss
两种时间戳转换为日期的方法,发现testTime方法比较快。
testTime2将long转换为字符串加0后,再转为long,需要时间比较久。
public static String testTime(String pattern, long timestamp) { SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.SIMPLIFIED_CHINESE); return format.format(timestamp * 1000); } public static String testTime2(String pattern, long timestamp) { SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.SIMPLIFIED_CHINESE); return format.format(Long.parseLong(String.valueOf(timestamp) + "000")); }
3日期转换为时间戳
先将日期字符串转为Date对象,再用Date.getTime()获取时间戳
public static String datePattern = "yyyy-MM-dd"; public static String timeStampPattern = "yyyy-MM-dd HH:mm:ss"; public static Date toDate(String dateStr) { if (StrKit.isBlank(dateStr)) { return null; } dateStr = dateStr.trim(); int length = dateStr.length(); try { if (length == timeStampPattern.length()) { SimpleDateFormat sdf = new SimpleDateFormat(timeStampPattern); try { return sdf.parse(dateStr); } catch (ParseException e) { dateStr = dateStr.replace(".", "-"); dateStr = dateStr.replace("/", "-"); return sdf.parse(dateStr); } } else if (length == datePattern.length()) { SimpleDateFormat sdfDate = new SimpleDateFormat(datePattern); try { return sdfDate.parse(dateStr); } catch (ParseException e) { dateStr = dateStr.replace(".", "-"); dateStr = dateStr.replace("/", "-"); return sdfDate.parse(dateStr); } } else { throw new IllegalArgumentException("The date format is not supported for the time being"); } } catch (ParseException e) { throw new IllegalArgumentException("The date format is not supported for the time being"); } }
测试代码如下
Date obtain = toDate("2017-12-10"); long ss = obtain.getTime(); String dateStr = timestamp2DateTime("yyyy-MM-dd HH:mm:ss", ss); System.out.println(dateStr); 结果2017-12-10 00:00:00
下面是废话
前端用js的Date对象的Date.getTime()方法获取到的时间戳是13位长度的,精确到毫秒,一般需要进行转换成精确度到秒。即除以1000
#时间戳转日期
一般时间戳都是精确到毫秒
默认的时间戳转日期的方法。
这个方法是调用的SimpleDateFormat只有一个参数的构造方法获得格式转换对象
这个方法会让我们对时间格式时区掌控,当系统所在时区发生变化时,日期格式也会发生变化
public static String timestamp2DateTime(String pattern, long timestamp) { SimpleDateFormat format = new SimpleDateFormat(pattern); return format.format(timestamp); }
上面的SimpleDateFormat(pattern)
默认会指定当前运行环境所在时区为构造方法,这种不确定性的默认应该需要避免
public SimpleDateFormat(String pattern) { this(pattern, Locale.getDefault(Locale.Category.FORMAT)); }
我们自己指定需要转换的时区
获取到Locale,即指定国家的时间,这里是获得简体中文,北京时间也就是东八区的locale对象
Locale simpleChinese = new Locale("zh","CN");
Locale.SIMPLIFIED_CHINESE
/** * 时间戳转化为北京时间日期格式 * @param pattern * 如:yyyy-MM-dd HH:mm:ss 获取时间格式为: 年-月-日 时:分:秒<br> * pattern模式匹配语法: G 年代标志符<br> y 年<br> M 月<br> d 日<br> h 时 在上午或下午 (1~12)<br> H 时 在一天中 (0~23)<br> m 分<br> s 秒<br> S 毫秒<br> E 星期<br> D 一年中的第几天<br> F 一月中第几个星期几<br> w 一年中第几个星期<br> W 一月中第几个星期<br> a 上午 / 下午 标记符<br> k 时 在一天中 (1~24)<br> K 时 在上午或下午 (0~11)<br> z 时区<br> * @param timestamp * 时间戳 * @return */ public static String timestamp2DateTime(String pattern, long timestamp) { //使用简体中文所在的时区 SimpleDateFormat format = new SimpleDateFormat(pattern,Locale.SIMPLIFIED_CHINESE); return format.format(timestamp); }
时间更精确的值
1秒 = 1 X 10E15(次方)飞秒 = 1 X 10E12 皮秒 = 1 X 10E9 纳秒 = 1 X 10E6 微妙 = 1 X 10E3 毫秒
1秒 = 1000000000000000飞秒 = 1000000000000皮秒 = 1000000000纳秒 = 1000000微秒 = 1000毫秒
1秒 = 1000000000000皮秒 = 1000000000纳秒 = 1000000微秒 = 1000毫秒
1秒 = 1000000000纳秒 = 1000000微秒 = 1000毫秒
1秒 = 1000000微秒 = 1000毫秒
1秒 = 1000毫秒
#纳秒
1秒等于10的9次方纳秒
这个纳秒只能用来精确统计程序运行的时间,比如计算一个程序运行的时间长短,需要一定的准确度。用currentTimeMillis()只能精确到毫秒
System.currentTimeMillis()起始时间是基于1970.1 10:00:00这个
前段时间项目中需要 统计接口连接时间,考虑到连接时间一般都是零点几毫秒级别的,为了拿到更精确地数值,没有使用System.currentTimeMillis(),而是贸然地使用System.nanoTime()来统计时间,后来分析服务器上的数据,发现 竟然有10-15%的数据数值竟然超过了 10的13次方。 原因: System.currentTimeMillis() 起始时间是基于 1970.1.1 0:00:00 这个确定的时间的,而System.nanoTime()是基于cpu核心的时钟周期来计时,它的开始时间是不确定的。(有篇文章说是根据cpu核心的启动时间开始计算的) 但是在多核处理器上,由于每个核心的开始时间不确定,但是在多核处理器上, long start = System.nanoTime();String ip = Utilities.getIpByUrl(url);long cost = System.nanoTime() - start; 这段代码有可能会运行在两个不同的cpu核心上,从而导致得到的结果完全不符逻辑。
这是测试纳秒的时间间隔的测试方法
60万次数据进行测试,600万数据太大,文本文件打不开,看来要重新安装nodepad++才好。
File tmpFile = new File("D:/testNanoTime.txt"); FileWriter write = null; try { write = new FileWriter(tmpFile); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } int size = 600000; long[] timeStamp = new long[size]; for (int i = 0; i < size; i++) { timeStamp[i] = System.nanoTime(); } long increase = 0l; for (int i = 0; i < size - 1; i++) { increase = timeStamp[i + 1] - timeStamp[i]; if (increase > 0) { // System.out.println("第" + i+1 + "个:"+timeStamp[i + 1]+"第" + i + "个:"+timeStamp[i]+"差值"+increase); try { write.write("第" + i+1 + "个:"+timeStamp[i + 1]+"第" + i + "个:"+timeStamp[i]+"差值"+increase+"\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } increase = 0; } try { write.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("操作完成");