時間工具類

package com.sprucetec.tms.utils;

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

/**
 * 時間工具類
 * 
 * Title: TmsTimeUtils.java<br>
 * Description: <br>
 * Copyright: Copyright (c) 2015<br>
 * Company: 北京雲杉世界信息技術有限公司<br>
 * 
 * @author qiang 
 * 2016年3月12日
 */
public class TmsTimeUtils {

    /**
     * 根據傳入的時間字符串,獲得unix對應的時間戳格式
     * 
     * @author 
     * 2016年3月12日
     * @param day
     * @return
     */
    public static Integer getDayUnixTimeStamp(String day) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = df.parse(day);
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            long timestamp = cal.getTimeInMillis();
            return Integer.valueOf((int) (timestamp / 1000));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 獲取當天日期的unix時間戳
     * 
     * @author 
     * 2016年3月12日
     * @return
     */
    public static Integer getTodayUnixTimeStamp() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String day = df.format(date);
        return getDayUnixTimeStamp(day);
    }
    
    /**
     * 獲得當前系統時間戳
     * @author  
     * 2016年3月20日
     * @return
     */
    public static Integer getNowTimeStamp() {
        Integer now = 0;
        Long time = System.currentTimeMillis() / 1000;
        now = time.intValue();
        return now;
    }

    public static void main(String[] args) {
        System.out.println(TmsTimeUtils.getDayUnixTimeStamp("2016-03-12"));
        System.out.println(TmsTimeUtils.getTodayUnixTimeStamp());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章