热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java时间戳处理技巧与最佳实践

java时间戳timestamp******************相关类TimestamppublicclassTimestampextendsjava.util.Date{


java 时间戳 timestamp

 


******************

相关类

 

Timestamp

public class Timestamp extends java.util.Date {********************************
构造函数public Timestamp(long time)*********************************
常用方法public void setTime(long time)//设置自1970开始的时间毫秒数
public long getTime()//返回自1970开始的毫秒数public static Timestamp valueOf(String s) //将字符串转换为时间戳,字符串格式yyyy-[m]m-[d]d hh:mm:ss[.f...]public static Timestamp from(Instant instant)//从Instant中提取时间戳
public Instant toInstant()//将时间戳转换为Instantpublic void setTime(long time)//设置自1970开始的毫秒数
public long getTime()//返回自1970开始的毫秒数public void setNanos(int n)//设置纳秒数
public int getNanos()//获得纳秒数public boolean before(Timestamp ts)
public boolean after(Timestamp ts)
public boolean equals(java.lang.Object ts)//比较先后顺序,是否相等

 


 


******************

示例

 

public class TimeTest {public static void main(String[] args){long l=System.currentTimeMillis();Timestamp timestamp=new Timestamp(l);System.out.println(l);System.out.println(timestamp);System.out.println(timestamp.getNanos());System.out.println(timestamp.getTime());System.out.println(timestamp.toInstant());System.out.println();System.out.println("*********************************");Instant instant=timestamp.toInstant();System.out.println(instant);Timestamp t1=Timestamp.from(instant);System.out.println(t1);System.out.println();System.out.println("**********************************");Instant instant2=Instant.now();System.out.println(instant2);Timestamp timestamp2=Timestamp.from(instant2);System.out.println(timestamp2);}
}

 

**********************

控制台输出

 

1568768671589
2019-09-18 09:04:31.589
589000000
1568768671589
2019-09-18T01:04:31.589Z*********************************
2019-09-18T01:04:31.589Z
2019-09-18 09:04:31.589**********************************
2019-09-18T01:04:31.611454900Z
2019-09-18 09:04:31.6114549

 

 


推荐阅读
author-avatar
盛夏的长安佳人
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有