java Date 處理常用方法

    public static final String SDF1 = "yyyy-MM-dd HH:mm:ss";
    public static final String SDF2 = "yyyy-MM-dd";
    public static final String SDF3 = "yyyyMMdd";
    public static final String SDF4 = "yyyy-MM";
    public static final String SDF5 = "yyyy";
    public static final String SDF6 = "yyMM";

/**
     * 獲取時間增加n天后的時間,s 時間格式
     * @param date 
     *             時間
     * @param s 
     *             時間格式
     * @param n 
     *             數量
     * @return
     */
    public static String getDateAddDayOfter(String date,String s,int n){
        try{
            Date date2 =getDate(s,date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date2);
            calendar.add(Calendar.MINUTE, n*24*60);
            SimpleDateFormat sdf=new SimpleDateFormat(s);
            String dateStr=sdf.format(calendar.getTimeInMillis());
            return dateStr;
            
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

/**
     * 根據特定格式獲取當前時間+n天后的時間
     * @param s 
     *         數據返回格式
     * @param n 
     *         天數
     * @return
     */
    public static Date getDateAddDayOfter(String s,int n){
        try{
            Calendar now=Calendar.getInstance();
            now.add(Calendar.MINUTE,n*24*60);
            SimpleDateFormat sdf=new SimpleDateFormat(s);
            String dateStr=sdf.format(now.getTimeInMillis());
            Date currentTime_2 = sdf.parse(dateStr);
            return currentTime_2;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

/**
     * 根據特定格式獲取當前時間+n個小時後的時間
     * @param s 數據返回格式
     * @param n 小時數
     * @return
     */
    public static Date getDateAddHourAfter(String s,int n){
        try{
            Calendar now=Calendar.getInstance();
            now.add(Calendar.MINUTE,n*60);
            SimpleDateFormat sdf=new SimpleDateFormat(s);
            String dateStr=sdf.format(now.getTimeInMillis());
            Date currentTime_2 = sdf.parse(dateStr);
            return currentTime_2;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

 

/**
     * 根據特定格式獲取當前時間date
     * @throws ParseException 
     */
    public static Date getNowDate(String s){
        try{
             Date currentTime = new Date();
             SimpleDateFormat df = new SimpleDateFormat(s);//設置日期格式
             String dateString = df.format(currentTime);
             Date currentTime_2 = df.parse(dateString);
             return currentTime_2;
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
         
    }

 

/**
     * 獲取時間字符串形式
     * @param s
     *         格式
     * @param date
     *             時間
     * @return
     */
    public static String getDate(String s,Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(s);
        return sdf.format(date);
    }

/**
     * 獲取字符串時間的Date
     * @param s
     *             格式
     * @param date
     *             時間
     * @return
     */
    public static Date getDate(String s,String date){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(s);
            return sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 獲取到秒的時間戳
     */
    public static String getSecondTimestamp(){
        return getTimeMillByBit(10);
    }
    /**
     * 根據特定格式獲取當前時間字符串
     */
    public static String getNowDateString(String s ){
        SimpleDateFormat df = new SimpleDateFormat(s);//設置日期格式
        return df.format(new Date());// new Date()爲獲取當前系統時間,也可使用當前時間戳
    }
    /**
     * 獲取特定位數的時間戳
     */
    public static String getTimeMillByBit(int bit){
        if(bit>=13){
            return getTimeMillis();
        }else{
            String timeM = getTimeMillis();
            return timeM.substring(timeM.length()-bit, timeM.length());
        }
    }

/**
     * 獲取當前時間時間戳
     * @return
     */
    public static  String getTimeMillis(){
        return String.valueOf(new Date().getTime());
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章