Java基础/时间戳与Date相互转化

1、时间戳转化为Date(or String)

  //时间戳转化为StingDate

      SimpleDateFormat format =  newSimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

      Long time=newLong(445555555);

      String d = format.format(time);

      Date date=format.parse(d);

      System.out.println("Format ToString(Date):"+d);

      System.out.println("Format To Date:"+date);

 

  运行结果:

       Format To String(Date):1970-01-0611:45:55

Format To Date:Tue Jan 06 11:45:55CST 1970

 

2、Date(or String)转化为时间戳

  //Date或者String转化为时间戳

      SimpleDateFormat format =  newSimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

      String time="1970-01-0611:45:55";

      Date date = format.parse(time);

      System.out.print("Format To times:"+date.getTime());

 

运行结果:

     Format To times:445555000

你可能感兴趣的:(Java基础/时间戳与Date相互转化)