---
title: "java有关时间转换的一些方法DateFactory.java"
author: "dh189"
date: "2010-07-12T13:17:17Z"
tags: []
categories: ["编程语言"]
slug: "java-datefactory-java"
draft: false
lastmod: "2010-07-12T13:20:50Z"
id: 710567
url: "https://dh189.iteye.com/blog/710567"
---

```java

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * <p>Title:时间转换类 </p>
 * <p>Description:有关时间转换的一些方法 </p>
 * @author ZZJ
 * @version 1.0
 */

public class DateFactory {

	/**
	 * 根据pattern 转换日期对象为日期字符串,如"yyyy/MM/dd hh:mm:ss"
	 * @param date 日期
	 * @param pattern 日期格式
	 * @return 日期字串
	 */
	public static String getFormatDate(Date date, String pattern) {
		if (date == null) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		String str = format.format(date);
		return str;
	}

	/**
	 * 根据参数得到当前时间值
	 * @param p (1:年；2：月；3：日；4：小时；5：分钟；6：秒)
	 * @return
	 */
	public static int getTimeValue(int p) {
		Calendar calendar = Calendar.getInstance();
		int result = 0;
		switch (p) {
		case 1:
			result = calendar.get(Calendar.YEAR);
			break;
		case 2:
			result = calendar.get(Calendar.MONTH) + 1;
			break;
		case 3:
			result = calendar.get(Calendar.DATE);
			break;
		}
		return result;
	}

	/**
	 *转换日期对象为日期字符串
	 * @param date 日期对象
	 * @param isFull 是否为完整的日期数据,
	 *               为true时, 格式如"2000-03-05 01:05:04"
	 *               为false时, 格式如 "2000-03-05"
	 * @return 符合要求的日期字符串
	 */
	public static String getSmpFormatDate(Date date, boolean isFull) {
		String pattern = null;
		if (date == null)
			return "";
		if (isFull) {
			pattern = "yyyy-MM-dd HH:mm:ss";
		} else {
			pattern = "yyyy-MM-dd";
		}
		return getFormatDate(date, pattern);
	}

	/**
	 * 转换日期对象为日期字符串,格式如"2000-03-05 01:05:04"
	 * 
	 * @param date 日期对象
	 * @return
	 */
	public static String getSmpFormatDate(Date date) {
		return getSmpFormatDate(date, true);
	}

	/**
	 * 将字符串转换为日期对象
	 * @param dateStr 字符串
	 * @param pattern 日期的pattern
	 * @return 日期对象,不成功则返回 null
	 */
	private static Date convertStrToDate(String dateStr, String pattern) {
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (Exception ex) {

		} finally {
		}
		return date;
	}

	/**
	 * 将字符串转换为日期对象,采用默认的pattern
	 * @param dateStr 字符串
	 * @return 日期对象,不成功则返回 null
	 */
	public static Date convertStrToDate(String dateStr) {
		try {
			String pattern = "";
			if (dateStr.length() <= 10) {
				pattern = "yyyy-MM-dd";
			} else if (dateStr.length() <= 20) {
				pattern = "yyyy-MM-dd HH:mm:ss";
			} else {
				pattern = "yyyy-MM-dd HH:mm:ss.SSS";
			}
			return convertStrToDate(dateStr, pattern);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 得到两个时间的差,并以字符串的形式给出
	 * @param dt1
	 * @param dt2
	 * @return
	 */
	public static String getDiffTime(Date dt1, Date dt2) {
		long time = dt2.getTime() / 1000 - dt1.getTime() / 1000;
		String str = "0:0:0";
		if (time > 0) {
			long day = time / 60 / 60 / 24;
			long hour = time / 60 / 60 % 24;
			long minute = time / 60 % 60;
			long second = time % 60;
			if (day > 0) {
				str = "" + day + "d " + hour + ":" + minute + ":" + second;
			} else {
				str = "" + hour + ":" + minute + ":" + second;
			}
		}
		return str;
	}

	/**
	 * 得到时间串精确到毫秒
	 * 
	 * @return
	 */
	public static String getTimeChar() {
		StringBuffer strResponseDate = new StringBuffer();
		try {
			Date date = new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			format.setLenient(false);

			strResponseDate.append(format.format(date));
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return strResponseDate.toString();
	}

	/**
	 * 计算两个日期间相隔的周数
	 * 
	 * @param startDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 * @return
	 */
	public static int computeWeek(Date startDate, Date endDate) {
		int weeks = 1;
		Calendar beginCalendar = Calendar.getInstance();
		beginCalendar.setTime(startDate);
		Calendar endCalendar = Calendar.getInstance();
		endCalendar.setTime(endDate);
		while (beginCalendar.before(endCalendar)) {
			// 如果开始日期和结束日期在同年、同月且当前月的同一周时结束循环
			if (beginCalendar.get(Calendar.YEAR) == endCalendar.get(Calendar.YEAR)
					&& beginCalendar.get(Calendar.MONTH) == endCalendar.get(Calendar.MONTH)
					&& beginCalendar.get(Calendar.DAY_OF_WEEK_IN_MONTH) == endCalendar
							.get(Calendar.DAY_OF_WEEK_IN_MONTH)) {
				break;
			} else {
				beginCalendar.add(Calendar.DAY_OF_YEAR, 7);
				weeks += 1;
			}
		}
		return weeks;
	}

	/**
	 * 加减天数
	 * 
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date dateAddOrReduce(Date date, int d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, d);
		return cal.getTime();
	}

	/**
	 * 根据当前日期获取之后一周的莫一天
	 * @param d 星期数 例如星期一d=1,星期日d=7
	 * @return
	 */
	public static Date getWeekDate(int d) {
		return getWeekDate(new Date(), d);
	}

	/**
	 * 根据指定日期获取之后一周的莫一天
	 * @param d 星期数 例如星期一d=1,星期日d=7
	 * @return
	 */
	public static Date getWeekDate(Date date, int d) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.set(Calendar.DAY_OF_WEEK, d + 1);
		return cal.getTime();
	}
}

```