时间戳、String、Date转换

博客介绍了时间戳与Date的相互转换,包括将时间戳转换为Date,Date转时间戳可使用getTime()方法,还提及了string与date的相互转换。此外,介绍了系统微秒和纳秒的获取方法,分别是System.currentTimeMillis()和System.nanoTime()。

时间戳(String or long) =》Date

long s=1557230621043L;
Date date=new Date(s);
System.out.println(date);

Date转时间戳

getTime()

 

string.date相互转换

//string转date
    public static void stringToDate(){
        try{
            String str="2016-10-24 21:59:06";
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(sdf.parse(str));
            System.out.println(sdf.parse(str).getTime());
        }catch(Exception e){
            e.printStackTrace();
        }    
    }
    
    //date转string1
    public static void dateToString1(){
        try {
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            System.out.println(format.format(new Date().getTime()));
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
    
    //date转string2
    public static void dateToString2(){
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            Calendar cal=Calendar.getInstance();
            System.out.println(format.format(cal.getTime()));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

系统微秒

System.currentTimeMillis();

纳秒

System.nanoTime();

转载于:https://www.cnblogs.com/siyyawu/p/10827917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值