[转帖]  java日期处理工具类_Android, Python及开发编程讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Android, Python及开发编程讨论区 »
总帖数
3
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 11920 | 回复: 2   主题: [转帖]  java日期处理工具类        下一篇 
    本主题由 522080330 于 2013-1-23 16:01:44 置为精华
522080330
注册用户
等级:少校
经验:1478
发帖:79
精华:20
注册:2013-1-18
状态:离线
发送短消息息给522080330 加好友    发送短消息息给522080330 发消息
发表于: IP:您无权察看 2013-1-23 14:56:50 | [全部帖] [楼主帖] 楼主

1 package util;
2
3
4 import java.text.ParseException;
5 import java.text.SimpleDateFormat;
6 import java.util.Calendar;
7 import java.util.Date;
8
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 /**
14 * 日期处理工具类
15 * @author 程欢
16 * 2013-1-11 04:33:50
17 */
18 public class DateUtil {
       19
       20 private static final transient Log log = LogFactory.getLog(DateUtil.class);
       21
       22 /**
       23 * 缺省的日期格式
       24 */
       25 private static final String DAFAULT_DATE_FORMAT = "yyyy-M-d";
       26
       27 private static final String DATE_FORMAT = "yyyy-MM-dd";
       28
       29 /**
       30 * 默认日期类型格试.
       31 *
       32 * @see DAFAULT_DATE_FORMAT
       33 */
       34 private static SimpleDateFormat dateFormat = new SimpleDateFormat(DAFAULT_DATE_FORMAT);
       35
       36 /**
       37 * 缺省的日期时间格式
       38 */
       39 private static final String DAFAULT_DATETIME_FORMAT = "yyyy-M-d HH:mm:ss";
       40
       41 /**
       42 * 时间格式
       43 */
       44 private static String DATETIME_FORMAT = DAFAULT_DATETIME_FORMAT;
       45
       46 private static SimpleDateFormat datetimeFormat = new SimpleDateFormat(DATETIME_FORMAT);
       47
       48 /**
       49 * 缺省的时间格式
       50 */
       51 private static final String DAFAULT_TIME_FORMAT = "HH:mm:ss";
       52
       53 /**
       54 * 时间格式
       55 */
       56 private static String TIME_FORMAT = DAFAULT_TIME_FORMAT;
       57
       58 private static SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
       59
       60 private DateUtil()
       61 {
             62 // 私用构造主法.因为此类是工具类.
       63 }
       64
       65 /**
       66 * 获取格式化实例.
       67 *
       68 * @param pattern
       69 * 如果为空使用DAFAULT_DATE_FORMAT
       70 * @return
       71 */
       72 public static SimpleDateFormat getFormatInstance(String pattern) {
             73 if (pattern == null || pattern.length() == 0)
             74 {
                   75 pattern = DAFAULT_DATE_FORMAT;
             76 }
             77 return new SimpleDateFormat(pattern);
       78 }
       79
       80 /**
       81 * 格式化Calendar
       82 *
       83 * @param calendar
       84 * @return
       85 */
       86 public static String formatCalendar(Calendar calendar) {
             87 if (calendar == null)
             88 {
                   89 return "";
             90 }
             91 return dateFormat.format(calendar.getTime());
       92 }
       93
       94 public static String formatDateTime(Date d) {
             95 if (d == null)
             96 {
                   97 return "";
             98 }
             99 return datetimeFormat.format(d);
      100 }
      101
      102 public static String formatDate(Date d) {
            103 if (d == null)
            104 {
                  105 return "";
            106 }
            107 return dateFormat.format(d);
      108 }
      109
      110 /**
      111 * 格式化时间
      112 *
      113 * @param calendar
      114 * @return
      115 */
      116 public static String formatTime(Date d) {
            117 if (d == null)
            118 {
                  119 return "";
            120 }
            121 return timeFormat.format(d);
      122 }
      123
      124 /**
      125 * 格式化整数型日期
      126 *
      127 * @param intDate
      128 * @return
      129 */
      130 public static String formatIntDate(Integer intDate) {
            131 if (intDate == null)
            132 {
                  133 return "";
            134 }
            135 Calendar c = newCalendar(intDate);
            136 return formatCalendar(c);
      137 }
      138
      139 /**
      140 * 根据指定格式化来格式日期.
      141 *
      142 * @param date
      143 * 待格式化的日期.
      144 * @param pattern
      145 * 格式化样式或分格,如yyMMddHHmmss
      146 * @return 字符串型日期.
      147 */
      148 public static String formatDate(Date date, String pattern) {
            149 if (date == null)
            150 {
                  151 return "";
            152 }
            153 if (StringUtils.isBlank(pattern))
            154 {
                  155 return formatDate(date);
            156 }
            157 SimpleDateFormat simpleDateFormat = null;
            158 try
            159 {
                  160 simpleDateFormat = new SimpleDateFormat(pattern);
            161 } catch (Exception e)
            162 {
                  163 e.printStackTrace();
                  164 return formatDate(date);
            165 }
            166 return simpleDateFormat.format(date);
      167 }
      168
      169 /**
      170 * 取得Integer型的当前日期
      171 *
      172 * @return
      173 */
      174 public static Integer getIntNow() {
            175 return getIntDate(getNow());
      176 }
      177
      178 /**
      179 * 取得Integer型的当前日期
      180 *
      181 * @return
      182 */
      183 public static Integer getIntToday() {
            184 return getIntDate(getNow());
      185 }
      186
      187 /**
      188 * 取得Integer型的当前年份
      189 *
      190 * @return
      191 */
      192 public static Integer getIntYearNow() {
            193 Calendar c = Calendar.getInstance();
            194 int year = c.get(Calendar.YEAR);
            195 return year;
      196 }
      197
      198 /**
      199 * 取得Integer型的当前月份
      200 *
      201 * @return
      202 */
      203 public static Integer getIntMonthNow() {
            204 Calendar c = Calendar.getInstance();
            205 int month = c.get(Calendar.MONTH) + 1;
            206 return month;
      207 }
      208
      209 public static String getStringToday() {
            210 return getIntDate(getNow()) + "";
      211 }
      212
      213 /**
      214 * 根据年月日获取整型日期
      215 *
      216 * @param year
      217 * @param month
      218 * @param day
      219 * @return
      220 */
      221 public static Integer getIntDate(int year, int month, int day) {
            222 return getIntDate(newCalendar(year, month, day));
      223 }
      224
      225 /**
      226 * 某年月的第一天
      227 *
      228 * @param year
      229 * @param month
      230 * @return
      231 */
      232 public static Integer getFirstDayOfMonth(int year, int month) {
            233 return getIntDate(newCalendar(year, month, 1));
      234 }
      235
      236 /**
      237 * 某年月的第一天
      238 *
      239 * @param year
      240 * @param month
      241 * @return
      242 */
      243 public static Integer getFirstDayOfThisMonth() {
            244 Integer year = DateUtil.getIntYearNow();
            245 Integer month = DateUtil.getIntMonthNow();
            246 return getIntDate(newCalendar(year, month, 1));
      247 }
      248
      249 /**
      250 * 某年月的第一天
      251 *
      252 * @param date
      253 * @return
      254 * @time:2008-7-4 上午09:58:55
      255 */
      256 public static Integer getFistDayOfMonth(Date date) {
            257 Integer intDate = getIntDate(date);
            258 int year = intDate / 10000;
            259 int month = intDate % 10000 / 100;
            260 return getIntDate(newCalendar(year, month, 1));
      261 }
      262
      263 /**
      264 * 某年月的最后一天
      265 *
      266 * @param year
      267 * @param month
      268 * @return
      269 */
      270 public static Integer getLastDayOfMonth(int year, int month) {
            271 return intDateSub(getIntDate(newCalendar(year, month + 1, 1)), 1);
      272 }
      273
      274 /**
      275 * 根据Calendar获取整型年份
      276 *
      277 * @param c
      278 * @return
      279 */
      280 public static Integer getIntYear(Calendar c) {
            281 int year = c.get(Calendar.YEAR);
            282 return year;
      283 }
      284
      285 /**
      286 * 根据Calendar获取整型日期
      287 *
      288 * @param c
      289 * @return
      290 */
      291 public static Integer getIntDate(Calendar c) {
            292 int year = c.get(Calendar.YEAR);
            293 int month = c.get(Calendar.MONTH) + 1;
            294 int day = c.get(Calendar.DAY_OF_MONTH);
            295 return year * 10000 + month * 100 + day;
      296 }
      297
      298 /**
      299 * 根据Date获取整型年份
      300 *
      301 * @param d
      302 * @return
      303 */
      304 public static Integer getIntYear(Date d) {
            305 if (d == null)
            306 {
                  307 return null;
            308 }
            309 Calendar c = Calendar.getInstance();
            310 c.setTime(d);
            311 return getIntYear(c);
      312 }
      313
      314 /**
      315 * 根据Date获取整型日期
      316 *
      317 * @param d
      318 * @return
      319 */
      320 public static Integer getIntDate(Date d) {
            321 if (d == null)
            322 {
                  323 return null;
            324 }
            325 Calendar c = Calendar.getInstance();
            326 c.setTime(d);
            327 return getIntDate(c);
      328 }
      329
      330 /**
      331 * 根据Integer获取Date日期
      332 *
      333 * @param n
      334 * @return
      335 */
      336 public static Date getDate(Integer n) {
            337 if (n == null)
            338 {
                  339 return null;
            340 }
            341 Calendar c = Calendar.getInstance();
            342 c.set(n / 10000, n / 100 % 100 - 1, n % 100);
            343 return c.getTime();
      344 }
      345
      346 public static Date getDate(String date) {
            347 if (date == null || date.length() == 0)
            348 {
                  349 return null;
            350 }
            351
            352 try
            353 {
                  354 if (date.contains("/"))
                  355 {
                        356 date = date.replaceAll("/", "-");
                  357 }
                  358 return getFormatInstance(DATE_FORMAT).parse(date);
            359 } catch (ParseException e)
            360 {
                  361 log.error("解析[" + date + "]错误!", e);
                  362 return null;
            363 }
      364 }
      365
      366 /**
      367 * 根据年份Integer获取Date日期
      368 *
      369 * @param year
      370 * @return
      371 */
      372 public static Date getFirstDayOfYear(Integer year) {
            373 if (year == null)
            374 {
                  375 return null;
            376 }
            377 Calendar c = Calendar.getInstance();
            378 c.set(year, 1, 1);
            379 return c.getTime();
      380 }
      381
      382 /**
      383 * 根据年月日生成Calendar
      384 *
      385 * @param year
      386 * @param month
      387 * @param day
      388 * @return
      389 */
      390 public static Calendar newCalendar(int year, int month, int day) {
            391 Calendar ret = Calendar.getInstance();
            392 if (year < 100)
            393 {
                  394 year = 2000 + year;
            395 }
            396 ret.set(year, month - 1, day);
            397 return ret;
      398 }
      399
      400 /**
      401 * 根据整型日期生成Calendar
      402 *
      403 * @param date
      404 * @return
      405 */
      406 public static Calendar newCalendar(int date) {
            407 int year = date / 10000;
            408 int month = (date % 10000) / 100;
            409 int day = date % 100;
            410
            411 Calendar ret = Calendar.getInstance();
            412 ret.set(year, month - 1, day);
            413 return ret;
      414 }
      415
      416 /**
      417 * 取得Date型的当前日期
      418 *
      419 * @return
      420 */
      421 public static Date getNow() {
            422 return new Date();
      423 }
      424
      425 /**
      426 * 取得Date型的当前日期
      427 *
      428 * @return
      429 */
      430 public static Date getToday() {
            431 return DateUtil.getDate(DateUtil.getIntToday());
      432 }
      433
      434 /**
      435 * 整数型日期的加法
      436 *
      437 * @param date
      438 * @param days
      439 * @return
      440 */
      441 public static Integer intDateAdd(int date, int days) {
            442 int year = date / 10000;
            443 int month = (date % 10000) / 100;
            444 int day = date % 100;
            445
            446 day += days;
            447
            448 return getIntDate(year, month, day);
      449 }
      450
      451 /**
      452 * 整数型日期的减法
      453 *
      454 * @param date
      455 * @param days
      456 * @return
      457 */
      458 public static Integer intDateSub(int date, int days) {
            459 return intDateAdd(date, -days);
      460 }
      461
      462 /**
      463 * 计算两个整型日期之间的天数
      464 *
      465 * @param startDate
      466 * @param endDate
      467 * @return
      468 */
      469 public static Integer daysBetweenDate(Integer startDate, Integer endDate) {
            470 if (startDate == null || endDate == null)
            471 {
                  472 return null;
            473 }
            474 Calendar c1 = newCalendar(startDate);
            475 Calendar c2 = newCalendar(endDate);
            476
            477 Long lg = (c2.getTimeInMillis() - c1.getTimeInMillis()) / 1000 / 60 / 60 / 24;
            478 return lg.intValue();
      479 }
      480
      481 /**
      482 * 计算两个整型日期之间的天数
      483 *
      484 * @param startDate
      485 * @param endDate
      486 * @return
      487 */
      488 public static Integer daysBetweenDate(Date startDate, Date endDate) {
            489 if (startDate == null || endDate == null)
            490 {
                  491 return null;
            492 }
            493 Long interval = endDate.getTime() - startDate.getTime();
            494 interval = interval / (24 * 60 * 60 * 1000);
            495 return interval.intValue();
      496 }
      497
      498 /**
      499 * 取得当前日期.
      500 *
      501 * @return 当前日期,字符串类型.
      502 */
      503 public static String getStringDate() {
            504 return getStringDate(DateUtil.getNow());
      505 }
      506
      507 /**
      508 * 根据calendar产生字符串型日期
      509 *
      510 * @param d
      511 * @return eg:20080707
      512 */
      513 public static String getStringDate(Date d) {
            514 if (d == null)
            515 {
                  516 return "";
            517 }
            518 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            519 return sdf.format(d);
      520 }
      521
      522 public static String getFormatStringDate(Date d) {
            523 if (d == null)
            524 {
                  525 return "";
            526 }
            527 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            528 return sdf.format(d);
      529 }
      530 // /**
      531 // * 国际化。
      532 // */
      533 // public static String formatI18nDate(Date date){
            534 // if(date == null){
                  535 // return "";
            536 // }
            537 // ActionSupport actionSupport = new ActionSupport();
            538 // SimpleDateFormat sdf = new
            539 // SimpleDateFormat(actionSupport.getText("date.i18n.format"));
            540 // return sdf.format(date);
      541 // }
      542
543 }




赞(0)    操作        顶端 
hei_nihao
注册用户
等级:少校
经验:1279
发帖:19
精华:0
注册:2015-4-17
状态:离线
发送短消息息给hei_nihao 加好友    发送短消息息给hei_nihao 发消息
发表于: IP:您无权察看 2015-4-21 9:24:34 | [全部帖] [楼主帖] 2  楼

你这头像 北京联动北方科技有限公司



赞(0)    操作        顶端 
lijun.fang
注册用户
等级:上尉
经验:751
发帖:29
精华:0
注册:1970-1-1
状态:离线
发送短消息息给lijun.fang 加好友    发送短消息息给lijun.fang 发消息
发表于: IP:您无权察看 2015-4-22 10:23:51 | [全部帖] [楼主帖] 3  楼

北京联动北方科技有限公司


赞(0)    操作        顶端 
总帖数
3
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论