<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JAVA—咖啡館

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

    BlogJava 首頁 新隨筆 聯系 聚合 管理
      447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
      1/**  
      2 * 日期操作助手類  
      3 *   
      4 *  
      5 */
      
      6public final class DateHelper {   
      7  
      8    /**  
      9     * 按照yyyy-MM-dd格式獲取當前日期  
     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的格式獲取系統當前時間  
     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     * 按照指定格式將字符串轉換為日期  
     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格式的字符串轉換為日期類型  
     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格式的字符串轉換為日期類型  
     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     * 將日期轉換為字符串類型  
     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     * 將日期轉換為字符串類型的時間  
    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     * 獲取兩個日期相差的天數  
    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     * 按照指定的格式返回當前日期的字符串表是形式  
    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格式返回當前日期的字符串表示形式  
    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格式返回當前時間的字符串表示形式  
    172     *   
    173     * @return  
    174     */
      
    175    public static String getTimeForChar(){   
    176        return getDateForChar("yyyy-MM-dd HH:mm:ss");   
    177    }
       
    178       
    179    /**  
    180     * 為原日期添加指定的天數并返回添加后的日期,如果天數為負數則在原日期的基礎上減去指定的天數  
    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     * 取得當前日期的星期數  
    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     * 取得當前日期的年  
    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     * 取得當前日期的月  
    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     * 取得當前日期的月添加指定的天數并返回添加后的月,如果天數為負數則在原日期的基礎上減去指定的天數  
    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     * 取得當前日期的月添加指定的天數并返回添加后的年,如果天數為負數則在原日期的基礎上減去指定的天數  
    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 閱讀(397) 評論(0)  編輯  收藏 所屬分類: 【Java知識】
    主站蜘蛛池模板: 一级日本高清视频免费观看| 亚洲人精品亚洲人成在线| 亚洲国产精品无码久久青草 | 亚洲成熟xxxxx电影| 亚洲国产另类久久久精品小说 | 在线免费中文字幕| 99re6在线视频精品免费下载| 久操免费在线观看| 四虎国产精品永久免费网址 | 一级做a爰片久久毛片免费陪 | 国产成人亚洲综合一区| 亚洲中文字幕久久久一区| 亚洲人成小说网站色| 亚洲精品无码成人片久久不卡| 亚洲国产欧美国产综合一区| 蜜臀亚洲AV无码精品国产午夜.| 国产成人精品亚洲一区| 有色视频在线观看免费高清在线直播| 亚洲日韩在线观看免费视频| 香蕉免费看一区二区三区| 男人j进入女人j内部免费网站 | eeuss影院www天堂免费| 你懂的免费在线观看| 99视频在线免费看| 黄页免费的网站勿入免费直接进入| 欧美a级成人网站免费| 精品免费久久久久久成人影院| 四虎影视免费永久在线观看| 亚洲欧洲自拍拍偷精品 美利坚 | 亚洲免费网站观看视频| 国产无遮挡又黄又爽免费视频| 亚洲国产精品成人网址天堂| 国产aⅴ无码专区亚洲av| 亚洲最新中文字幕| 亚洲国产精品18久久久久久| 永久免费精品影视网站| 久久伊人免费视频| 成人人免费夜夜视频观看| 亚洲综合精品网站| 亚洲熟妇av一区二区三区下载| 亚洲国产成人精品无码区二本 |