java 时间戳 timestamp


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

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值