JAVA—咖啡館

          ——歡迎訪問rogerfan的博客,常來《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗,分享JAVA帶來的快樂!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問題請與我聯(lián)系。

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
            1/**  
            2 * 日期操作助手類  
            3 *   
            4 *  
            5 */
            
            6public final class DateHelper {   
            7  
            8    /**  
            9     * 按照yyyy-MM-dd格式獲取當(dāng)前日期  
           10     *   
           11     * @return  
           12     */
            
           13    public static Date getDate(){   
           14        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");   
           15        try {   
           16            return dateFormat.parse(dateFormat.format(new Date()));   
           17        }
           catch (ParseException e) {   
           18            e.printStackTrace();   
           19            return null;   
           20        }
             
           21    }
             
           22       
           23    /**  
           24     * 按照yyyy-MM-dd HH:mm:ss的格式獲取系統(tǒng)當(dāng)前時間  
           25     *   
           26     * @param format  
           27     * @param date  
           28     */
            
           29    public static Date getTime(){   
           30        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
           31        try {   
           32            return dateFormat.parse(dateFormat.format(new Date()));   
           33        }
           catch (ParseException e) {   
           34            e.printStackTrace();   
           35            return null;   
           36        }
             
           37    }
             
           38       
           39    /**  
           40     * 按照指定格式將字符串轉(zhuǎn)換為日期  
           41     *   
           42     * @param format  
           43     * @param date  
           44     */
            
           45    public static Date getDate(String format, String source){   
           46        SimpleDateFormat dateFormat = new SimpleDateFormat(format);   
           47        try {   
           48            if (StringUtils.isNotEmpty(source)){   
           49                return dateFormat.parse(source);   
           50            }
           else{   
           51                return null;   
           52            }
             
           53        }
           catch (ParseException e) {   
           54            e.printStackTrace();   
           55            return null;   
           56        }
             
           57    }
             
           58       
           59    /**  
           60     * 將yyyy-MM-dd格式的字符串轉(zhuǎn)換為日期類型  
           61     *   
           62     * @param source  
           63     * @return  
           64     */
            
           65    public static Date getDate(String source){   
           66        if (source != null){   
           67            return getDate("yyyy-MM-dd", source);   
           68        }
           else{   
           69            return null;   
           70        }
             
           71    }
             
           72       
           73    /**  
           74     * 將yyyy-MM-dd HH:mm:ss格式的字符串轉(zhuǎn)換為日期類型  
           75     *   
           76     * @param source  
           77     * @return  
           78     */
            
           79    public static Date getTime(String source){   
           80        if (source != null){   
           81            return getDate("yyyy-MM-dd HH:mm:ss", source);   
           82        }
           else{   
           83            return null;   
           84        }
             
           85    }
             
           86       
           87    /**  
           88     * 將日期轉(zhuǎn)換為字符串類型  
           89     *   
           90     * @return  
           91     */
            
           92    public static String date2Char(Date date){   
           93        try{   
           94            if (date != null){   
           95                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
           96                return format.format(date);   
           97            }
             
           98        }
           catch (Exception e){   
           99            e.printStackTrace();   
          100        }
             
          101        return null;   
          102    }
             
          103       
          104    /**  
          105     * 將日期轉(zhuǎn)換為字符串類型的時間  
          106     *   
          107     * @return  
          108     */
            
          109    public static String time2Char(Date date){   
          110        try{   
          111            if (date != null){   
          112                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
          113                return format.format(date);   
          114            }
             
          115        }
           catch (Exception e){   
          116            e.printStackTrace();   
          117        }
             
          118        return null;   
          119    }
             
          120       
          121    /**  
          122     * 獲取兩個日期相差的天數(shù)  
          123     *   
          124     * @param date1 起始日期  
          125     * @param date2 截止日期  
          126     * @return  
          127     */
            
          128    public static Integer getDays(Date start, Date end){   
          129        try{   
          130            if (start == null || end == null){   
          131                return null;   
          132            }
             
          133            SimpleDateFormat f1 = new SimpleDateFormat("yyyy-MM-dd");   
          134            SimpleDateFormat f2 = new SimpleDateFormat("yyyy-MM-dd");   
          135            Date d1 = f1.parse(f1.format(start));   
          136            Date d2 = f2.parse(f2.format(end));   
          137            int days = (int)((d2.getTime() - d1.getTime()) / (60000 * 60 * 24));   
          138            return days;   
          139        }
           catch (Exception e){   
          140            e.printStackTrace();   
          141            throw new RuntimeException("get days is error");   
          142        }
             
          143    }
             
          144       
          145    /**  
          146     * 按照指定的格式返回當(dāng)前日期的字符串表是形式  
          147     *   
          148     * @param format  
          149     * @return  
          150     */
            
          151    public static String getDateForChar(String format){   
          152        try{   
          153            SimpleDateFormat dateFormat = new SimpleDateFormat(format);   
          154            return dateFormat.format(new Date());   
          155        }
           catch (Exception e){   
          156            e.printStackTrace();   
          157            return null;   
          158        }
             
          159    }
             
          160       
          161    /**  
          162     * 按照yyyy-MM-dd格式返回當(dāng)前日期的字符串表示形式  
          163     *   
          164     * @return  
          165     */
            
          166    public static String getDateForChar(){   
          167        return getDateForChar("yyyy-MM-dd");   
          168    }
             
          169       
          170    /**  
          171     * 按照yyyy-MM-dd HH:mm:ss格式返回當(dāng)前時間的字符串表示形式  
          172     *   
          173     * @return  
          174     */
            
          175    public static String getTimeForChar(){   
          176        return getDateForChar("yyyy-MM-dd HH:mm:ss");   
          177    }
             
          178       
          179    /**  
          180     * 為原日期添加指定的天數(shù)并返回添加后的日期,如果天數(shù)為負(fù)數(shù)則在原日期的基礎(chǔ)上減去指定的天數(shù)  
          181     *   
          182     * @param source  
          183     * @param days  
          184     * @return  
          185     */
            
          186    public static Date addDays(Date source, int days){   
          187        try{   
          188            SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");   
          189            format.parse(format.format(source));   
          190            Calendar calendar = format.getCalendar();   
          191            calendar.add(Calendar.DAY_OF_YEAR, days);   
          192            return calendar.getTime();   
          193        }
           catch (Exception e){   
          194            throw new RuntimeException("add days is error.");   
          195        }
             
          196    }
             
          197       
          198    /**  
          199     * 取得當(dāng)前日期的星期數(shù)  
          200     *   
          201     * @return  
          202     */
            
          203    public static String getWeek(){   
          204        Calendar calendar = Calendar.getInstance();   
          205        int week = calendar.get(Calendar.DAY_OF_WEEK);   
          206        switch (week) {   
          207        case Calendar.MONDAY:   
          208            return "星期一";   
          209        case Calendar.TUESDAY:   
          210            return "星期二";   
          211        case Calendar.WEDNESDAY:   
          212            return "星期三";   
          213        case Calendar.THURSDAY:   
          214            return "星期四";   
          215        case Calendar.FRIDAY:   
          216            return "星期五";   
          217        case Calendar.SATURDAY:   
          218            return "星期六";   
          219        case Calendar.SUNDAY:   
          220            return "星期日";   
          221        default:   
          222            return null;   
          223        }
             
          224    }
             
          225       
          226    /**  
          227     * 取得當(dāng)前日期的年  
          228     *   
          229     * @return  
          230     */
            
          231    public static String getYear(){   
          232        Calendar calendar = Calendar.getInstance();   
          233        int year = calendar.get(Calendar.YEAR);   
          234        return String.valueOf(year);   
          235    }
             
          236       
          237    /**  
          238     * 取得當(dāng)前日期的月  
          239     *   
          240     * @return  
          241     */
            
          242    public static String getMonth(){   
          243        Calendar calendar = Calendar.getInstance();   
          244        int moth = calendar.get(Calendar.MONTH);   
          245        return String.valueOf(moth);   
          246    }
             
          247       
          248    /**  
          249     * 取得當(dāng)前日期的月添加指定的天數(shù)并返回添加后的月,如果天數(shù)為負(fù)數(shù)則在原日期的基礎(chǔ)上減去指定的天數(shù)  
          250     * @param days  
          251     * @return  
          252     */
            
          253    public static String getAddDaysMonth(int days){   
          254        Calendar calendar = Calendar.getInstance();   
          255        calendar.add(Calendar.DAY_OF_YEAR, days);   
          256        int moth = calendar.get(Calendar.MONTH);   
          257        return String.valueOf(moth);   
          258    }
             
          259       
          260    /**  
          261     * 取得當(dāng)前日期的月添加指定的天數(shù)并返回添加后的年,如果天數(shù)為負(fù)數(shù)則在原日期的基礎(chǔ)上減去指定的天數(shù)  
          262     * @param days  
          263     * @return  
          264     */
            
          265    public static String getAddDaysYear(int days){   
          266        Calendar calendar = Calendar.getInstance();   
          267        calendar.add(Calendar.DAY_OF_YEAR, days);   
          268        int moth = calendar.get(Calendar.YEAR);   
          269        return String.valueOf(moth);   
          270    }
             
          271       
          272    public static void main(String[] args){   
          273        System.out.println(getYear());   
          274        System.out.println(getMonth());   
          275        System.out.println(getAddDaysMonth(-30));   
          276    }
             
          277  
          278}
           
          279
          posted on 2010-06-21 10:15 rogerfan 閱讀(404) 評論(0)  編輯  收藏 所屬分類: 【Java知識】
          主站蜘蛛池模板: 尼木县| 嘉禾县| 蓝田县| 安达市| 林甸县| 定安县| 吉木萨尔县| 彩票| 上虞市| 大余县| 赤水市| 乃东县| 宝鸡市| 乐亭县| 南涧| 河曲县| 深水埗区| 长兴县| 阿鲁科尔沁旗| 大邑县| 宁海县| 全椒县| 高清| 张掖市| 阳朔县| 潢川县| 泰州市| 石泉县| 随州市| 突泉县| 大足县| 闸北区| 理塘县| 临猗县| 汕头市| 玉门市| 内丘县| 阜新市| 仁化县| 恩平市| 南澳县|