Java tutorial
/* * Copyright (C) 2008 feilong * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.feilong.core.date; import java.util.Calendar; import java.util.Date; import org.apache.commons.lang3.Validate; import com.feilong.core.DatePattern; /** * {@link DateUtil},??? . * * <h3>Calendar?:</h3> * * <blockquote> * Calendar,?17,?? {@link java.util.Calendar#FIELD_COUNT} * * <table border="1" cellspacing="0" cellpadding="4" summary=""> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * <tr valign="top"> * <td></td> * <td></td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#ERA}</td> * <td>?0 1.0BC("before Christ",??),1AD(?"Anno Domini",?)</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#YEAR}</td> * <td></td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#MONTH}</td> * <td> ?:?,JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER, UNDECIMBER. * JANUARY, 0</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#WEEK_OF_YEAR}</td> * <td>?. 1</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#WEEK_OF_MONTH}</td> * <td>?. 1</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#DATE}</td> * <td>. 1</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#DAY_OF_MONTH}</td> * <td>?"DATE",""</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#DAY_OF_YEAR}</td> * <td>?. 1.</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#DAY_OF_WEEK}</td> * <td>. ?:?,SUNDAY?MONDAY?TUESDAY?WEDNESDAY?THURSDAY?FRIDAY SATURDAY. ,SUNDAY1,MONDAY2,?.</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#DAY_OF_WEEK_IN_MONTH}</td> * <td>?. ?:DAY_OF_MONTH 1 7 DAY_OF_WEEK_IN_MONTH 1;8 14 DAY_OF_WEEK_IN_MONTH 2,?. <br> * DAY_OF_WEEK_IN_MONTH ?. DAY_OF_WEEK ,???. <br> * WEEK_OF_MONTH WEEK_OF_YEAR ??,? ? getFirstDayOfWeek() getMinimalDaysInFirstWeek(). <br> * DAY_OF_MONTH 1 7 DAY_OF_WEEK_IN_MONTH 1; <br> * 8 14 DAY_OF_WEEK_IN_MONTH 2,?. <br> * DAY_OF_WEEK_IN_MONTH 0 DAY_OF_WEEK_IN_MONTH 1 ?. <br> * ?,,? DAY_OF_WEEK = SUNDAY, DAY_OF_WEEK_IN_MONTH = -1. <br> * ?,????. <br> * , 31 , DAY_OF_WEEK_IN_MONTH -1 DAY_OF_WEEK_IN_MONTH 5 DAY_OF_WEEK_IN_MONTH 4 ??</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#AM_PM}</td> * <td>? ? ?:?AM PM.AM0,?;PM1,?.</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#HOUR}</td> * <td>?. HOUR 12 ? (0 - 11).?? 0 ,? 12 </td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#HOUR_OF_DAY}</td> * <td>?. HOUR_OF_DAY 24 ?., 10:04:15.250 PM ,HOUR_OF_DAY 22</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#MINUTE}</td> * <td>?. , 10:04:15.250 PM,MINUTE 4</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#SECOND}</td> * <td>. , 10:04:15.250 PM ,SECOND 15</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#MILLISECOND}</td> * <td>. , 10:04:15.250 PM ,MILLISECOND 250.</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#ZONE_OFFSET}</td> * <td>??? GMT ???</td> * </tr> * * <tr valign="top"> * <td>{@link Calendar#DST_OFFSET}</td> * <td>??????.</td> * </tr> * * </table> * </blockquote> * * <h3>{@link Calendar#getActualMaximum(int)} VS {@link Calendar#getMaximum(int)}</h3> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4" summary=""> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left"></th> * </tr> * * <tr valign="top"> * <td>{@link Calendar#getMaximum(int)}</td> * <td>?"",?,"".<br> * ,getMaximum(Calendar.DATE)"?".?,31.,getMaximum(Calendar.DATE)"31"?</td> * </tr> * * <tr valign="top" style="background-color:#eeeeff"> * <td>{@link Calendar#getActualMaximum(int)}</td> * <td>?"?,".<br> * ,2013-09-01,getActualMaximum(Calendar.DATE)?"""30".?9,9?30.,getActualMaximum(Calendar.DATE)"30"?</td> * </tr> * * </table> * </blockquote> * * @author <a href="http://feitianbenyue.iteye.com/">feilong</a> * @see DateUtil * @since 1.0.1 */ final class CalendarUtil { /** Don't let anyone instantiate this class. */ private CalendarUtil() { //AssertionError?. ?????. ???. //see Effective Java 2nd throw new AssertionError("No " + getClass().getName() + " instances for you!"); } // [start] /** * ,<code>00:00:00.000</code> * * @param calendar * the calendar * @return <code>calendar</code> null, {@link NullPointerException} * @see Calendar#set(int, int) * @see Calendar#HOUR_OF_DAY * @see Calendar#MINUTE * @see Calendar#SECOND * @see Calendar#MILLISECOND * @see org.apache.commons.lang3.time.DateUtils#truncate(Calendar, int) * @since 1.3.0 */ static Calendar resetDayBegin(Calendar calendar) { Validate.notNull(calendar, "calendar can't be null!"); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); return calendar; } /** * ?,? <code>23:59:59.999</code> * * @param calendar * the calendar * @return <code>calendar</code> null, {@link NullPointerException} * @see Calendar#set(int, int) * @see Calendar#HOUR_OF_DAY * @see Calendar#MINUTE * @see Calendar#SECOND * @see Calendar#MILLISECOND * @since 1.3.0 */ static Calendar resetDayEnd(Calendar calendar) { Validate.notNull(calendar, "calendar can't be null!"); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MILLISECOND, 999); return calendar; } /** * ?,? <code>1231? 23:59:59.999</code> * * @param calendar * the calendar * @return <code>calendar</code> null, {@link NullPointerException} * @since 1.3.0 * @see #resetDayEnd(Calendar) */ static Calendar resetYearEnd(Calendar calendar) { Validate.notNull(calendar, "calendar can't be null!"); calendar.set(Calendar.MONTH, Calendar.DECEMBER); calendar.set(Calendar.DAY_OF_MONTH, 31); return resetDayEnd(calendar); } // [end] //************************************************************************************** /** * {@link Calendar} ? {@link Date}. * * @param calendar * calendar * @return <code>calendar</code> null, {@link NullPointerException} * @see java.util.Calendar#getTime() */ static Date toDate(Calendar calendar) { Validate.notNull(calendar, "calendar can't be null!"); return calendar.getTime(); } /** * {@link Calendar}?{@link String}. * * @param calendar * calendar * @param datePattern * pattern {@link DatePattern} * @return <code>calendar</code> null, {@link NullPointerException} * @see com.feilong.core.date.DateUtil#toString(Date, String) */ static String toString(Calendar calendar, String datePattern) { Date date = toDate(calendar); return DateUtil.toString(date, datePattern); } //********************************************************************************************* /** * . * * @param date * date * @param field * Calendar:<br> * :{@link Calendar#MONTH}(?1?),<br> * :{@link Calendar#DAY_OF_MONTH},<br> * :{@link Calendar#YEAR}<br> * ... * @return <code>calendar</code> null, {@link NullPointerException}<br> * @see #getFieldValue(Calendar, int) * @since 1.3.0 */ static int getFieldValue(Date date, int field) { Calendar calendar = DateUtil.toCalendar(date); return getFieldValue(calendar, field); } /** * . * * @param calendar * the calendar * @param field * Calendar:<br> * :{@link Calendar#MONTH}(?1?),<br> * :{@link Calendar#DAY_OF_MONTH},<br> * :{@link Calendar#YEAR}<br> * ... * @return <code>calendar</code> null, {@link NullPointerException}<br> * @see java.util.Calendar#get(int) * @since 1.3.0 */ static int getFieldValue(Calendar calendar, int field) { Validate.notNull(calendar, "calendar can't be null!"); return calendar.get(field); } }