|
http://blog.csdn.net/gubaohua/archive/2005/07/29/439506.aspx
http://icansoft.blog.51cto.com/268543/50000
http://topic.csdn.net/t/20050727/12/4171134.html
http://brucezheng.iteye.com/blog/86967
http://www.blogjava.net/ruoyoux/archive/2008/10/26/236711.html
import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;public abstract class DateHelper {public static final int YEAR = Calendar.YEAR;public static final int MONTH = Calendar.MONTH;public static final int DATE = Calendar.DATE;public static final int HOUR = Calendar.HOUR;public static final int HOUR24 = Calendar.HOUR_OF_DAY;public static final int MINUTE = Calendar.MINUTE;public static final int SECOND = Calendar.SECOND;public static final int DAY_OF_WEEK = Calendar.DAY_OF_WEEK;public static final int WEEK_OF_YEAR = Calendar.WEEK_OF_YEAR;public static final long millisInDay = 86400000;private static final SimpleDateFormat mFormat6chars = new SimpleDateFormat("yyMMdd");private static final SimpleDateFormat mFormat8chars = new SimpleDateFormat("yyyyMMdd");private static final SimpleDateFormat mFormatIso8601Day = new SimpleDateFormat("yyyy-MM-dd");private static final SimpleDateFormat mFormatIso8601 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// http://www.w3.org/Protocols/rfc822/Overview.html#z28private static final SimpleDateFormat mFormatRfc822 = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");// -----------------------------------------------------------------------/** * Returns a Date set to the last possible millisecond of the day, just * before midnight. If a null day is passed in, a new Date is created. * midnight (00m 00h 00s) */public static Date getEndOfDay(Date day) {if (day == null)day = new Date();Calendar cal = Calendar.getInstance();cal.setTime(day);cal.set(Calendar.HOUR_OF_DAY, cal.getMaximum(Calendar.HOUR_OF_DAY));cal.set(Calendar.MINUTE, cal.getMaximum(Calendar.MINUTE));cal.set(Calendar.SECOND, cal.getMaximum(Calendar.SECOND));cal.set(Calendar.MILLISECOND, cal.getMaximum(Calendar.MILLISECOND));return cal.getTime();}/** * Returns a Date set to the first possible millisecond of the day, just * after midnight. If a null day is passed in, a new Date is created. * midnight (00m 00h 00s) */public static Date getStartOfDay(Date day) {if (day == null)day = new Date();Calendar cal = Calendar.getInstance();cal.setTime(day);cal.set(Calendar.HOUR_OF_DAY, cal.getMinimum(Calendar.HOUR_OF_DAY));cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));return cal.getTime();}/** * Returns a Date set just to Noon, to the closest possible millisecond of * the day. If a null day is passed in, a new Date is created. nnoon (00m * 12h 00s) */public static Date getNoonOfDay(Date day) {if (day == null)day = new Date();Calendar cal = Calendar.getInstance();cal.setTime(day);cal.set(Calendar.HOUR_OF_DAY, 12);cal.set(Calendar.MINUTE, cal.getMinimum(Calendar.MINUTE));cal.set(Calendar.SECOND, cal.getMinimum(Calendar.SECOND));cal.set(Calendar.MILLISECOND, cal.getMinimum(Calendar.MILLISECOND));return cal.getTime();}// -----------------------------------------------------------------------/** * Returns a string the represents the passed-in date parsed according to * the passed-in format. Returns an empty string if the date or the format * is null. */public static String format(Object dateStr, SimpleDateFormat format) {if (dateStr == null || format == null) {return "";}return format.format(dateStr);}// -----------------------------------------------------------------------/** * Returns a Date using the passed-in string and format. Returns null if the * string is null or empty or if the format is null. The string must match * the format. */public static Date parse(String dateValue, SimpleDateFormat aFormat)throws ParseException {if (StringHelper.isEmpty(dateValue) || aFormat == null) {return null;}return aFormat.parse(dateValue);}public static Date parse(String dateValue, String formatStr)throws ParseException {if (StringHelper.isEmpty(dateValue) || formatStr == null) {return null;}return new SimpleDateFormat(formatStr).parse(dateValue);}// -----------------------------------------------------------------------/** * Returns true if endDate is after startDate or if startDate equals endDate * or if they are the same date. Returns false if either value is null. */public static boolean isValidDateRange(Date startDate, Date endDate) {return isValidDateRange(startDate, endDate, true);}// -----------------------------------------------------------------------/** * Returns true if endDate is after startDate or if startDate equals * endDate. Returns false if either value is null. If equalOK, returns true * if the dates are equal. */public static boolean isValidDateRange(Date startDate, Date endDate,boolean equalOK) {// false if either value is nullif (startDate == null || endDate == null) {return false;}if (equalOK) {// true if they are equalif (startDate.equals(endDate)) {return true;}}// true if endDate after startDateif (endDate.after(startDate)) {return true;}return false;}// -----------------------------------------------------------------------// convenience method returns long friendly formatted timestamppublic static String format6chars(Object date) {return DateHelper.format(date, mFormat6chars);}// -----------------------------------------------------------------------// convenience method returns long friendly formatted timestamppublic static String format8chars(Object date) {return DateHelper.format(date, mFormat8chars);}// -----------------------------------------------------------------------// convenience method returns long friendly formatted timestamppublic static String formatIso8601Day(Object date) {return DateHelper.format(date, mFormatIso8601Day);}// -----------------------------------------------------------------------public static String formatRfc822(Object date) {return DateHelper.format(date, mFormatRfc822);}// -----------------------------------------------------------------------// This is a hack, but it seems to workpublic static String formatIso8601(Object date) {if (date == null)return "";// Add a colon 2 chars before the end of the string// to make it a valid ISO-8601 date.String str = DateHelper.format(date, mFormatIso8601);// StringBuffer sb = new StringBuffer();// sb.append(str.substring(0, str.length() - 2));// sb.append(":");// sb.append(str.substring(str.length() - 2));// return sb.toString();return str;}public static Date getDate() {return new Date();}public static Date getDate(long date) {return new Date(date);}public static long getTime() {return new Date().getTime();}public static long getTime(Date date) {return date.getTime();}public static long getTime(String dateStr, String format)throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat(format);Date date = sdf.parse(dateStr);return getTime(date);}public static long getTime(String dateStr) throws ParseException {return getTime(DateHelper.parse(dateStr, DateHelper.mFormatIso8601Day));}public static String format(long time, String formatStr) {if (time == 0)return "";SimpleDateFormat df = new SimpleDateFormat(formatStr);Date date = new Date(time);return df.format(date);}public static String format(Object date, String formatStr) {if (date == null)return "";SimpleDateFormat df = new SimpleDateFormat(formatStr);return df.format(date);}public static String format(long time) {return DateHelper.formatIso8601Day(new Date(time));}public static String format(Date time, String formatStr)throws ParseException {if (time == null)return "";long longTime = Long.parseLong(time.toString());if (longTime == 0)return "";SimpleDateFormat sdf = new SimpleDateFormat(formatStr);Date date = new Date(longTime);return sdf.format(date);}public static String format(Long time) throws ParseException {return DateHelper.format(time.longValue());}public static String format(Date date) {return DateHelper.formatIso8601Day(date);}public static String today() {return DateHelper.format(new Date());}public static String today(String formatStr) throws ParseException {return DateHelper.format(getTime(today()), formatStr);}public static String now() {return DateHelper.formatRfc822(new Date());}public static String now(String formatStr) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat(formatStr);return sdf.format(new Date());}public static int get(int field) {GregorianCalendar calendar = new GregorianCalendar();int reValue = 0;switch (field) {case YEAR:reValue = calendar.get(YEAR);break;case MONTH:reValue = calendar.get(MONTH) + 1;break;case DATE:reValue = calendar.get(DATE);break;case HOUR:reValue = calendar.get(HOUR);break;case HOUR24:reValue = calendar.get(HOUR24);break;case MINUTE:reValue = calendar.get(MINUTE);break;case SECOND:reValue = calendar.get(SECOND);break;case DAY_OF_WEEK:reValue = calendar.get(DAY_OF_WEEK);break;case WEEK_OF_YEAR:reValue = calendar.get(WEEK_OF_YEAR);break;default:reValue = 0;}return reValue;}public static int get(int field,Date date) {GregorianCalendar calendar = new GregorianCalendar();calendar.setTime(date);int reValue = 0;switch (field) {case YEAR:reValue = calendar.get(YEAR);break;case MONTH:reValue = calendar.get(MONTH) + 1;break;case DATE:reValue = calendar.get(DATE);break;case HOUR:reValue = calendar.get(HOUR);break;case HOUR24:reValue = calendar.get(HOUR24);break;case MINUTE:reValue = calendar.get(MINUTE);break;case SECOND:reValue = calendar.get(SECOND);break;case DAY_OF_WEEK:reValue = calendar.get(DAY_OF_WEEK);break;case WEEK_OF_YEAR:reValue = calendar.get(WEEK_OF_YEAR);break;default:reValue = 0;}return reValue;}public static void main(String[] args) throws Exception {System.out.println(DateHelper.getNoonOfDay(new Date()));System.out.println(format(DateHelper.getTime("2001-09-02 12:56")));System.out.println(DateHelper.get(DateHelper.YEAR));System.out.println(DateHelper.get(DateHelper.MONTH));System.out.println(DateHelper.get(DateHelper.DATE));}} |
|