天天看點

時間戳util,擷取目前日期、目前時間

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author 
 */

public class TimesUtil {

    public static final Logger LOGGER = LoggerFactory.getLogger(TimesUtil.class);

    private TimesUtil(){}

    /**
     * 擷取目前日期時間戳,不包含時間
     *
     * @return
     */
    public static long getCurrentDateTimesTamp() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String currentTime = simpleDateFormat.format(new Date());
        long currentTimesTamp = 0;
        try {
            Date currentDate = simpleDateFormat.parse(currentTime);
            currentTimesTamp = currentDate.getTime();
        } catch (ParseException e) {
            LOGGER.error(e.getMessage(),e);
        }
        return currentTimesTamp;
    }


    /**
     * 擷取目前時間,時間戳
     *
     * @return
     */
    public static long getCurrentTimesTamp() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = simpleDateFormat.format(new Date());
        long currentTimesTamp = 0;
        try {
            Date currentDate = simpleDateFormat.parse(currentTime);
            currentTimesTamp = currentDate.getTime();
        } catch (ParseException e) {
            LOGGER.error("時間戳轉換異常:" + e.toString(), e);
        }
        return currentTimesTamp;
    }
}