Java

Java - 时间戳、Date、Calendar、SimpleD

2018-01-06  本文已影响29人  533e11a308d9

Java

package com.ikoo.demo;
 
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateUtil {
    public static void main(String[] args) {
        /**
         * 时间戳
         * 1.System.currentTimeMillis()
         * 2.new Date().getTime()
         * 3.Calendar.getInstance().getTimeInMillis()
         * P.S.
         *  Java获取时间戳精确到毫秒级,Unix中精确到秒级,所以Java获取的时间戳需要除以1000,就可以保证一致性
         *  Calendar.getInstance().getTimeInMillis() 耗时最多,这是因为Canlendar要处理时区问题
         */
        System.out.println("System.currentTimeMillis(): " + System.currentTimeMillis());
        System.out.println("new Date().getTime(): " + new Date().getTime());
        System.out.println("Calendar.getInstance().getTimeInMillis(): " + Calendar.getInstance().getTimeInMillis());
        System.out.println("----------");
 
        /**
         * 时间戳(日期)转换自定义日期格式
         */
        long timeStamp = System.currentTimeMillis();
        System.out.println("System.currentTimeMillis(): " + timeStamp);
        Date date = new Date();
        System.out.println("new Date(): " + date);
        long dateTimaStamp = date.getTime();
        System.out.println("date.getTime(): " + date.getTime());
        String formatDate0 = DateUtil.timeStampToFormatDate(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        System.out.println("formatDate0: " + formatDate0);
        String formatDate1 = DateUtil.timeStampToFormatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
        System.out.println("formatDate1: " + formatDate1);
        String formatDate2 = DateUtil.timeStampToFormatDate(new Date().getTime(), "yyyy-MM-dd HH:mm:ss");
        System.out.println("formatDate2: " + formatDate2);
        String formatDate3 = DateUtil.timeStampToFormatDate(Calendar.getInstance().getTime(), "yyyy-MM-dd HH:mm:ss");
        System.out.println("formatDate3: " + formatDate3);
    }
 
    /**
     * 时间戳转换为格式化日期
     *
     * @param dateObj 时间对象
     *                System.currentTimeMillis()
     *                new Date()
     *                new Date().getTime()
     *                Calendar.getInstance().getTimeInMillis()
     * @param pattern 日期格式
     *                yyyy-MM-dd HH:mm:ss
     *                yyyy/MM/dd HH:mm:ss
     *                yyyy年MM月dd日 HH:mm:ss
     * @return sdf.format(Object dateObj)
     */
    public static String timeStampToFormatDate(Object dateObj, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        String formatDate = sdf.format(dateObj);
        return formatDate;
    }
}

 
控制台打印结果:

System.currentTimeMillis(): 1503660334272
new Date().getTime(): 1503660334274
Calendar.getInstance().getTimeInMillis(): 1503660334299
----------
System.currentTimeMillis(): 1503660334328
new Date(): Fri Aug 25 19:25:34 CST 2017
date.getTime(): 1503660334328
formatDate0: 2017-08-25 19:25:34
formatDate1: 2017-08-25 19:25:34
formatDate2: 2017-08-25 19:25:34
formatDate3: 2017-08-25 19:25:34
 
Process finished with exit code 0
上一篇下一篇

猜你喜欢

热点阅读