---
title: "java日期处理"
author: "sshqwe"
date: "2011-03-16T21:13:11Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2011-03-16T21:13:11Z"
id: 964690
url: "https://sshqwe.iteye.com/blog/964690"
---

package com.broadengate.core.commons.time;

import java.text.DateFormat;  
import java.text.ParseException;  
import java.text.SimpleDateFormat;  
import java.util.ArrayList;  
import java.util.Calendar;  
import java.util.Collection;  
import java.util.Date;  
import java.util.Iterator;

import org.apache.log4j.Logger;

/\**  
\* 日期处理的类  
\*  
\*/  
public class DateUtils extends org.apache.commons.lang.time.DateUtils  
{  
/\**  
  * Logger for this class  
  \*/  
private static final Logger logger = Logger.getLogger(DateUtils.class);

/\**  
  * 日期格式化对象  
  \*/  
private static DateFormat dateFormat = new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);

private static DateFormat dateFormatCN = new SimpleDateFormat(&quot;yyyy年MM月dd日&quot;);

/\**  
  * 日期格式化对象  
  \*/  
private static DateFormat monthDateFormate = new SimpleDateFormat(&quot;MM-dd&quot;);  
/\**  
  * 日期时间格式化对象  
  \*/  
private static DateFormat dateTimeFormat = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm&quot;);  
/\**  
  * 时间格式化对象  
  \*/  
private static DateFormat timeFormat = new SimpleDateFormat(&quot;HH:mm:ss&quot;);

public static final SimpleDateFormat sdf = new SimpleDateFormat(&quot;yyyy-MM-dd HH:mm:ss&quot;);

/\**  
  * 日期转换为字符窜：yyyy-mm-dd hh:mm:ss  
  *  
  * @param d  
  * @return  
  \*/  
public static String timeToString(Date d)  
{  
  return sdf.format(d);  
}

/\**  
  * 字符窜转换为日期：yyyy-mm-dd hh:mm:ss  
  *  
  * @param s  
  * @return  
  * @throws ParseException  
  \*/  
public static Date stringToDate(String s)  
{  
  if (s == null)  
  {  
   return null;  
  }  
  try  
  {  
   return sdf.parse(s);  
  } catch (ParseException e)  
  {  
   // do nothing, just return null;  
   return null;  
  }  
}

/\**  
  * 获取时间格式化对象 &quot;yyyy-MM-dd&quot;  
  *  
  * @return  
  \*/  
public static final DateFormat getDateFormat()  
{  
  return dateFormat;  
}

/\**  
  * 获取时间日期格式化对象 &quot;yyyy-MM-dd HH:mm&quot;  
  *  
  * @return  
  \*/  
public static final DateFormat getDateTimeFormat()  
{  
  return dateTimeFormat;  
}

/\**  
  * @return Returns the monthDateFormate.  
  \*/  
public static DateFormat getMonthDateFormate()  
{  
  return monthDateFormate;  
}

/\**  
  * 获取当前时间的时间对象  
  *  
  * @return  
  \*/  
public static final Date nowDate()  
{  
  return new Date();  
}

/\**  
  * 系统最小时间  
  *  
  * @return  
  \*/  
public static final Date minDate()  
{  
  return dateBegin(getDate(1900, 1, 1));  
}

/\**  
  * 系统最大时间  
  *  
  * @return  
  \*/  
public static final Date maxDate()  
{  
  return dateEnd(getDate(2079, 1, 1));  
}

/\**  
  * 获取指定时间的年  
  *  
  * @param date  
  * @return  
  \*/  
public static final int year(Date date)  
{  
  Calendar calendar = Calendar.getInstance();  
  calendar.setTime(date);  
  return calendar.get(Calendar.YEAR);  
}

/\**  
  * 获取指定时间的月  
  *  
  * @param date  
  * @return  
  \*/  
public static final int month(Date date)  
{  
  Calendar calendar = Calendar.getInstance();  
  calendar.setTime(date);  
  return calendar.get(Calendar.MONTH) + 1;  
}

/\**  
  * 获取指定时间的日  
  *  
  * @param date  
  * @return  
  \*/  
public static final int day(Date date)  
{  
  Calendar calendar = Calendar.getInstance();  
  calendar.setTime(date);  
  return calendar.get(Calendar.DATE);  
}

