本站首页    管理页面    写新日志    退出


«June 2026»
123456
78910111213
14151617181920
21222324252627
282930


公告
暂无公告...

我的分类(专题)

日志更新

最新评论

留言板

链接


Blog信息
blog名称:
日志总数:4
评论数量:5
留言数量:0
访问次数:25322
建立时间:2010年7月14日




[java部分]一个Date工具类
原创空间,  软件技术

neverback 发表于 2010/8/13 13:40:16

日期类。。工作中经常会用到的转日期,日期偏移...等 顺便发句牢骚,chrome居然不支持Rich text编辑器,折腾好久不知道怎么回事,发出来的东西就是不给我换行。。换成IE才OK. ================分割线==================   import java.util.Calendar;import java.util.Date;import java.sql.Timestamp;import java.text.SimpleDateFormat; public class DateUtil {      /**  * Format 格式 yyyy-MM-dd  */ public static final String YMD="yyyy-MM-dd"; /**  *  Format 格式 yyyy-MM-dd HH:mm:ss  */ public static final String YMDHMS="yyyy-MM-dd HH:mm:ss"; /**  *  Format 格式 yyyyMMdd-HHmm  */ public static final String YMD_HM="yyyyMMdd-HHmm"; /**  *  Format 格式 dd/HHmm  */ public static final String DD_HHmm="dd/HHmm"; /**  *  Format 格式 MMdd/HHmm  */ public static final String MMDD_HHmm="MMdd/HHmm"; /**  *  Format 格式 yyyy-MM  */ public static final String YM="yyyy-MM";  public static final String YYYY="yyyy"; /**  * Format 格式 yyyyMM  */ public static final String YYYYMM="yyyyMM";     public static String getNowYm() {        return DateUtil.getDateValue (new Date(),DateUtil.YYYYMM);    }     public static String getNowYear(){        return DateUtil.getDateValue (new Date(),DateUtil.YYYY);    }     public static int getYear() {        return Integer.parseInt (getNowYear());    }     /**     * 两时间相减得到天数(yyyyMMdd) ,日期字符串2-日期字符串1     * @param date1 日期字符串1     * @param date2 日期字符串2     * @return 天数     */    public static long getDayTimes(String date1, String date2) {        try {            SimpleDateFormat yyyyMMdd = new SimpleDateFormat(DateUtil.YMD);            long d1 = yyyyMMdd.parse(date1).getTime();            long d2 = yyyyMMdd.parse(date2).getTime();            return (d2 - d1) / (1000 * 60 * 60 * 24);        } catch (Exception ex) {            System.out.println(ex);            return 0;        }    }  /**  * 日期字符串2-日期字符串1, 获得小时数(yyyyMMdd)  * @param date2 日期字符串2  * @return  小时数  */    public static long getHours(String date1, String date2) {        try {            SimpleDateFormat yyyyMMdd = new SimpleDateFormat(DateUtil.YMD);            long d1 = yyyyMMdd.parse(date1).getTime();            long d2 = yyyyMMdd.parse(date2).getTime();            return (d2 - d1) / (1000 * 60 * 60);        } catch (Exception ex) {            System.out.println(ex);            return 0;        }    }  /**     * 获取小时数(yyyyMMddhhmmss)     * @param date1 日期字符串1     * @param date2 日期字符串2     * @return  小时数     */    public static float getDayTimes0(String date1, String date2) {        try {            SimpleDateFormat yyyyMMddhhmmss = new SimpleDateFormat(DateUtil.YMDHMS);            long d1 = yyyyMMddhhmmss.parse(date1).getTime();            long d2 = yyyyMMddhhmmss.parse(date2).getTime();            return ((float)(d2 - d1)) / (1000 * 60 *60);        } catch (Exception ex) {            return 0;        }    }     /**     * 设置一个时间根据偏移量得到新的时间     * @param dateTime  日期     * @param excursion 偏移量     * @param format  日期格式 [yyyy-MM-dd|yyyy-MM-dd HH:mm:ss]     * @return  字符串     */    public static String getExcursionDateString(String dateTime,int excursion,String format){        return getDateValue(getExcursionDate(dateTime,excursion,format),format);    }     /**     * 设置一个时间根据偏移量得到新的年月     * @param dateTime 日期     * @param excursion 偏移量     * @return 年月     */    public static String getExcursionYearMonthString(String dateTime,int excursion){        if(dateTime.length ()<10)dateTime=dateTime+"-01";        return getDateValue(getExcursionMonth(dateTime,excursion),DateUtil.YM);    }     /**     * 设置一个时间根据偏移量得到新的时间     * @param dateTime  日期     * @param excursion 偏移量(天)     * @return Date对象     */    public static Date getExcursionDate(String dateTime,int excursion,String fmt){        Calendar cal = Calendar.getInstance();         if(fmt.equals(DateUtil.DD_HHmm)){            cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)),Integer.parseInt(dateTime.substring(11,13)),Integer.parseInt(dateTime.substring(14,16)));        }else if (fmt.equals(DateUtil.YMDHMS)){            cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)),Integer.parseInt(dateTime.substring(11,13)),Integer.parseInt(dateTime.substring(14,16)),Integer.parseInt(dateTime.substring(17,19)));        }else            cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)));        cal.add(Calendar.DATE,excursion);         return cal.getTime();    }     public static Date getExcuSecond(String dateTime,int second){        Calendar cal=Calendar.getInstance ();        cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)),Integer.parseInt(dateTime.substring(11,13)),Integer.parseInt(dateTime.substring(14,16)),Integer.parseInt(dateTime.substring(17,19)));        cal.add (Calendar.SECOND,second);        return cal.getTime ();    }       /**  * 设置一个时间根据偏移量得到新的时间  * @param dateTime  * @param excursion 偏移量(小时)  * @param fmt DD_HHmm| YMDHMS | others  * @return Date对象  */      public static Date getExcursionDateTime(String dateTime,int excursion,String fmt){          if(dateTime!=null){              Calendar cal = Calendar.getInstance();              if(fmt.equals(DateUtil.DD_HHmm)){                  cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)),Integer.parseInt(dateTime.substring(11,13)),Integer.parseInt(dateTime.substring(14,16)));              }else if (fmt.equals(DateUtil.YMDHMS)){                  cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)),Integer.parseInt(dateTime.substring(11,13)),Integer.parseInt(dateTime.substring(14,16)),Integer.parseInt(dateTime.substring(17,19)));              }else                  cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7))-1, Integer.parseInt(dateTime.substring(8,10)));               cal.add(Calendar.HOUR,excursion);               return cal.getTime();          }else{              return null;          }      }  /**  * 月份的偏移  * @param dateTime  * @param excursion  * @return  */    public static Date getExcursionMonth(String dateTime,int excursion){        Calendar cal = Calendar.getInstance();        cal.set(Integer.parseInt(dateTime.substring(0,4)), Integer.parseInt(dateTime.substring(5,7)), Integer.parseInt(dateTime.substring(8,10)));        cal.add(Calendar.MONTH, excursion);        return cal.getTime();    }     /**     * 获取当前的时间     * @return 字符串格式     */    public static String getNowDayTime() {        java.util.Date nowdate = new java.util.Date();        Timestamp ts=new java.sql.Timestamp(nowdate.getTime());        String now = ts.toString();         nowdate=null;        ts=null;        return now;    }     /**     * 将时间对象按照一定的格式转换成字符串     * @param aDate  时间对象     * @param format yyyy-MM-dd | yyyy-MM-dd HH:mm:ss | dd/HHmm     * @return 时间字符串     */    public static String getDateValue(Date aDate,String format) {        try {            if (aDate == null) return "";            SimpleDateFormat yyyyMMddhhmmss = new SimpleDateFormat(format);            return yyyyMMddhhmmss.format(aDate);        } catch (Exception ex) {            return "";        }    }     /**     * 比较d段是否包含t段     * @param d1     * @param d2     * @param t1     * @param t2     * @return true 包含 | false 不包含     */    public static boolean dateAreaIsContain(Date d1,Date d2,Date t1,Date t2)    {        return DateUtil.getDayTimes0 (DateUtil.getDateValue (d1,DateUtil.YMDHMS),                    DateUtil.getDateValue (t1,DateUtil.YMDHMS))>=0 &&                DateUtil.getDayTimes0 (DateUtil.getDateValue (t2,DateUtil.YMDHMS),                    DateUtil.getDateValue (d2,DateUtil.YMDHMS))>=0  ;    }  /**  * 年月的偏移  * @param ym 年月  * @param move 偏移量  * @return 新的年月  */    public static String getExcursionYearMonthString0(String ym,int move){        String _tmp=DateUtil.getExcursionYearMonthString(ym.substring (0,4)+"-"+ym.substring (4),move);  return _tmp.substring (0,4)+_tmp.substring (5);    }  /**  * 获取某年某月的天数  * @param year  年  * @param month  月  * @return 天数  */ public static int getMonthDays(int year,int month){  Calendar time=Calendar.getInstance();  time.clear();  time.set(Calendar.YEAR,year);  time.set(Calendar.MONTH,month-1);//注意,Calendar对象默认一月为0  return time.getActualMaximum(Calendar.DAY_OF_MONTH);//本月份的天数 }         /**  * 判断当前是否是本月的最后一天  * @return true 是 false 否  */ public static boolean isMonthEndDay(){  boolean isEndDay=false;   java.util.Date nowdate = new java.util.Date();  int days=getMonthDays(nowdate.getYear (),nowdate.getMonth ());         if(DateUtil.getDayTimes(DateUtil.getDateValue(new Date(),DateUtil.YM)+"-"+days,DateUtil.getDateValue(new Date(),DateUtil.YMD))==0){   isEndDay=true;  }  return isEndDay; }  /**  * 返回指定年月的最后一天  * @param year  年  * @param month  月  * @return  最后一天  */    public static String getEndDay(int year,int month){        int days=getMonthDays(year,month);        if(month<10)            return year+"-0"+month+"-"+days;        else            return year+"-"+month+"-"+days;    }  public static String getFDateTime(String dateTime){  if(dateTime!=null)   return dateTime.substring (5,7)+dateTime.substring (8,10)+"/"+dateTime.substring (11,13)+dateTime.substring (14,16);  else      return null; }  /**  * 将日期字符串转换为日期类型,格式:yyyy-mm-dd  * @param dateTime  日期字符串  * @return  转换后的日期类型  */ public static Date getDateByStr(String dateTime){  return getExcursionDate(dateTime,0,DateUtil.YMD); }  public static Date getDate(String dateTime){  return getExcursionDate(dateTime,0,DateUtil.YMDHMS); } }


阅读全文(1446) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144829690 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号