📄 dateutils.java
字号:
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.commons.lang.time;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.TimeZone;
/**
* <p>A suite of utilities surrounding the use of the
* {@link java.util.Calendar} and {@link java.util.Date} object.</p>
*
* @author <a href="mailto:sergek@lokitech.com">Serge Knystautas</a>
* @author Stephen Colebourne
* @author Janek Bogucki
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 2.0
* @version $Id: DateUtils.java,v 1.16 2003/08/18 21:52:39 ggregory Exp $
*/
public class DateUtils {
/**
* The UTC time zone (often referred to as GMT).
*/
public static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("GMT");
/**
* Number of milliseconds in a standard second.
*/
public static final int MILLIS_IN_SECOND = 1000;
/**
* Number of milliseconds in a standard minute.
*/
public static final int MILLIS_IN_MINUTE = 60 * 1000;
/**
* Number of milliseconds in a standard hour.
*/
public static final int MILLIS_IN_HOUR = 60 * 60 * 1000;
/**
* Number of milliseconds in a standard day.
*/
public static final int MILLIS_IN_DAY = 24 * 60 * 60 * 1000;
/**
* This is half a month, so this represents whether a date is in the top
* or bottom half of the month.
*/
public final static int SEMI_MONTH = 1001;
private static final int[][] fields = {
{Calendar.MILLISECOND},
{Calendar.SECOND},
{Calendar.MINUTE},
{Calendar.HOUR_OF_DAY, Calendar.HOUR},
{Calendar.DATE, Calendar.DAY_OF_MONTH, Calendar.AM_PM /* Calendar.DAY_OF_YEAR, Calendar.DAY_OF_WEEK, Calendar.DAY_OF_WEEK_IN_MONTH */},
{Calendar.MONTH, DateUtils.SEMI_MONTH},
{Calendar.YEAR},
{Calendar.ERA}};
/**
* A week range, starting on Sunday.
*/
public final static int RANGE_WEEK_SUNDAY = 1;
/**
* A week range, starting on Monday.
*/
public final static int RANGE_WEEK_MONDAY = 2;
/**
* A week range, starting on the day focused.
*/
public final static int RANGE_WEEK_RELATIVE = 3;
/**
* A week range, centered around the day focused.
*/
public final static int RANGE_WEEK_CENTER = 4;
/**
* A month range, the week starting on Sunday.
*/
public final static int RANGE_MONTH_SUNDAY = 5;
/**
* A month range, the week starting on Monday.
*/
public final static int RANGE_MONTH_MONDAY = 6;
/**
* <p><code>DateUtils</code> instances should NOT be constructed in
* standard programming. Instead, the class should be used as
* <code>DateUtils.parse(str);</code>.</p>
*
* <p>This constructor is public to permit tools that require a JavaBean
* instance to operate.</p>
*/
public DateUtils() {
}
//-----------------------------------------------------------------------
/**
* <p>Round this date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the datetime of 28 Mar 2002
* 13:45:01.231, if this was passed with HOUR, it would return
* 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it
* would return 1 April 2002 0:00:00.000.</p>
*
* @param date the date to work with
* @param field the field from <code>Calendar</code>
* or <code>SEMI_MONTH</code>
* @return the rounded date
* @throws IllegalArgumentException if the date is <code>null</code>
*/
public static Date round(Date date, int field) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
GregorianCalendar gval = new GregorianCalendar();
gval.setTime(date);
modify(gval, field, true);
return gval.getTime();
}
/**
* <p>Round this date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the datetime of 28 Mar 2002
* 13:45:01.231, if this was passed with HOUR, it would return
* 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it
* would return 1 April 2002 0:00:00.000.</p>
*
* @param date the date to work with
* @param field the field from <code>Calendar</code>
* or <code>SEMI_MONTH</code>
* @return the rounded date (a different object)
* @throws IllegalArgumentException if the date is <code>null</code>
*/
public static Calendar round(Calendar date, int field) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar rounded = (Calendar) date.clone();
modify(rounded, field, true);
return rounded;
}
/**
* <p>Round this date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the datetime of 28 Mar 2002
* 13:45:01.231, if this was passed with HOUR, it would return
* 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it
* would return 1 April 2002 0:00:00.000.</p>
*
* @param date the date to work with, either Date or Calendar
* @param field the field from <code>Calendar</code>
* or <code>SEMI_MONTH</code>
* @return the rounded date
* @throws IllegalArgumentException if the date is <code>null</code>
* @throws ClassCastException if the object type is not a <code>Date</code>
* or <code>Calendar</code>
*/
public static Date round(Object date, int field) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
if (date instanceof Date) {
return round((Date) date, field);
} else if (date instanceof Calendar) {
return round((Calendar) date, field).getTime();
} else {
throw new ClassCastException("Could not round " + date);
}
}
//-----------------------------------------------------------------------
/**
* <p>Truncate this date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the datetime of 28 Mar 2002
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
* 2002 13:00:00.000. If this was passed with MONTH, it would
* return 1 Mar 2002 0:00:00.000.</p>
*
* @param date the date to work with
* @param field the field from <code>Calendar</code>
* or <code>SEMI_MONTH</code>
* @return the rounded date
* @throws IllegalArgumentException if the date is <code>null</code>
*/
public static Date truncate(Date date, int field) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
GregorianCalendar gval = new GregorianCalendar();
gval.setTime(date);
modify(gval, field, false);
return gval.getTime();
}
/**
* <p>Truncate this date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the datetime of 28 Mar 2002
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
* 2002 13:00:00.000. If this was passed with MONTH, it would
* return 1 Mar 2002 0:00:00.000.</p>
*
* @param date the date to work with
* @param field the field from <code>Calendar</code>
* or <code>SEMI_MONTH</code>
* @return the rounded date (a different object)
* @throws IllegalArgumentException if the date is <code>null</code>
*/
public static Calendar truncate(Calendar date, int field) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
Calendar truncated = (Calendar) date.clone();
modify(truncated, field, false);
return truncated;
}
/**
* <p>Truncate this date, leaving the field specified as the most
* significant field.</p>
*
* <p>For example, if you had the datetime of 28 Mar 2002
* 13:45:01.231, if you passed with HOUR, it would return 28 Mar
* 2002 13:00:00.000. If this was passed with MONTH, it would
* return 1 Mar 2002 0:00:00.000.</p>
*
* @param date the date to work with, either <code>Date</code>
* or <code>Calendar</code>
* @param field the field from <code>Calendar</code>
* or <code>SEMI_MONTH</code>
* @return the rounded date
* @throws IllegalArgumentException if the date
* is <code>null</code>
* @throws ClassCastException if the object type is not a
* <code>Date</code> or <code>Calendar</code>
*/
public static Date truncate(Object date, int field) {
if (date == null) {
throw new IllegalArgumentException("The date must not be null");
}
if (date instanceof Date) {
return truncate((Date) date, field);
} else if (date instanceof Calendar) {
return truncate((Calendar) date, field).getTime();
} else {
throw new ClassCastException("Could not truncate " + date);
}
}
//-----------------------------------------------------------------------
/**
* <p>Internal calculation method.</p>
*
* @param val the calendar
* @param field the field constant
* @param round true to round, false to truncate
*/
private static void modify(Calendar val, int field, boolean round) {
boolean roundUp = false;
for (int i = 0; i < fields.length; i++) {
for (int j = 0; j < fields[i].length; j++) {
if (fields[i][j] == field) {
//This is our field... we stop looping
if (round && roundUp) {
if (field == DateUtils.SEMI_MONTH) {
//This is a special case that's hard to generalize
//If the date is 1, we round up to 16, otherwise
// we subtract 15 days and add 1 month
if (val.get(Calendar.DATE) == 1) {
val.add(Calendar.DATE, 15);
} else {
val.add(Calendar.DATE, -15);
val.add(Calendar.MONTH, 1);
}
} else {
//We need at add one to this field since the
// last number causes us to round up
val.add(fields[i][0], 1);
}
}
return;
}
}
//We have various fields that are not easy roundings
int offset = 0;
boolean offsetSet = false;
//These are special types of fields that require different rounding rules
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -