Java时间戳转换

时间戳转换:
一、十位时间戳和时间互转

1.十位 时间戳和Stirng互转

/*
     * String(yyyy-MM-dd HH:mm:ss)转10位时间戳
     * 2011-05-09 11:49:45   结果:1304912985
     */
    public static Integer StringToTimestamp(String time){
        int times = 0;
        try {  
            times = (int) ((Timestamp.valueOf(time).getTime())/1000);  
            System.out.println("times:"+times);
        } catch (Exception e) {  
            e.printStackTrace();  
        }
        if(times==0){
            System.out.println("String转10位时间戳失败");
        }
        return times; 

    }
    /*
     * 10位int型的时间戳转换为String(yyyy-MM-dd HH:mm:ss)
     * 1304912985 结果:2011-05-09 11:49:45
     */
    public static String timestampToString(Integer time){
        //int转long时,先进行转型再进行计算,否则会是计算结束后在转型
        long temp = (long)time*1000;
        Timestamp ts = new Timestamp(temp);  
        String tsStr = "";  
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        try {  
            tsStr = dateFormat.format(ts);  
            System.out.println(tsStr);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }
        return tsStr;  
    }

2.十位 时间戳和date互转

/*
     * 10位时间戳转Date
     * 1304912985    结果:2011-05-09 11:49:45.0
     */
    public static Date TimestampToDate(Integer time){
        long temp = (long)time*1000;
        Timestamp ts = new Timestamp(temp);  
        Date date = new Date();  
        try {  
            date = ts;  
            System.out.println(date);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return date;
    }

    /*
     * Date类型转换为10位时间戳
     * new Date()  结果:1493286148
     */
    public static Integer DateToTimestamp(Date time){
        Timestamp ts = new Timestamp(time.getTime());
        Integer timestamp = (int) ((ts.getTime())/1000);
        System.out.println("timestamp:"+timestamp);
        return timestamp;
    }

二、十三位时间戳和时间互转 String类型 / 时间戳

/*
     * 时间戳转换为时间  13位
     * 1493175590107  结果:2017-04-26 10:59:50.107
     */
      public static String stampToDate(String s){
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            long lt = new Long(s);
            Date date = new Date(lt);
            res = simpleDateFormat.format(date);
            System.out.println(res);
            return res;
        }


        /* 
         * 将时间转换为时间戳 13位
         * 2017-04-26 10:59:50.107   1493175590107
         */    
        public static String dateToStamp(String s) throws Exception{
            String res;
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
            System.out.println(res);
            return res;
        }

三、Stirng格式 转date
1、String(yyyy-MM-dd HH:mm:ss) 转 Date Tue May 04 12:34:23 CST 2010

    /*
     * String(yyyy-MM-dd HH:mm:ss) 转 Date
     * String date = "2010-05-04 12:34:23"; 结果:Tue May 04 12:34:23 CST 2010
     */ 
    public static Date StringToDate(String time) throws Exception {
        Date date = new Date();
        // 注意format的格式要与日期String的格式相匹配
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(time);
            System.out.println(date.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

你可能感兴趣的:(java基础-时间戳,Java-时间戳处理)