/** * 实现对时间类型的格式处理. */package com.eeechina.framework.util;import java.text.ParseException;import java.text.ParsePosition;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;/** * 实现对时间类型的格式处理. * * * */public class DateHelper {/** * 将java.util.Date 格式转换为字符数组格式'2009-08' * 返回字符数组数组0位代表年,数组1位代表月 * * @param time * Date 日期 * @return String字符 */public static String fmtToStringYearMonth(Date time) {if (null == time) {return "";}try {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");String ymd = format.format(time);return ymd;} catch (Exception e) {}return "";}/** * 字符串转换为java.util.Date * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z 如 '2002-1-1 AD at 22:10:59 PSD' * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00' * yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm' * yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' * yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' * * @param time * String 字符串 * @return Date 日期 */public static Date stringToDate(String time) {if (null == time || 0 > time.length()) {return null;}try {SimpleDateFormat formatter;int tempPos = time.indexOf("AD");time = time.trim();formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");if (tempPos > -1) {time = time.substring(0, tempPos) + "公元"+ time.substring(tempPos + "AD".length());// chinaformatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");}tempPos = time.indexOf("-");if (tempPos > -1 && (time.indexOf(" ") < 0)) {formatter = new SimpleDateFormat("yyyyMMddHHmmssZ");} else if ((time.indexOf("/") > -1) && (time.indexOf(" ") > -1)) {formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");} else if ((time.indexOf("-") > -1) && (time.indexOf(" ") > -1)) {formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");} else if ((time.indexOf("/") > -1) && (time.indexOf("am") > -1)|| (time.indexOf("pm") > -1)) {formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");} else if ((time.indexOf("-") > -1) && (time.indexOf("am") > -1)|| (time.indexOf("pm") > -1)) {formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");}ParsePosition pos = new ParsePosition(0);java.util.Date ctime = formatter.parse(time, pos);return ctime;} catch (Exception e) {}return null;}/** * 将java.util.Date 格式转换为字符串格式'yyyy/MM/dd' * * @param time * Date 日期 * @return String 字符串 */public static String fmtShortEnu(Date time) {if (null == time) {return "";}try {SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");String strDate = formatter.format(time);return strDate;} catch (Exception e) {}return "";}/** * 将java.util.Date 格式转换为字符数组格式'{[yyyy][MM][dd]}' * 返回字符数组数组0位代表年,数组1位代表月和数组2位代表日 * * @param time * Date 日期 * @return String[] 字符数组 */public static String[] fmtToStringArray(Date time) {if (null == time) {return null;}String ymd = fmtToString(time);return ymd.split("-");}/** * 将java.util.Date 格式转换为字符数组格式'2009-08-04' * 返回字符数组数组0位代表年,数组1位代表月和数组2位代表日 * * @param time * Date 日期 * @return String字符 */public static String fmtToString(Date time) {if (null == time) {return "";}try {SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");String ymd = format.format(time);return ymd;} catch (Exception e) {}return "";}/** * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制) * 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21' * * @param time * Date 日期 * @return String 字符串 */public static String dateToString(Date time) {if (null == time) {return "";}try {SimpleDateFormat formatter;formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String ctime = formatter.format(time);return ctime;} catch (Exception e) {}return "";}/** * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制) * 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午' * * @param time * Date 日期 * @param x * int 任意整数如:1 * @return String 字符串 */public static String dateToString(Date time, int x) {if (null == time) {return "";}try {SimpleDateFormat formatter;formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");String ctime = formatter.format(time);return ctime;} catch (Exception e) {}return "";}/** * 取系统当前时间:返回只值为如下形式 2002-10-30 20:24:39 * * @return String */public static String Now() {return dateToString(new Date());}/** * 取系统当前时间:返回只值为如下形式 2002-10-30 08:28:56 下午 * * @param hour * 为任意整数 * @return String */public static String Now(int hour) {return dateToString(new Date(), hour);}/** * 取系统当前时间:返回值为如下形式 2002-10-30 * * @return String */public static String getYYYY_MM_DD() {return dateToString(new Date()).substring(0, 10);}/** * 取系统给定时间:返回值为如下形式 2002-10-30 * * @return String */public static String getYYYY_MM_DD(String date) {return date.substring(0, 10);}/** * 取得当前小时值. * * @return String */public static String getHour() {try {SimpleDateFormat formatter = new SimpleDateFormat("H");String ctime = formatter.format(new Date());return ctime;} catch (Exception e) {}return "";}/** * 取得当前天数. * * @return String */public static String getDay() {try {SimpleDateFormat formatter;formatter = new SimpleDateFormat("d");String ctime = formatter.format(new Date());return ctime;} catch (Exception e) {}return "";}/** * 取得当前月数. * * @return String */public static String getMonth() {try {SimpleDateFormat formatter;formatter = new SimpleDateFormat("M");String ctime = formatter.format(new Date());return ctime;} catch (Exception e) {}return "";}/** * 取得当前年数. * * @return String */public static String getYear() {try {SimpleDateFormat formatter;formatter = new SimpleDateFormat("yyyy");String ctime = formatter.format(new Date());return ctime;} catch (Exception e) {}return "";}/** * 取得当前周数. * * @return String */public static String getWeek() {try {SimpleDateFormat formatter;formatter = new SimpleDateFormat("E");String ctime = formatter.format(new Date());return ctime;} catch (Exception e) {}return "";}/** * 得到本月的第一天 * * @return */public static Date getMonthFirstDay() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);return calendar.getTime();}/** * 得到本月的最后一天 * * @return */public static Date getMonthLastDay() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));calendar.set(Calendar.HOUR_OF_DAY, 23);calendar.set(Calendar.MINUTE, 59);calendar.set(Calendar.SECOND, 59);return calendar.getTime();}/** * 得到上月的第一天 * * @return */public static Date getLastMonthFirstDay() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));calendar.add(Calendar.MONTH, -1);calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);return calendar.getTime();}/** * 得到上月的最后一天 * * @return */public static Date getLastMonthLastDay() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));calendar.add(Calendar.MONTH, -1);calendar.set(Calendar.HOUR_OF_DAY, 23);calendar.set(Calendar.MINUTE, 59);calendar.set(Calendar.SECOND, 59);return calendar.getTime();}/** * 得到上周的这个时间 * * @return */public static Date getLastWeekThisTime() {Calendar calendar = Calendar.getInstance();calendar.add(Calendar.WEEK_OF_YEAR, -1);calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);return calendar.getTime();}/** * 得到昨天 * * @return */public static Date getYesterday() {Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DAY_OF_YEAR, -1);calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);return calendar.getTime();}/** * 得到今天 * * @return */public static Date getToday() {Calendar calendar = Calendar.getInstance();calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);return calendar.getTime();}/** * 得到明天 * * @return */public static Date getTomorrow() {Calendar calendar = Calendar.getInstance();calendar.add(Calendar.DAY_OF_YEAR, 1);calendar.set(Calendar.HOUR_OF_DAY, 0);calendar.set(Calendar.MINUTE, 0);calendar.set(Calendar.SECOND, 0);return calendar.getTime();}/** * * 日期:2011-12-12 * 版本:v1.0 * 描述:getDaysBetween(得到两个DATE 之间的日期list) * * @param date_start * @param date_end * @return List * @Exception 异常对象 */public static List getDaysBetween(Date date_start, Date date_end) {List list = new ArrayList ();SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");Date d1 = null;Date d2 = null;try {d1 = sdf.parse(DateHelper.fmtToString(date_start));d2 = sdf.parse(DateHelper.fmtToString(date_end));} catch (ParseException e) {}Calendar cal_start = Calendar.getInstance();Calendar cal_end = Calendar.getInstance();cal_start.setTime(d1);cal_end.setTime(d2);while (cal_start.getTimeInMillis() <= cal_end.getTimeInMillis()) {list.add(cal_start.getTime());cal_start.add(Calendar.DAY_OF_YEAR, +1);};return list;}// 把一个字符串转换成日期类型/** * 日期:2012-12-20 * 版本:v1.0 * 描述:string2Date(yyyy-mm-dd) * 主要方法: 将yyyy-mm-dd 类型的日期字符串,转换为年-月-日形式日期 * @param str * @return * Date * @Exception 异常对象 */public static Date string2Date(String str) {if (null==str||str.length()<1) {return null;}String pattern = "yyyy-MM-dd"; // 系统默认转换该类型if (0 < str.indexOf("/")) {pattern = "yyyy/MM/dd";}SimpleDateFormat sdf = new SimpleDateFormat(pattern);Date date = null;try {date = sdf.parse(str);} catch (ParseException e) {e.printStackTrace();}return date;}// 将Date 类型转为String/** * 日期:2012-12-20 * 版本:v1.0 * 描述:date2String(将年-月-日 转换为String 串) * 主要方法: * @param date * @return * String * @Exception 异常对象 */public static String date2String(Date date) {if (null == date) {return "";}SimpleDateFormat sdf = null;String str = "";try {sdf = new SimpleDateFormat("yyyy-MM-dd");str = sdf.format(date);} catch (Exception e) {sdf = new SimpleDateFormat("yyyy/MM/dd");str = sdf.format(date);}return str;}}