📄 dateutil.java
字号:
/**
* =============================================
* Copyright 2005 TransFar
*
* Change Revision
* --------------------------------
* Date Author Remarks
* Nov 29, 2005
* =============================================
*/
package com.szmx.framework.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.TimeZone;
import com.szmx.tlms.GlobalConstants;
public class DateUtil
{
/**
* A log instance for this class
*/
private static Log log = LogFactory.getLog(DateUtil.class);
/**
* "JAN" to represent January
*/
public static final String MTH_JAN = "Jan";
/**
* "FEB" to represent February
*/
public static final String MTH_FEB = "Feb";
/**
* "MAR" to represent March
*/
public static final String MTH_MAR = "Mar";
/**
* "APR" to represent April
*/
public static final String MTH_APR = "Apr";
/**
* "MAY" to represent May
*/
public static final String MTH_MAY = "May";
/**
* "JUNE" to represent June
*/
public static final String MTH_JUN = "Jun";
/**
* "JUL" to represent July
*/
public static final String MTH_JUL = "Jul";
/**
* "AUG" to represent August
*/
public static final String MTH_AUG = "Aug";
/**
* "SEP" to represent September
*/
public static final String MTH_SEP = "Sep";
/**
* "OCT" to represent October
*/
public static final String MTH_OCT = "Oct";
/**
* "NOV" to represent November
*/
public static final String MTH_NOV = "Nov";
/**
* "DEC" to represent December
*/
public static final String MTH_DEC = "Dec";
/**
* The date pattern in this format 'ddMMyyyy HHmm'
*/
public static String DATETIME_FORMAT = "ddMMyyyy HHmm";
public static String DATE_FORMAT = "dd MMM yyyy";
public static String TIMESTAMP_FORMAT = "dd MMM yyyy HH:mm:ss";
public static String DATE_FORMAT_YYYYMMDD = "yyyy-MM-dd";
public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
/**
* Returns a date object from input string indicating year, month and day
*
* @param year Year Indicator
* @param month Month indicator, 1=jan 2=feb ...
* @param day Date indicator eg: any day from 1...31.
* @return date java.util.Date object in millisecond.
* @since 15/05/2000
*/
public static Date getDate(int year, int month, int day)
{
Calendar cal = Calendar.getInstance();
cal.set(year, month - 1, day, 0, 0, 0);
return cal.getTime();
}
/**
* Compares the 2 dates: Returns true if the 2 dates are equal.
*
* @param date1 Date to compare
* @param date2 Date to compare
* @return true if <code>date1</code> equals to <code>date2</code>.
* @since 24/04/2001
*/
public static boolean isDateEqual(Date date1, Date date2)
{
if((date1 == null) || (date2 == null))
{
return false;
}
return resetTime(date1).compareTo(resetTime(date2)) == 0;
}
/**
* Sets the default timezone to the specified timezone ID. Note: The
* system time will remain unchanged. Only the Time zone for the current
* thread is set.
*
* @param timeZoneID The timezone ID. Example: "America/Los_Angeles", "CCT"
* which stands for China/Taiwan = S'pore
*/
public static void setDefaultTimeZone(String timeZoneID)
{
TimeZone.setDefault(TimeZone.getTimeZone(timeZoneID));
}
/**
* Calculates the elapsed time between 2 dates. The elapsed time calculated
* could either be in years, months or days
*
* @param type (int) The variable type determines the calculation of the
* elapsed time to be based on either years, months or days. To
* compute the elapsed time in year input type set to Calendar.YEAR
* To compute the elapsed time in month input type set to
* Calendar.MONTH By default the elapsed time will compute in days
* @param startDate start date
* @param endDate end date
* @return the elapsed time (int)
*/
public static int getElapsedTime(int type, Date startDate,
Date endDate)
{
int elapsed = 0;
if((startDate == null) || (endDate == null))
{
return -1;
}
if(startDate.after(endDate))
{
return -1;
}
GregorianCalendar gc1 = (GregorianCalendar) GregorianCalendar.getInstance();
GregorianCalendar gc2 = (GregorianCalendar) gc1.clone();
gc1.setTime(startDate);
gc2.setTime(endDate);
gc1.clear(Calendar.MILLISECOND);
gc1.clear(Calendar.SECOND);
gc1.clear(Calendar.MINUTE);
gc1.clear(Calendar.HOUR_OF_DAY);
gc2.clear(Calendar.MILLISECOND);
gc2.clear(Calendar.SECOND);
gc2.clear(Calendar.MINUTE);
gc2.clear(Calendar.HOUR_OF_DAY);
if((type != Calendar.MONTH) && (type != Calendar.YEAR))
{
type = Calendar.DATE;
}
if(type == Calendar.MONTH)
{
gc1.clear(Calendar.DATE);
gc2.clear(Calendar.DATE);
}
if(type == Calendar.YEAR)
{
gc1.clear(Calendar.DATE);
gc2.clear(Calendar.DATE);
gc1.clear(Calendar.MONTH);
gc2.clear(Calendar.MONTH);
}
while(gc1.before(gc2))
{
gc1.add(type, 1);
elapsed++;
}
return elapsed;
}
/**
* This method will determine if the date is the last day of the month
*
* @param date date
* @return returns true if the date falls on the last day of the month else
* returns false
*/
public static boolean isEndOfTheMonth(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
return cal.get(Calendar.DATE) == maxDay;
}
/**
* This method will determine if the date is the last day of the year
*
* @param date date
* @return returns true if the date falls on the last day of the year else
* returns false
*/
public static boolean isEndOfTheYear(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return (cal.get(Calendar.MONTH) == 11) && (cal.get(Calendar.DATE) == 31);
}
/**
* This method will return the last day of the months
*
* @param date date
* @return returns the last day of the month
*/
public static int getLastDayOfTheMonth(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
/**
* Returns the numeric value of the specified month
*
* @param month The 3 letter month representation. Example: "Jan", "Feb", etc
* @return the numeric value of the specified month
*/
public static int getMthInInt(String month)
{
if(month.equalsIgnoreCase(MTH_JAN))
{
return 1;
} else if(month.equalsIgnoreCase(MTH_FEB))
{
return 2;
} else if(month.equalsIgnoreCase(MTH_MAR))
{
return 3;
} else if(month.equalsIgnoreCase(MTH_APR))
{
return 4;
} else if(month.equalsIgnoreCase(MTH_MAY))
{
return 5;
} else if(month.equalsIgnoreCase(MTH_JUN))
{
return 6;
} else if(month.equalsIgnoreCase(MTH_JUL))
{
return 7;
} else if(month.equalsIgnoreCase(MTH_AUG))
{
return 8;
} else if(month.equalsIgnoreCase(MTH_SEP))
{
return 9;
} else if(month.equalsIgnoreCase(MTH_OCT))
{
return 10;
} else if(month.equalsIgnoreCase(MTH_NOV))
{
return 11;
} else if(month.equalsIgnoreCase(MTH_DEC))
{
return 12;
} else
{
return 0;
}
}
/**
* Return the date of the next working day
*
* @return the date of the next working day
*/
public static Date getNextWorkingDay()
{
Date nextWorkingDay = DateUtil.addDaysToDate(DateUtil.getSystemDate(), 1);
Calendar c = Calendar.getInstance();
c.setTime(nextWorkingDay);
int day = c.get(Calendar.DAY_OF_WEEK);
if(day == Calendar.SUNDAY)
{
nextWorkingDay = DateUtil.addDaysToDate(nextWorkingDay, 1);
}
return nextWorkingDay;
}
/**
* Compares the 2 dates: Returns true if the start date is before the end
* date.
*
* @param startDate Starting date of a particular time period.
* @param endDate Ending date of a particular time period.
* @return true if the <code>startDate</code> is before
* <code>endDate</code>.
* @since 24/03/2001
*/
public static boolean isStartBeforeEndDate(Date startDate, Date endDate)
{
if((startDate == null) || (endDate == null))
{
return false;
}
return resetTime(startDate).compareTo(resetTime(endDate)) < 0;
}
/**
* This method will determine if the date occurs on the beginning of the
* month
*
* @param date date
* @return returns true if date is on the beginning of the month else
* returns false
*/
public static boolean isStartOfTheMonth(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.DATE) == 1;
}
/**
* This method will return
* month
*
* @param date date
* @return returns month in int
*/
public static int getMonth(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.MONTH);
}
public static int getYear(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
/**
* This method will determine if the date occurs at the beginning of the
* year
*
* @param date date
* @return returns true if the date occurs on the beginning of the year
* else returns false
*/
public static boolean isStartOfTheYear(Date date)
{
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return (cal.get(Calendar.MONTH) == 1) && (cal.get(Calendar.DATE) == 1);
}
/**
* Returns the corresponding 3 letter string value of the month specified by numeric month value
*
* @param month The numeric value of the specified month
* @return the corresponding 3 letter string value of the month specified by numeric month value
*/
public static String getStrMth(int month)
{
if(month == 1)
{
return MTH_JAN;
} else if(month == 2)
{
return MTH_FEB;
} else if(month == 3)
{
return MTH_MAR;
} else if(month == 4)
{
return MTH_APR;
} else if(month == 5)
{
return MTH_MAY;
} else if(month == 6)
{
return MTH_JUN;
} else if(month == 7)
{
return MTH_JUL;
} else if(month == 8)
{
return MTH_AUG;
} else if(month == 9)
{
return MTH_SEP;
} else if(month == 10)
{
return MTH_OCT;
} else if(month == 11)
{
return MTH_NOV;
} else if(month == 12)
{
return MTH_DEC;
} else
{
return "";
}
}
/**
* Calculates the duration in years, months and days.
*
* @param startDate Start Date of a period.
* @param endDate End date of a period.
* @return int [] result [0]=duration in years, [1]=duration in months,
* [2]=duration in days.
*/
public static int[] computeDuration(Date startDate, Date endDate)
{
Calendar from = Calendar.getInstance();
Calendar to = Calendar.getInstance();
from.setTime(startDate);
to.setTime(endDate);
int birthYYYY = from.get(Calendar.YEAR);
int birthMM = from.get(Calendar.MONTH);
int birthDD = from.get(Calendar.DAY_OF_MONTH);
int asofYYYY = to.get(Calendar.YEAR);
int asofMM = to.get(Calendar.MONTH);
int asofDD = to.get(Calendar.DAY_OF_MONTH);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -