package ftp;
import java.io.File;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class DateUtil {
private static Log log = LogFactory.getLog(DateUtil.class);
private static String defaultDatePattern = "yyyy-MM-dd HH:mm:ss";
private static String datePattern = "yyyyMMdd";
public static final int START_DATE = 1;
public static final int END_DATE = 2;
private static String timePattern = "HH:mm";
private static String timePattern2 = "HH:mm:ss";
public static Date getFirstDayOfMonth(Date date) {
return getCalendarDate(START_DATE, date, true);
}
public static Date getLastDayOfMonth(Date date) {
return getCalendarDate(END_DATE, date, true);
}
/**
* 取得日程管理查询日期 for spp
*
* @param type
* @param date
* @param tuncate
* @return
*/
private static Date getCalendarDate(int type, Date date, boolean truncate) {
Calendar rightNow = Calendar.getInstance(Locale.CHINA);
rightNow.setTime(date);
int base = 1;
switch (type) {
case END_DATE:
base = 0;
rightNow.add(Calendar.MONTH, 1);
if (!truncate) {
break;
}
case START_DATE:
rightNow.add(Calendar.DAY_OF_MONTH, base
- rightNow.get(Calendar.DAY_OF_MONTH));
break;
default:
break;
}
return rightNow.getTime();
}
/**
* Return default datePattern (MM/dd/yyyy)
*
* @return a string representing the date pattern on the UI
*/
public static String getDefaultDatePattern() {
return defaultDatePattern;
}
public static String getDatePattern() {
return datePattern;
}
/**
* This method attempts to convert an Oracle-formatted date in the form
* dd-MMM-yyyy to mm/dd/yyyy.
*
* @param aDate
* date from database as a string
* @return formatted string for the ui
*/
public static final String getDate(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(getDefaultDatePattern());
returnValue = df.format(aDate);
}
return (returnValue);
}
public static final String getDate_yyyyMMdd(Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate != null) {
df = new SimpleDateFormat(getDatePattern());
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* This method generates a string representation of a date/time in the
* format you specify on input
*
* @param aMask
* the date pattern the string is in
* @param strDate
* a string representation of a date
* @return a converted Date object
* @see java.text.SimpleDateFormat
* @throws ParseException
*/
public static final Date convertStringToDate(String aMask, String strDate)
throws ParseException {
SimpleDateFormat df = null;
Date date = null;
df = new SimpleDateFormat(aMask);
if (log.isDebugEnabled()) {
log.debug("converting '" + strDate + "' to date with mask '"
+ aMask + "'");
}
try {
date = df.parse(strDate);
} catch (ParseException pe) {
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return (date);
}
/**
*
* @param date
* @return
*/
public static final java.sql.Date convertUtilDateToSqlDate(
java.util.Date date) {
java.sql.Date current = new java.sql.Date(date.getTime());
return current;
}
/**
* This method returns the current date time in the format: MM/dd/yyyy HH:MM
* a
*
* @param theTime
* the current time
* @return the current date/time
*/
public static String getTimeNow(Date theTime) {
return getDateTime(timePattern, theTime);
}
//
public static String getHMS(Date theTime) {
return getDateTime(timePattern2, theTime);
}
/**
* This method returns the current date in the format: MM/dd/yyyy
*
* @return the current date
* @throws ParseException
*/
public static Calendar getToday() throws ParseException {
Date today = new Date();
SimpleDateFormat df = new SimpleDateFormat(getDefaultDatePattern());
String todayAsString = df.format(today);
Calendar cal = new GregorianCalendar();
cal.setTime(convertStringToDate(todayAsString));
return cal;
}
/**
* This method generates a string representation of a date's date/time in
* the format you specify on input
*
* @param aMask
* the date pattern the string is in
* @param aDate
* a date object
* @return a formatted string representation of the date
*
* @see java.text.SimpleDateFormat
*/
public static final String getDateTime(String aMask, Date aDate) {
SimpleDateFormat df = null;
String returnValue = "";
if (aDate == null) {
log.error("aDate is null!");
} else {
df = new SimpleDateFormat(aMask);
returnValue = df.format(aDate);
}
return (returnValue);
}
/**
* This method generates a string representation of a date based on the
* System Property 'dateFormat' in the format you specify on input
*
* @param aDate
* A date to convert
* @return a string representation of the date
*/
public static final String convertDateToString(Date aDate) {
return getDateTime(getDefaultDatePattern(), aDate);
}
/**
* This method converts a String to a date using the datePattern
*
* @param strDate
* the date to convert (in format yyyy-mm-dd)
* @return a date object
*
* @throws ParseException
*/
public static Date convertStringToDate(String strDate)
throws ParseException {
Date aDate = null;
try {
if (log.isDebugEnabled()) {
log.debug("converting date with pattern: "
+ getDefaultDatePattern());
}
aDate = convertStringToDate(getDefaultDatePattern(), strDate);
} catch (ParseException pe) {
log.error("Could not convert '" + strDate
+ "' to a date, throwing exception");
pe.printStackTrace();
throw new ParseException(pe.getMessage(), pe.getErrorOffset());
}
return aDate;
}
private static final SimpleDateFormat mFormat8chars = new SimpleDateFormat(
"yyyyMMdd");
private static final SimpleDateFormat mFormatIso8601Day = new SimpleDateFormat(
"yyyy-MM-dd");
private static final SimpleDateFormat mFormatZh = new SimpleDateFormat(
"yyyy年MM月dd日");
private static final SimpleDateFormat mFormatIso8601 = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ssZ");
private static final SimpleDateFormat mFormatRfc822 = new SimpleDateFormat(
"EEE, d MMM yyyy HH:mm:ss z");
/**
* 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(Date aDate, SimpleDateFormat aFormat) {
if (aDate == null || aFormat == null) {
return "";
}
return aFormat.format(aDate);
}
public static String format(Date aDate, String aFormat) {
if (aDate == null || aFormat == null) {
return "";
}
return new SimpleDateFormat(aFormat).format(aDate);
}
public static String getDefaultDateFormat(Date aDate) {
return DateUtil.format(aDate, mFormatIso8601Day);
}
public static String getTimeFormat(Date aDate) {
return DateUtil.format(aDate, mFormatIso8601);
}
/**
* 获取中文格式的日期格式2007年11月08日
*
* @param data
* @return
*/
public static String getZhTimeFormat(Date data) {
return DateUtil.format(data, mFormatZh);
}
public static String getDefaultDateFormat(long aDate) {
Date date = new Date();
date.set
评论30
最新资源