日期处理类

本文介绍了一个实用的日期转换工具类,提供了将日期转换为特定格式字符串的方法,包括年月日、年月及月日等,并能计算指定日期与1970年1月1日之间的天数差。
  1. import java.util.Calendar;
  2. import java.util.Date;
  3. import java.util.TimeZone;
  4. /**  
  5.  * --------------------------------------------------  
  6.  * 日期转换对象-主要提供日期与1970-01-01后的天数的转换和到字符串的转换  
  7.  * --------------------------------------------------  
  8.  * */ 
  9. public class DateUtil {   
  10.     private static Calendar _calendar = Calendar.getInstance(); // 用于日期计算   
  11.     private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒数   
  12.     private static int rawOffset = TimeZone.getDefault().getRawOffset();   
  13.     /**  
  14.      * 将日期转换为1970-01-01后的天数  
  15.      * @param Date   theDate 要计算天数的日期  
  16.      * @return int   所传入日期与1970-01-01相差的天数  
  17.      */  
  18.     public static int dateToDay(Date theDate) {   
  19.         return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);   
  20.     }   
  21.     /**  
  22.      * 将1970-01-01后的天数转换为日期  
  23.      * @param int  要取的日期与1970-01-01相差的天数  
  24.      * @return Date theDate 与1970-01-01相差相应天数的日期  
  25.      */  
  26.     public static Date dayToDate(int day) {   
  27.         return new Date(day * MSEC_EVERYDAY);   
  28.     }   
  29.     /**  
  30.      * 取今天与1970-01-01相差的天数  
  31.      * @return int 取今天与1970-01-01相差的天数  
  32.      */  
  33.     public static int toDay() {   
  34.         return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);   
  35.     }   
  36.     /**  
  37.      * 将日期转换为年月日字符串  
  38.      * @param int  theDay 与1970-01-01相差的天数  
  39.      * @return String 对应日期年月日形式的字符串  
  40.      */  
  41.     public static String getYMD(int theDay) {   
  42.         _calendar.setTime(dayToDate(theDay));   
  43.         return _calendar.get(Calendar.YEAR) % 100 + "/"  
  44.                 + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
  45.                 + (_calendar.get(Calendar.MONTH) + 1) + "/"  
  46.                 + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
  47.                 + _calendar.get(Calendar.DATE);   
  48.     }   
  49.     /**  
  50.      * 将日期转换为年月字符串  
  51.      * @param int  theDay 与1970-01-01相差的天数  
  52.      * @return String 对应日期年月形式的字符串  
  53.      */  
  54.     public static String getYM(int theDay) {   
  55.         _calendar.setTime(dayToDate(theDay));   
  56.         return _calendar.get(Calendar.YEAR) + "/"  
  57.                 + (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
  58.                 + (_calendar.get(Calendar.MONTH) + 1);   
  59.     }   
  60.   
  61.     /**  
  62.      * 将日期转换为月日字符串  
  63.      * @param int theDay 与1970-01-01相差的天数  
  64.      * @return String 对应日期月日形式的字符串  
  65.      */  
  66.     public static String getMD(int theDay) {   
  67.         _calendar.setTime(dayToDate(theDay));   
  68.         return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")   
  69.                 + (_calendar.get(Calendar.MONTH) + 1) + "/"  
  70.                 + (_calendar.get(Calendar.DATE) > 9 ? "" : "0")   
  71.                 + _calendar.get(Calendar.DATE);   
  72.     }   
  73.     /**  
  74.      * 将日期转换为当月一号  
  75.      * @param int   theDay 与1970-01-01相差的天数  
  76.      * @return int 对应日期所在月份第一天与1970-01-01相差的天数  
  77.      */  
  78.     public static int getMonthFirstDay(int theDay) {   
  79.         _calendar.setTime(dayToDate(theDay));   
  80.         _calendar.set(Calendar.DAY_OF_MONTH, 1);   
  81.         return (int) (dateToDay(_calendar.getTime()));   
  82.     }   
  83.     /**  
  84.      * 取日期所在年份  
  85.      * @param int  theDay 与1970-01-01相差的天数  
  86.      * @return int 对应日期所在年份  
  87.      */  
  88.     public static int getYear(int theDay) {   
  89.         _calendar.setTime(dayToDate(theDay));   
  90.         return _calendar.get(Calendar.YEAR);   
  91.     }   
  92.     /**  
  93.      * 取日期所在月份  
  94.      * @param int theDay 与1970-01-01相差的天数  
  95.      * @return int 对应日期所在月份  
  96.      */  
  97.     public static int getMonth(int theDay) {   
  98.         _calendar.setTime(dayToDate(theDay));   
  99.         return _calendar.get(Calendar.MONTH);   
  100.     }   
  101.     /**  
  102.      * 取日期所在周次  
  103.      * @param int theDay 与1970-01-01相差的天数  
  104.      * @return int 对应日期所在周次  
  105.      */  
  106.     public static int getWeek(int theDay) {   
  107.         // 1971-01-03是星期日,从该日开始计算周次   
  108.         _calendar.setTime(dayToDate(theDay));   
  109.         return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);   
  110.     }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值