java时间与时间戳

(一)时间:当前时间java

1.获取当前时间orm

        Date date = new Date();

        System.out.println(date); //Thu Dec 14 18:08:53 CST 2017

  

2.格式化输出blog

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    String date2 = df.format(new Date());// new Date()为获取当前系统时间,也可以使用当前时间戳

    System.out.println(date2); //2017-12-14 18:15:32

 

(二)时间戳:时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至如今的总秒数。get

1.获取时间戳form

long timeStamp = System.currentTimeMillis(); // 1513247664639

long timeStamp2 = Calendar.getInstance().getTimeInMillis();//1513247664639

long timeStamp3 = new Date().getTime();//1513247664639

 

(三)时间与时间戳的转换class

1.利用Date的构造方法转换—时间戳到时间的转换date

Date date3 = new Date(timeStamp3);   

  

2.利用Date的getTime()方法—时间到时间戳的转换方法

long ts = date4.getTime();

  

(四)格式化im

   将时间(Date类)或者时间戳(long类)转化成必定格式的时间(String类)时间戳

SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
String date5 = df.format(new Date());// new Date()为获取当前系统时间,也可以使用当前时间戳
String date6 = df.format(new Date().getTime());// new Date()为获取当前系统时间,也可以使用当前时间戳
相关文章
相关标签/搜索