一、前言
定义日期时间DateUtils工具类,进行不同日期格式转换处理、时间戳和日期的相互转换、日期格式校验、获取指定时间范围区间的时间集、获取当前天/周/小时/年等相关代码示例。
二、代码示例
import java.text.DateFormat;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.ArrayList;@b@import java.util.Calendar;@b@import java.util.Date;@b@import java.util.GregorianCalendar;@b@import java.util.List;@b@import java.util.TimeZone;@b@@b@public class DateUtils {@b@ @b@ public static final int TIMESTAMPTYPE_UNIX = 2;@b@@b@ public static Date getDate(String sDate) throws ParseException {@b@ return getDate(sDate, "");@b@ }@b@@b@ public static Date getJustDate(String sDate) throws ParseException {@b@ return getDate(sDate, "yyyy-MM-dd");@b@ }@b@@b@ public static Date getFlightDate(String date, String time)@b@ throws ParseException {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");@b@ return sdf.parse(date + " " + time);@b@ }@b@@b@ public static Date getDate(long lDate) throws ParseException {@b@ return getDate(lDate, "");@b@ }@b@@b@ public static Date getJustDate(long lDate) throws ParseException {@b@ return getDate(lDate, "yyyy-MM-dd");@b@ }@b@@b@ public static Date getDate(long lDate, String sFormat)@b@ throws ParseException {@b@ if ((sFormat == null) || ("".equals(sFormat))) {@b@ sFormat = "yyyy-MM-dd HH:mm:ss";@b@ }@b@@b@ return getDate(formatDate(new Date(lDate), sFormat), sFormat);@b@ }@b@@b@ public static Date getDate(String sDate, String sFormat)@b@ throws ParseException {@b@ if ((sFormat == null) || ("".equals(sFormat)))@b@ sFormat = "yyyy-MM-dd HH:mm:ss";@b@@b@ Date dValue = new SimpleDateFormat(sFormat).parse(sDate);@b@ return dValue;@b@ }@b@@b@ public static Date getDate(Date sDate, String sFormat)@b@ throws ParseException {@b@ return getDate(formatDate(sDate, sFormat), sFormat);@b@ }@b@@b@ public static Date getDate(Date sDate) throws ParseException {@b@ String sFormat = "yyyy-MM-dd";@b@ return getDate(sDate, sFormat);@b@ }@b@@b@ public static String formatDate(long sDate, String sFormat) {@b@ return formatDate(new Date(sDate), sFormat);@b@ }@b@@b@ public static String formatDate(long lDate) {@b@ return formatDate(new Date(lDate), "");@b@ }@b@@b@ public static String formatJustDate(long lDate) {@b@ return formatDate(new Date(lDate), "yyyy-MM-dd");@b@ }@b@@b@ public static String formatDate(Date date, String sFormat) {@b@ if ((sFormat == null) || ("".equals(sFormat))) {@b@ sFormat = "yyyy-MM-dd HH:mm:ss";@b@ }@b@@b@ return new SimpleDateFormat(sFormat).format(date);@b@ }@b@@b@ public static String formatDate(Date date) {@b@ return formatDate(date, TimeZone.getDefault());@b@ }@b@@b@ public static String formatDate(Date date, TimeZone timeZone) {@b@ return formatDate(date, timeZone, "yyyy-MM-dd");@b@ }@b@@b@ public static String formatDate(Date date, TimeZone timeZone, String sFormat) {@b@ SimpleDateFormat dateFormat = new SimpleDateFormat(sFormat);@b@ dateFormat.setTimeZone(TimeZone.getDefault());@b@ return dateFormat.format(date);@b@ }@b@@b@ public static String simplifyDate(String date) throws ParseException {@b@ Date d = getDate(date, "yyyy-MM-dd HH:mm:ss");@b@ return formatDate(d, "yy-MM-dd HH:mm");@b@ }@b@@b@ public static String getTodayStr() {@b@ DateFormat df = new SimpleDateFormat("yyyy-MM-dd");@b@ Date todate = new Date(System.currentTimeMillis());@b@ String today = df.format(todate);@b@ return today;@b@ }@b@@b@ public static String getDateStamp(int intFormat) {@b@ return findFormat(intFormat).format(new Date());@b@ }@b@@b@ public static String format(Date date, int intFormat) {@b@ SimpleDateFormat sdf = findFormat(intFormat);@b@ return sdf.format(date);@b@ }@b@@b@ public static SimpleDateFormat findFormat(int intFormat) {@b@ String strFormat = "yyyy'??'MM'??'dd'??' H:mm:ss.S";@b@ switch (intFormat) {@b@ case 0:@b@ strFormat = "yyyy'??'MM'??'dd'??' H:mm:ss.S";@b@ break;@b@ case 1:@b@ strFormat = "yyyy'-'MM'-'dd H:mm:ss.S";@b@ break;@b@ case 2:@b@ strFormat = "yyyy'??'MM'??'dd'??'";@b@ break;@b@ case 3:@b@ strFormat = "yyyy'-'MM'-'dd";@b@ break;@b@ case 4:@b@ strFormat = "H:mm:ss";@b@ break;@b@ case 5:@b@ strFormat = "K:mm:ss a";@b@ break;@b@ case 6:@b@ strFormat = "yyyy'??'MM'??'dd'??' H:mm:ss";@b@ break;@b@ case 7:@b@ strFormat = "yyyy'??'MM'??'dd'??' K:mm:ss a";@b@ break;@b@ case 8:@b@ strFormat = "yyyy-MM-dd H:mm:ss";@b@ break;@b@ case 9:@b@ strFormat = "yyyy-MM-dd K:mm:ss a";@b@ break;@b@ case 10:@b@ strFormat = "H:mm:ss.S";@b@ break;@b@ case 11:@b@ strFormat = "K:mm:ss.S a";@b@ break;@b@ case 12:@b@ strFormat = "H:mm";@b@ break;@b@ case 13:@b@ strFormat = "K:mm a";@b@ break;@b@ case 14:@b@ strFormat = "yyyy-MM-dd H:mm";@b@ break;@b@ case 15:@b@ strFormat = "yyyyMMddHHmmssS";@b@ break;@b@ case 16:@b@ strFormat = "yyyyMMdd";@b@ break;@b@ case 17:@b@ strFormat = "yyyy/MM/dd";@b@ break;@b@ case 18:@b@ strFormat = "yyyy/MM/dd H:mm:ss";@b@ break;@b@ case 19:@b@ strFormat = "yyyy-MM-dd";@b@ break;@b@ case 20:@b@ strFormat = "yyyy-MM-dd HH:mm:ss";@b@ break;@b@ default:@b@ strFormat = "yyyy'??'MM'??'dd'??' H:mm:ss.S";@b@ }@b@@b@ return new SimpleDateFormat(strFormat);@b@ }@b@@b@ public static String formatDateStr(String dateStr, int newtimestampType,@b@ int oldtimestampType) throws ParseException {@b@ Date oldDate = findFormat(oldtimestampType).parse(dateStr);@b@ return format(oldDate, newtimestampType);@b@ }@b@@b@ public static boolean isValdateDate(String strDate, int timestampType) {@b@ boolean isPassed = false;@b@@b@ SimpleDateFormat sdf = findFormat(timestampType);@b@ sdf.setLenient(false);@b@ if ((strDate != null) && (strDate.length() > 0))@b@ try {@b@ Date dtCheck = sdf.parse(strDate);@b@ String strCheck = sdf.format(dtCheck);@b@ if (strDate.equals(strCheck))@b@ isPassed = true;@b@ else@b@ isPassed = false;@b@ } catch (Exception e) {@b@ isPassed = false;@b@ }@b@@b@ return isPassed;@b@ }@b@@b@ public static List<String> getDateList(String startTime, String endTime)@b@ throws Exception {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@b@ List dateInfos = new ArrayList();@b@ Date start = null;@b@ Date end = null;@b@ startTime = startTime + " 00:00:00";@b@ endTime = endTime + " 23:59:59";@b@ try {@b@ start = sdf.parse(startTime);@b@ end = sdf.parse(endTime);@b@ } catch (Exception e) {@b@ throw new Exception("Parse date exception !");@b@ }@b@@b@ if (start.after(end)) {@b@ throw new Exception("Query time error.Start time after end time.");@b@ }@b@@b@ Calendar calStart = Calendar.getInstance();@b@ calStart.setTime(start);@b@@b@ Calendar calEnd = Calendar.getInstance();@b@ calEnd.setTime(end);@b@@b@ sdf = new SimpleDateFormat("yyyy-MM-dd");@b@ if (calStart.after(calEnd))@b@ dateInfos.add(startTime);@b@ else@b@ while (!(calStart.after(calEnd))) {@b@ dateInfos.add(sdf.format(calStart.getTime()));@b@ calStart.add(5, 1);@b@ }@b@@b@ return dateInfos;@b@ }@b@@b@ public static long findDateSpaceInMinuties(String startTime, String endTime)@b@ throws ParseException {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");@b@@b@ Date startDate = sdf.parse(startTime + " 00:00:00");@b@@b@ Date endDate = sdf.parse(endTime + " 23:59:59");@b@@b@ long minutes = (endDate.getTime() - startDate.getTime()) / 60000L;@b@@b@ return (minutes + 1L);@b@ }@b@@b@ public static List<String> findDateScopeStandardFormat(String startTime,@b@ String endTime, int divisor, String strFormat)@b@ throws ParseException {@b@ List returnValue = null;@b@ if ((startTime == null) || (startTime.length() <= 0))@b@ return returnValue;@b@@b@ if ((endTime == null) || (endTime.length() <= 0))@b@ return returnValue;@b@@b@ if ((divisor == 0) || (divisor % 2 != 0))@b@ return returnValue;@b@@b@ returnValue = new ArrayList();@b@ SimpleDateFormat sdf = new SimpleDateFormat(strFormat);@b@@b@ Date startDate = sdf.parse(startTime + " 00:00:00");@b@ Date endDate = sdf.parse(endTime + " 23:59:59");@b@@b@ Calendar calStartTime = Calendar.getInstance();@b@ calStartTime.setTime(startDate);@b@@b@ Calendar calEndTime = Calendar.getInstance();@b@ calEndTime.setTime(endDate);@b@@b@ int minuties = (int) findDateSpaceInMinuties(startTime, endTime)@b@ / divisor;@b@@b@ while (!(calStartTime.after(calEndTime))) {@b@ returnValue.add(sdf.format(calStartTime.getTime()));@b@ calStartTime.add(12, minuties);@b@ }@b@ return returnValue;@b@ }@b@@b@ public static long getDateTime(String date, String format)@b@ throws ParseException {@b@ SimpleDateFormat sdf = new SimpleDateFormat(format);@b@@b@ return (sdf.parse(date).getTime() / 1000L);@b@ }@b@@b@ public static long getZeroTimeStampOfDay(Date date, int timestampType) {@b@ long returnValue = -1L;@b@ Calendar calendar = null;@b@@b@ calendar = GregorianCalendar.getInstance();@b@ calendar.setTime(date);@b@ calendar.set(14, 0);@b@ calendar.set(13, 0);@b@ calendar.set(12, 0);@b@ calendar.set(11, 0);@b@@b@ returnValue = calendar.getTimeInMillis();@b@@b@ if (timestampType == 2) {@b@ returnValue /= 1000L;@b@ }@b@@b@ return returnValue;@b@ }@b@@b@ public static long getDateTimestamp(Date date, int timestampType) {@b@ long returnValue = -1L;@b@@b@ if (date == null)@b@ return -1L;@b@@b@ returnValue = date.getTime();@b@@b@ if (timestampType == 2) {@b@ returnValue /= 1000L;@b@ }@b@@b@ return returnValue;@b@ }@b@@b@ public static String findYestarday(int timestampType) {@b@ Calendar calendar = null;@b@ calendar = GregorianCalendar.getInstance();@b@ calendar.setTime(new Date());@b@ calendar.set(14, 0);@b@ calendar.set(13, 0);@b@ calendar.set(12, 0);@b@ calendar.set(11, 0);@b@ calendar.add(5, -1);@b@@b@ return format(calendar.getTime(), timestampType);@b@ }@b@@b@ public static Date formatDate(String date, int intFormat) throws Exception {@b@ Calendar calendar = null;@b@ calendar = GregorianCalendar.getInstance();@b@ calendar.setTime(findFormat(intFormat).parse(date));@b@ calendar.set(14, 0);@b@ calendar.set(13, 0);@b@ calendar.set(12, 0);@b@ calendar.set(11, 0);@b@ return calendar.getTime();@b@ }@b@@b@ public static String addDate(String dateStr, int intFormat, int addNum)@b@ throws Exception {@b@ Calendar calendar = null;@b@ calendar = GregorianCalendar.getInstance();@b@ calendar.setTime(findFormat(intFormat).parse(dateStr));@b@ calendar.set(14, 0);@b@ calendar.set(13, 0);@b@ calendar.set(12, 0);@b@ calendar.set(11, 0);@b@ calendar.add(5, addNum);@b@ return findFormat(intFormat).format(calendar.getTime());@b@ }@b@@b@ public static Date toDate(String param, String format)@b@ throws ParseException {@b@ if ((null == param) || ("".equals(param.trim())))@b@ return null;@b@@b@ Date date = null;@b@ SimpleDateFormat sdf = new SimpleDateFormat(format);@b@ date = sdf.parse(param);@b@ return date;@b@ }@b@@b@ public static Date computeDate(Date date, int day) {@b@ GregorianCalendar nowDate = new GregorianCalendar();@b@ nowDate.setTime(date);@b@ nowDate.add(5, day);@b@ Date computeAfter = nowDate.getTime();@b@ return computeAfter;@b@ }@b@@b@ public static Date todayTrim(Date date) {@b@ Calendar calendar = null;@b@ calendar = GregorianCalendar.getInstance();@b@ calendar.setTime(date);@b@ calendar.set(14, 0);@b@ calendar.set(13, 0);@b@ calendar.set(12, 0);@b@ calendar.set(11, 0);@b@ return calendar.getTime();@b@ }@b@@b@ public static int getAge(Date birthDay) {@b@ Calendar noewCal = Calendar.getInstance();@b@@b@ if (noewCal.before(birthDay)) {@b@ throw new IllegalArgumentException(@b@ "The birthDay is after Now.It's unbelievable!");@b@ }@b@@b@ int yearNow = noewCal.get(1);@b@ int monthNow = noewCal.get(2) + 1;@b@ int dayOfMonthNow = noewCal.get(5);@b@@b@ noewCal.setTime(birthDay);@b@ int yearBirth = noewCal.get(1);@b@ int monthBirth = noewCal.get(2);@b@ int dayOfMonthBirth = noewCal.get(5);@b@@b@ int age = yearNow - yearBirth;@b@@b@ if (monthNow <= monthBirth) {@b@ if (monthNow == monthBirth)@b@ if (dayOfMonthNow < dayOfMonthBirth)@b@ --age;@b@@b@ else@b@ --age;@b@@b@ }@b@@b@ return age;@b@ }@b@@b@ public static int getWeek(Date date) {@b@ int weekIndex = -1;@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ weekIndex = calendar.get(7);@b@ return weekIndex;@b@ }@b@@b@ public static int getValueByParamater(Date date, int param) {@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ return calendar.get(param);@b@ }@b@@b@ public static int getYear(Date date) {@b@ return getValueByParamater(date, 1);@b@ }@b@@b@ public static int getMonth(Date date) {@b@ return (getValueByParamater(date, 2) + 1);@b@ }@b@@b@ public static int getDay(Date date) {@b@ return getValueByParamater(date, 5);@b@ }@b@@b@ public static String getDayOfWeek(Date date) {@b@ Calendar cal = Calendar.getInstance();@b@ cal.setTime(date);@b@ String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };@b@ return weekDays[(cal.get(7) - 1)];@b@ }@b@@b@ public static Date addDays(Date date, int amount) {@b@ return add(date, 5, amount);@b@ }@b@@b@ public static Date add(Date date, int calendarField, int amount) {@b@ if (date == null)@b@ throw new IllegalArgumentException("The date must not be null");@b@@b@ Calendar c = Calendar.getInstance();@b@ c.setTime(date);@b@ c.add(calendarField, amount);@b@ return c.getTime();@b@ }@b@@b@ public static String getDayOfWeek(Calendar cal) {@b@ String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };@b@ return weekDays[(cal.get(7) - 1)];@b@ }@b@}