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()
//将时间戳转换为Instant
public 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