/\**  
  * 获取一个时间对象  
  *  
  * @param year  
  *            格式为：2004  
  * @param month  
  *            从1开始  
  * @param date  
  *            从1开始  
  * @return  
  \*/  
public static final Date getDate(int year, int month, int date)  
{  
  Calendar calendar = Calendar.getInstance();  
  calendar.set(year, month - 1, date);  
  return calendar.getTime();  
}

/\**  
  * 获取一个时间对象  
  *  
  * @param year  
  *            格式为：2004  
  * @param month  
  *            从1开始  
  * @param date  
  *            从1开始  
  * @param hour  
  * @param minute  
  * @param second  
  * @return  
  \*/  
public static final Date getDateTime(int year, int month, int date, int hour, int minute, int second)  
{  
  Calendar calendar = Calendar.getInstance();  
  calendar.set(year, month - 1, date, hour, minute, second);  
  return calendar.getTime();  
}

/\**  
  * 返回两个时间相差的天数  
  *  
  * @param d1  
  * @param d2  
  * @return  
  \*/  
public static int dateSub(String d1, String d2)  
{  
  int count = 0;  
  if (d1 != null &amp;&amp; d1.trim().length() &gt; 0 &amp;&amp; d2 != null &amp;&amp; d2.trim().length() &gt; 0)  
  {  
   count = (int) (DateUtils.parseDate(d1).getTime() / (24 * 60 * 60 * 1000) - DateUtils  
     .parseDate(d2).getTime()  
     / (24 * 60 * 60 * 1000));  
  }  
  return count;  
}

/\**  
  * 返回两个时间相差的小时数  
  *  
  * @param a  
  * @param b  
  * @return  
  \*/  
