网站: 
JavaEye 
          作者: 
iwinyeah 
                    链接:
http://iwinyeah.javaeye.com/blog/173704 
          发表时间: 2008年03月19日
          
          声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责任!
          
          我的一个日期处理类,解决了时区问题,给有需要的人。
package util;
/**
 * --------------------------------------------------
 * 日期转换对象
 * --------------------------------------------------
 * 主要提供日期与1970-01-01后的天数的转换和到字符串的转换
 * --------------------------------------------------
 * 
 * @author iwinyeah 李永超
 * @version 1.0.0
 * */
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class DateUtil {
	private static Calendar _calendar = Calendar.getInstance(); // 用于日期计算
	private static long MSEC_EVERYDAY = 86400000L; // 一天的微秒数
	private static int rawOffset = TimeZone.getDefault().getRawOffset();
	/**
	 * 将日期转换为1970-01-01后的天数
	 * 
	 * @param Date
	 *            theDate 要计算天数的日期
	 * @return int 所传入日期与1970-01-01相差的天数
	 */
	public static int dateToDay(Date theDate) {
		return (int) ((theDate.getTime() + rawOffset) / MSEC_EVERYDAY);
	}
	/**
	 * 将1970-01-01后的天数转换为日期
	 * 
	 * @param int
	 *            要取的日期与1970-01-01相差的天数
	 * @return Date theDate 与1970-01-01相差相应天数的日期
	 */
	public static Date dayToDate(int day) {
		return new Date(day * MSEC_EVERYDAY);
	}
	/**
	 * 取今天与1970-01-01相差的天数
	 * 
	 * @return int 取今天与1970-01-01相差的天数
	 */
	public static int toDay() {
		return (int) ((System.currentTimeMillis() + rawOffset) / MSEC_EVERYDAY);
	}
	/**
	 * 将日期转换为年月日字符串
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return String 对应日期年月日形式的字符串
	 */
	public static String getYMD(int theDay) {
		_calendar.setTime(dayToDate(theDay));
		return _calendar.get(Calendar.YEAR) % 100 + "/"
				+ (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
				+ (_calendar.get(Calendar.MONTH) + 1) + "/"
				+ (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
				+ _calendar.get(Calendar.DATE);
	}
	/**
	 * 将日期转换为年月字符串
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return String 对应日期年月形式的字符串
	 */
	public static String getYM(int theDay) {
		_calendar.setTime(dayToDate(theDay));
		return _calendar.get(Calendar.YEAR) + "/"
				+ (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
				+ (_calendar.get(Calendar.MONTH) + 1);
	}
	/**
	 * 将日期转换为月日字符串
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return String 对应日期月日形式的字符串
	 */
	public static String getMD(int theDay) {
		_calendar.setTime(dayToDate(theDay));
		return (_calendar.get(Calendar.MONTH) + 1 > 9 ? "" : "0")
				+ (_calendar.get(Calendar.MONTH) + 1) + "/"
				+ (_calendar.get(Calendar.DATE) > 9 ? "" : "0")
				+ _calendar.get(Calendar.DATE);
	}
	/**
	 * 将日期转换为当月一号
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return int 对应日期所在月份第一天与1970-01-01相差的天数
	 */
	public static int getMonthFirstDay(int theDay) {
		_calendar.setTime(dayToDate(theDay));
		_calendar.set(Calendar.DAY_OF_MONTH, 1);
		return (int) (dateToDay(_calendar.getTime()));
	}
	/**
	 * 取日期所在年份
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return int 对应日期所在年份
	 */
	public static int getYear(int theDay) {
		_calendar.setTime(dayToDate(theDay));
		return _calendar.get(Calendar.YEAR);
	}
	/**
	 * 取日期所在月份
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return int 对应日期所在月份
	 */
	public static int getMonth(int theDay) {
		_calendar.setTime(dayToDate(theDay));
		return _calendar.get(Calendar.MONTH);
	}
	/**
	 * 取日期所在周次
	 * 
	 * @param int
	 *            theDay 与1970-01-01相差的天数
	 * @return int 对应日期所在周次
	 */
	public static int getWeek(int theDay) {
		// 1971-01-03是星期日,从该日开始计算周次
		_calendar.setTime(dayToDate(theDay));
		return (int) ((_calendar.getTime().getTime() - 172800000L) / 604800000L);
	}
}
          
          
            本文的讨论也很精彩,浏览讨论>>
          
          
          JavaEye推荐
          
          
          
          文章来源:
http://iwinyeah.javaeye.com/blog/173704