关于java:java-Date时间戳TimeStampString-互相转换

47次阅读

共计 2249 个字符,预计需要花费 6 分钟才能阅读完成。

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 * 工夫转化工具 date 转为工夫戳 工夫戳转 date 相互与 String 的转换
 * 所有呈现的 String time 格局都必须为 (yyyy-MM-dd HH:mm:ss),否则出错
 */
public class TimeExchange {
 
    /**
     * String(yyyy-MM-dd HH:mm:ss) 转 Date
     * 
     * @param time
     * @return
     * @throws ParseException
     */
    // String date = "2010/05/04 12:34:23";
    public static Date StringToDate(String time) throws ParseException {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;
    }
 
    /**
     * Date 转为 String(yyyy-MM-dd HH:mm:ss)
     * 
     * @param time
     * @return
     */
    public static String DateToString(Date time) {
        String dateStr = "";
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");
        try {dateStr = dateFormat.format(time);
            System.out.println(dateStr);
        } catch (Exception e) {e.printStackTrace();
        }
        return dateStr;
    }
    /**
     * String(yyyy-MM-dd HH:mm:ss) 转 10 位工夫戳
     * @param time
     * @return
     */
    public static Integer StringToTimestamp(String time){
   
        int times = 0;
        try {times = (int) ((Timestamp.valueOf(time).getTime())/1000);  
        } catch (Exception e) {e.printStackTrace();  
        }
        if(times==0){System.out.println("String 转 10 位工夫戳失败");
        }
        return times; 
        
    }
    /**
     * 10 位 int 型的工夫戳转换为 String(yyyy-MM-dd HH:mm:ss)
     * @param time
     * @return
     */
    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;  
    }
    /**
     * 10 位工夫戳转 Date
     * @param time
     * @return
     */
    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 位工夫戳
     * @param time
     * @return
     */
    public static Integer DateToTimestamp(Date time){Timestamp ts = new Timestamp(time.getTime());
        
        return (int) ((ts.getTime())/1000);
    }
    public static void main(String [] args){System.out.println(StringToTimestamp("2011-05-09 11:49:45"));
        System.out.println(DateToTimestamp(TimestampToDate(StringToTimestamp("2011-05-09 11:49:45"))));
    }

}

正文完
 0