天天看點

時間戳工具類

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	
	private static SimpleDateFormat sf = null;
	
    /*擷取系統時間 格式為:"yyyy/MM/dd "*/
    public static String getCurrentDate() {
        Date d = new Date();
         sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }
                                      
    /*時間戳轉換成字元竄*/
    public static String getDateToString(long time) {
        Date d = new Date(time);
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        return sf.format(d);
    }

    /*将字元串轉為時間戳*/
    public static long getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date();
        try{
            date = sf.parse(time);
        } catch(ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }
    
    /**
     * 直接擷取時間戳
     * @return
     */
    public static String getTimeStamp() {
    	String currentDate = getCurrentDate();
    	sf = new SimpleDateFormat("yyyy年MM月dd日");
    	Date date = new Date();
    	try{
    		date = sf.parse(currentDate);
    	} catch(ParseException e) {
    		e.printStackTrace();
    	}
    	return String.valueOf(date.getTime());
    }
    
}