public static final int subHour(Date a, Date b)  
{  
  int hour = (int) (b.getTime() / (60 * 60 * 1000) - a.getTime() / (60 * 60 * 1000));  
  return hour  0)  
  {  
   beResult = 1;  
  } else if ((d1.getTime() - d2.getTime())  0 &amp;&amp; endDate != null  
    &amp;&amp; endDate.trim().length() &gt; 0)  
  {  
   if (dateSub(startDate, endDate) == 0)  
   {  
    return new String\[] { startDate };  
   } else  
   {  
    Date start = DateUtils.parseDate(startDate);  
    Date end = DateUtils.parseDate(endDate);  
    int count = DateUtils.dateSub(end, start) + 1;  
    dateSpace = new String\[count];  
    dateSpace\[0] = DateUtils.formatDate(start);  
    for (int i = 1; i = 0 &amp;&amp; c.get(Calendar.HOUR\_OF\_DAY) = 0 &amp;&amp; c.get(Calendar.HOUR\_OF\_DAY)  0)  
  {  
   // 处理第一天  
   if (DateUtils.isAMorPM(startDate).equals(&quot;AM&quot;))  
   {  
    list.add(DateUtils.formatDate(startDate) + &quot;AM&quot;);  
    list.add(DateUtils.formatDate(startDate) + &quot;PM&quot;);  
   }  
   if (DateUtils.isAMorPM(startDate).equals(&quot;PM&quot;))  
   {  
    list.add(DateUtils.formatDate(startDate) + &quot;PM&quot;);  
   }

   /\** 处理第二天到结束日期的前一天 \*\*/  
   Date temp = DateUtils.addDate(startDate, 0, 0, 1);  
   while (DateUtils.dateSub(endDate, temp) &gt; 0)  
   {  
    list.add(DateUtils.formatDate(temp) + &quot;AM&quot;);  
    list.add(DateUtils.formatDate(temp) + &quot;PM&quot;);  
    temp = DateUtils.addDate(temp, 0, 0, 1);  
   }

   // 处理最后一天  
   if (DateUtils.isAMorPM(endDate).equals(&quot;AM&quot;))  
   {  
    list.add(DateUtils.formatDate(endDate) + &quot;AM&quot;);  
   }  
   if (DateUtils.isAMorPM(endDate).equals(&quot;PM&quot;))  
   {  
    list.add(DateUtils.formatDate(endDate) + &quot;AM&quot;);  
    list.add(DateUtils.formatDate(endDate) + &quot;PM&quot;);  
   }  
  }  
  return list;  
}

// 获得时间区间数据(比如2007-8-1到2007-8-30，将获得一个连续的时间数组dateSpace\[]{&quot;2007-8-1AM&quot;,&quot;2007-8-1PM&quot;,&quot;2007-8-2AM&quot;,&quot;2007-8-2PM&quot;...&quot;2007-8-30PM&quot;})，精确到半天  
public static Collection getHalfDateSpace(String startDate, String endDate)  
{  
  Collection list = null;  
  if (startDate != null &amp;&amp; startDate.trim().length() &gt; 0 &amp;&amp; endDate != null  
    &amp;&amp; endDate.trim().length() &gt; 0)  
  {  
   Date start = parseDateTime(startDate);  
   Date end = parseDateTime(endDate);  
   list = new ArrayList();  
   // 两个日期在同一天的情况  
   if (dateSub(start, end) == 0)  
   {  
    if (isAMorPM(start).equals(&quot;AM&quot;) &amp;&amp; isAMorPM(end).equals(&quot;PM&quot;))  
    {  
     list.add(formatDate(start) + &quot;AM&quot;);  
     list.add(formatDate(start) + &quot;PM&quot;);  
    }  
    if (isAMorPM(start).equals(&quot;AM&quot;) &amp;&amp; isAMorPM(end).equals(&quot;AM&quot;))  
    {  
     list.add(formatDate(start) + &quot;AM&quot;);  
    }  
    if (isAMorPM(start).equals(&quot;PM&quot;) &amp;&amp; isAMorPM(end).equals(&quot;PM&quot;))  
    {  
     list.add(formatDate(start) + &quot;PM&quot;);  
    }  
   }  
   if (dateSub(endDate, startDate) &gt; 0)  
   {  
    // 处理第一天  
    if (DateUtils.isAMorPM(start).equals(&quot;AM&quot;))  
    {  
     list.add(formatDate(start) + &quot;AM&quot;);  
     list.add(formatDate(start) + &quot;PM&quot;);  
    }  
    if (DateUtils.isAMorPM(start).equals(&quot;PM&quot;))  
    {  
     list.add(formatDate(start) + &quot;PM&quot;);  
    }

    /\** 处理第二天到结束日期的前一天 \*\*/  
    Date temp = addDate(start, 0, 0, 1);  
    String t = null;  
    while (dateSub(end, temp) &gt; 0)  
    {  
     list.add(formatDate(temp) + &quot;AM&quot;);  
     list.add(formatDate(temp) + &quot;PM&quot;);  
     temp = addDate(temp, 0, 0, 1);  
    }

    // 处理最后一天  
    if (DateUtils.isAMorPM(end).equals(&quot;AM&quot;))  
    {  
     list.add(formatDate(end) + &quot;AM&quot;);  
    }  
    if (DateUtils.isAMorPM(end).equals(&quot;PM&quot;))  
    {  
     list.add(formatDate(end) + &quot;AM&quot;);  
     list.add(formatDate(end) + &quot;PM&quot;);  
    }  
   }  
  }  
  return list;  
}

// 获得时间区间数据(比如2007-8-1到2007-8-30，将获得一个连续的时间数组dateSpace\[]{&quot;2007-8-1AM&quot;,&quot;2007-8-1PM&quot;,&quot;2007-8-2AM&quot;,&quot;2007-8-2PM&quot;...&quot;2007-8-30PM&quot;})，精确到半天  
public static Collection getHalfDateSpace(Date startDate, int count)  
{  
  Collection list = new ArrayList();  
  // 处理第一天  
  list.add(formatDate(startDate) + &quot;AM&quot;);  
  list.add(formatDate(startDate) + &quot;PM&quot;);

  /\** 处理第二天到结束日期的前一天 \*\*/  
  Date temp = addDate(startDate, 0, 0, 1);  
  int c = 1;  
  while (c  0 &amp;&amp; endDate != null  
    &amp;&amp; endDate.trim().length() &gt; 0)  
  {  
   String\[] s = getDateSpace(startDate, endDate);  
   for (int i = 0; i  0)  
  {  
   for (Iterator i = list.iterator(); i.hasNext();)  
   {  
    String str = (String) i.next();  
    if (strDate.equalsIgnoreCase(str))  
    {  
     beResult = true;  
     break;  
    }  
   }  
  }  
  return beResult;  
}

/\**  
  * 获取当前年的前若干年  
  *  
  * @return  
  \*/  
public static String\[] getYearArray(int n)  
{  
  // 取当前年的前若干年  
  String\[] yearArray = new String\[n];  
  int y = year(nowDate());  
  for (int i = 0; i