一、前言
关于empire-db源码包中org.apache.empire.commons.DateUtils日期工具类,对获取指定日期类型、对指定日期时分秒及常见格式化处理等。
二、源码说明
package org.apache.empire.commons;@b@@b@import java.text.DateFormat;@b@import java.text.ParseException;@b@import java.text.SimpleDateFormat;@b@import java.util.Calendar;@b@import java.util.Date;@b@import java.util.Locale;@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@public class DateUtils@b@{@b@ private static final Logger log = LoggerFactory.getLogger(DateUtils.class);@b@@b@ public static Date getDateNow()@b@ {@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.set(11, 0);@b@ calendar.set(12, 0);@b@ calendar.set(13, 0);@b@ calendar.set(14, 0);@b@ return calendar.getTime();@b@ }@b@@b@ public static Date getTimeNow()@b@ {@b@ return Calendar.getInstance().getTime();@b@ }@b@@b@ public static Date addDate(Date date, int years, int months, int days)@b@ {@b@ Calendar calendar = Calendar.getInstance();@b@ if (date != null)@b@ calendar.setTime(date);@b@ if (years != 0)@b@ calendar.add(1, years);@b@ if (months != 0)@b@ calendar.add(2, months);@b@ if (days != 0)@b@ calendar.add(5, days);@b@ return calendar.getTime();@b@ }@b@@b@ public static Date setDate(Date date, int year, int month, int day)@b@ {@b@ Calendar calendar = Calendar.getInstance();@b@ if (date != null)@b@ calendar.setTime(date);@b@ if (year > 0)@b@ calendar.set(1, year);@b@ if (month >= 0)@b@ calendar.set(2, month);@b@ if (day > 0)@b@ calendar.set(5, day);@b@ return calendar.getTime();@b@ }@b@@b@ public static Date getDate(int year, int month, int day)@b@ {@b@ Calendar calendar = Calendar.getInstance();@b@ if (year > 0)@b@ calendar.set(1, year);@b@ if (month >= 0)@b@ calendar.set(2, month);@b@ if (day > 0)@b@ calendar.set(5, day);@b@@b@ calendar.set(11, 0);@b@ calendar.set(12, 0);@b@ calendar.set(13, 0);@b@ calendar.set(14, 0);@b@ return calendar.getTime();@b@ }@b@@b@ public static Date setTime(Date date, int hours, int minutes, int seconds, int millis)@b@ {@b@ Calendar calendar = Calendar.getInstance();@b@ calendar.setTime(date);@b@ calendar.set(11, hours);@b@ calendar.set(12, minutes);@b@ calendar.set(13, seconds);@b@ calendar.set(14, millis);@b@ return calendar.getTime();@b@ }@b@@b@ public static Date getDateOnly(Date date)@b@ {@b@ return setTime(date, 0, 0, 0, 0);@b@ }@b@@b@ public static Date parseDate(String sDate, Locale locale)@b@ {@b@ DateFormat df;@b@ try@b@ {@b@ df = DateFormat.getDateInstance(3, getSafeLocale(locale));@b@ df.setLenient(true);@b@ return df.parse(sDate);@b@ }@b@ catch (ParseException e) {@b@ log.error("Invalid date value", e); }@b@ return null;@b@ }@b@@b@ private static Locale getSafeLocale(Locale locale)@b@ {@b@ return ((locale == null) ? Locale.getDefault() : locale);@b@ }@b@@b@ public static String formatDate(Date d, Locale locale)@b@ {@b@ DateFormat df = DateFormat.getDateInstance(2, getSafeLocale(locale));@b@ return df.format(d);@b@ }@b@@b@ public static String formatTime(Date d, Locale locale, boolean withSeconds)@b@ {@b@ int style = (withSeconds) ? 2 : 3;@b@ DateFormat df = DateFormat.getTimeInstance(style, getSafeLocale(locale));@b@ return df.format(d);@b@ }@b@@b@ public static String formatDayOfWeek(Date d, Locale locale, boolean longFormat)@b@ {@b@ SimpleDateFormat sdf = new SimpleDateFormat("", getSafeLocale(locale));@b@ Calendar c = Calendar.getInstance(getSafeLocale(locale));@b@ c.setTime(d);@b@ int dayOfWeek = c.get(7);@b@ if (longFormat)@b@ return sdf.getDateFormatSymbols().getWeekdays()[dayOfWeek];@b@@b@ return sdf.getDateFormatSymbols().getShortWeekdays()[dayOfWeek];@b@ }@b@@b@ public static String formatMonth(Date d, Locale locale, boolean longFormat)@b@ {@b@ SimpleDateFormat sdf = new SimpleDateFormat("", getSafeLocale(locale));@b@ Calendar c = Calendar.getInstance(getSafeLocale(locale));@b@ c.setTime(d);@b@ int month = c.get(2);@b@ if (longFormat)@b@ return sdf.getDateFormatSymbols().getMonths()[month];@b@@b@ return sdf.getDateFormatSymbols().getShortMonths()[month];@b@ }@b@@b@ public static int getWeekOfYear(Date d, Locale locale)@b@ {@b@ Calendar c = Calendar.getInstance(getSafeLocale(locale));@b@ c.setTime(d);@b@ return c.get(3);@b@ }@b@@b@ public static String formatMonth(int month, Locale locale, boolean longFormat)@b@ {@b@ SimpleDateFormat sdf = new SimpleDateFormat("", getSafeLocale(locale));@b@ if (longFormat)@b@ return sdf.getDateFormatSymbols().getMonths()[month];@b@@b@ return sdf.getDateFormatSymbols().getShortMonths()[month];@b@ }@b@@b@ public static String formatYear(Date d, Locale locale)@b@ {@b@ SimpleDateFormat sdf = new SimpleDateFormat("yyyy", getSafeLocale(locale));@b@ Calendar c = Calendar.getInstance(getSafeLocale(locale));@b@ c.setTime(d);@b@ return sdf.format(d);@b@ }@b@}