java日期处理简单封装

今天没什么事情做,温习一下基本知识,在网上看到和日期处理的相关框架,什么joda,date4j等,都宣称超级强大简单易用。下下来试了下,确实都挺不错。不过自己不是经常涉及到日期操作,且涉及到的也不复杂。且不说这些库的功能强不强大,单说为了处理个时间就引入几十个类,实在有点浪费了。再说JDK提供的Calendar和SimpleDateFormat组合使用功能也还是非常强大啊。如果觉得同时使用这两个类稍显麻烦,可以稍微封装一下,即可满足大部分需求,就一个类,自己需要用到什么功能的时候,添加进去就行了。

package luojing.date;

import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 日期时间处理封装
 * 
 * @author luojing
 */
public class DateTime implements Comparable<DateTime>, Serializable {

	private static final long serialVersionUID = 4715414577633839197L;
	private Calendar calendar = Calendar.getInstance();
	private SimpleDateFormat sdf = new SimpleDateFormat();

	private final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
	private final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
	private final String DEFAULT_TIME_PATTERN = "HH:mm:ss";

	public DateTime() {
	}

	public DateTime(String dateStr) {
		try {
			parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DateTime(String dateStr, TimeZone timeZone) {
		this(dateStr);
		calendar.setTimeZone(timeZone);
	}

	public DateTime(String dateStr, String pattern) {
		try {
			parse(dateStr, pattern);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public DateTime(String dateStr, String pattern, TimeZone timeZone) {
		this(dateStr, pattern);
		calendar.setTimeZone(timeZone);
	}

	public DateTime(int year, int month, int day, int hour, int minute, int second) {
		calendar.set(year, month, day, hour, minute, second);
	}

	public DateTime(int year, int month, int day, int hour, int minute, int second, TimeZone timeZone) {
		this(year, month, day, hour, minute, second);
		calendar.setTimeZone(timeZone);
	}

	public DateTime(long milliSeconds) {
		calendar.setTimeInMillis(milliSeconds);
	}

	public Calendar getCalendar() {
		return calendar;
	}

	public void setCalendar(Calendar calendar) {
		this.calendar = calendar;
	}

	public Date getDate() {
		return calendar.getTime();
	}

	public void setDate(Date date) {
		calendar.setTime(date);
	}

	public int getYear() {
		return calendar.get(Calendar.YEAR);
	}

	public void setYear(int year) {
		calendar.set(Calendar.YEAR, year);
	}

	public int getMonth() {
		return calendar.get(Calendar.MONTH);
	}

	public void setMonth(int month) {
		calendar.set(Calendar.MONTH, month);
	}

	public int getDay() {
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	public void setDay(int dayOfMonth) {
		calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
	}

	public int getHour() {
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	public void setHour(int hour) {
		calendar.set(Calendar.HOUR_OF_DAY, hour);
	}

	public int getMinute() {
		return calendar.get(Calendar.MINUTE);
	}

	public void setMinute(int minute) {
		calendar.set(Calendar.MINUTE, minute);
	}

	public int getSecond() {
		return calendar.get(Calendar.SECOND);
	}

	public void setSecond(int second) {
		calendar.set(Calendar.SECOND, second);
	}

	public long getTimeInMilliSeconds() {
		return calendar.getTimeInMillis();
	}

	public void setTimeInMilliSeconds(long milliSeconds) {
		calendar.setTimeInMillis(milliSeconds);
	}

	public TimeZone getTimeZone() {
		return calendar.getTimeZone();
	}

	public void setTimeZone(TimeZone timeZone) {
		calendar.setTimeZone(timeZone);
	}

	/**
	 * 使用默认格式解析日期字符串
	 * 
	 * @param dateStr
	 * @throws Exception
	 */
	public void parse(String dateStr) throws Exception {
		try {
			parse(dateStr, DEFAULT_DATETIME_PATTERN);
		} catch (Exception e) {
			try {
				parse(dateStr, DEFAULT_DATE_PATTERN);
			} catch (Exception e1) {
				try {
					parse(dateStr, DEFAULT_TIME_PATTERN);
				} catch (Exception e2) {
					throw new Exception("the date string [" + dateStr + "] could not be parsed");
				}
			}
		}

	}

	/**
	 * 使用给定模式解析日期字符串
	 * 
	 * @param dateStr
	 * @param pattern
	 * @throws Exception
	 */
	public void parse(String dateStr, String pattern) throws Exception {
		if (dateStr == null) {
			throw new NullPointerException("date string could not be null");
		}

		if (pattern == null) {
			throw new NullPointerException("the pattern string could not be null");
		}

		try {
			sdf.applyPattern(pattern);
			sdf.parse(dateStr);
		} catch (ParseException e) {
			throw new Exception("the date string [" + dateStr + "] could not be parsed with the pattern [" + pattern + "]");
		}
		calendar = sdf.getCalendar();
	}

	/**
	 * 格式化当前DateTime对象代表的时间
	 * 
	 * @param pattern
	 * @return
	 */
	public String format(String pattern) {
		sdf.applyPattern(pattern);
		return sdf.format(calendar.getTime());
	}

	/**
	 * 获取默认格式日期字符串
	 * 
	 * @return
	 */
	public String toDateTimeString() {
		sdf.applyPattern(DEFAULT_DATETIME_PATTERN);
		return sdf.format(calendar.getTime());
	}

	@Override
	public int compareTo(DateTime otherDateTime) {
		return calendar.compareTo(otherDateTime.getCalendar());
	}

	/**
	 * 是否闰年
	 * 
	 * @return
	 */
	public boolean isLeapYear() {
		int year = getYear();
		boolean result = false;
		if (year % 100 == 0) {
			if (year % 400 == 0) {
				result = true;
			}
		} else if (year % 4 == 0) {
			result = true;
		}
		return result;
	}

	/**
	 * 获取星期
	 * 
	 * @return
	 */
	public int getDayOfWeek() {
		return calendar.get(Calendar.DAY_OF_WEEK);
	}

	/**
	 * 是否周末
	 * 
	 * @return
	 */
	public boolean isWeekend() {
		int dayOfWeek = getDayOfWeek();
		return dayOfWeek == 1 || dayOfWeek == 7;
	}

	/**
	 * 当前DateTime对象月份天数
	 * 
	 * @return
	 */
	public int getDayNumsInMonth() {
		Calendar c = (Calendar) calendar.clone();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.roll(Calendar.DAY_OF_MONTH, -1);
		return c.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 两个日期之间间隔天数
	 * 
	 * @param otherDateTime
	 * @return
	 */
	public int dayNumFrom(DateTime otherDateTime) {
		long millis = Math.abs(getTimeInMilliSeconds() - otherDateTime.getTimeInMilliSeconds());
		int days = (int) Math.floor(millis / 86400000);
		return days;
	}

	public boolean lessThan(DateTime otherDateTime) {
		return compareTo(otherDateTime) < 0;
	}

	public boolean greaterThan(DateTime otherDateTime) {
		return compareTo(otherDateTime) > 0;
	}

	public boolean equal(DateTime otherDateTime) {
		return compareTo(otherDateTime) == 0;
	}

	/**
	 * 当前时间基础上增加秒数(负数时为减)
	 * 
	 * @param amount
	 */
	public void plusSecond(int amount) {
		calendar.add(Calendar.SECOND, amount);
	}

	public void plusMinute(int amount) {
		calendar.add(Calendar.MINUTE, amount);
	}

	public void plusHour(int amount) {
		calendar.add(Calendar.HOUR, amount);
	}

	public void plusDays(int amount) {
		calendar.add(Calendar.DAY_OF_MONTH, amount);
	}

	public void plusMonth(int amount) {
		calendar.add(Calendar.MONTH, amount);
	}

	public void plusYear(int amount) {
		calendar.add(Calendar.YEAR, amount);
	}

	public void plus(int year, int month, int day, int hour, int minute, int second) {
		plusYear(year);
		plusMonth(month);
		plusDays(day);
		plusHour(hour);
		plusMinute(minute);
		plusSecond(second);
	}

}
测试:

package luojing.date;

import java.util.Date;

public class DateTimeTest {

	public static void main(String[] args) throws Exception {
		DateTime dateTime = new DateTime();
		DateTime dateTime2 = new DateTime("2013-12-12");
		System.out.println("默认格式输出:" + dateTime.toDateTimeString());
		System.out.println("是否闰年:" + dateTime.isLeapYear());
		System.out.println("自定义格式输出:" + dateTime.format("yyyy-MM-dd"));
		System.out.println("输出到毫秒:" + dateTime.format("yyyy-MM-dd HH:mm:ss.SSS"));
		System.out.println("某月天数:" + dateTime.getDayNumsInMonth());
		System.out.println("星期:" + dateTime.getDayOfWeek());//1:星期日,7:星期六
		System.out.println("是否周末:" + dateTime.isWeekend());
		System.out.println("相距:" + dateTime.dayNumFrom(dateTime2) + "天");
		
		dateTime.plusMonth(1);
		System.out.println("增加一个月后的datetime: " + dateTime.toDateTimeString());
		dateTime.plus(0, 0, 2, 4, 4, 5);
		System.out.println("增加 XXX后的datetime: " + dateTime.toDateTimeString());
		System.out.println("毫秒数:" + dateTime.getTimeInMilliSeconds());
		
		//DateTime转换为Date
		Date date = dateTime.getDate();
		System.out.println( dateTime.getTimeInMilliSeconds() == date.getTime());
	}
}

输出:

默认格式输出:2013-09-25 19:16:15
是否闰年:false
自定义格式输出:2013-09-25
输出到毫秒:2013-09-25 19:16:15.278
某月天数:30
星期:4
是否周末:false
相距:77天
增加一个月后的datetime: 2013-10-25 19:16:15
增加 XXX后的datetime: 2013-10-27 23:20:20
毫秒数:1382887220278
true


本人使用到日期处理相关的操作,大多就是格式化时间和时间之间的比较。这个封装已经完全能够满足我的日常需要了,再说,即使找不到合适的封装方法,还可以直接获取包装的calendar对象来做处理。

你可能感兴趣的:(java日期处理简单封装)