很多地方都会用到Date日期处理,睿智平台当然也包括自己的dateUtil、stringutil、fileutil、IOUtil、NetUtil、XMLUtil等等大量的辅助开发工具类,这里先介绍一个大家常用的DateUtil
/**
package org.sagacity.framework.utils;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.sagacity.framework.log.Log;
import org.sagacity.framework.log.LogFactory;
/**
*
*@project sagacity-core
*@description:$<p>日期处理支持类,提供日常工作中的所有日期的操作处理</p>$
*@author chenrenfei $<a href="mailto:[email protected]">联系作者</a>$
*@version $id:DateUtil.java,Revision:v1.0,Date:2008-12-14 下午08:01:19 $
*/
public class DateUtil {
/**
* 定义日志
*/
protected final static Log logger = LogFactory.getFactory().getLog(
DateUtil.class);
private static final String[] CHINA_DATE_KEYS = { "○", "О", "0", "Ο", "O",
"零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "年", "月",
"日", "时", "分", "秒" };
private static final String[] CHINA_DATE_KEY_MAP = { "0", "0", "0", "0",
"0", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "-",
"-", "", ":", ":", "" };
private static String dateFormat = "yyyy-MM-dd";
private static DateFormat df = new SimpleDateFormat(dateFormat);
/**
* 英文月份名称
*/
private static final String[] MONTH_ENGLISH_NAME = { "January", "February",
"March", "April", "May", "June", "July", "August", "September",
"October", "November", "December" };
/**
* 中文星期的名称
*/
private static final String[] WEEK_CHINA_NAME = { "星期一", "星期二", "星期三",
"星期四", "星期五", "星期六", "星期日" };
/**
* 英文星期的名称
*/
private static final String[] WEEK_ENGLISH_NAME = { "Monday", "Tuesday",
"Wensday", "Tursday", "Friday", "Saturday", "Sunday" };
/**
* 定义日期的格式
*/
public final static class FORMAT {
/**
* 8位日期格式
*/
public final static String DATE_8CHAR = "yyyyMMdd";
/**
* 点号日期格式
*/
public final static String DATE_DOT = "yyyy.MM.dd";
/**
* 反斜杠日期格式
*/
public final static String DATE_SLASH = "yyyy/MM/dd";
/**
* 横杠日期格式
*/
public final static String DATEF_HORIZONTAL = "yyyy-MM-dd";
/**
* 日期时间(日期点格�?)
*/
public final static String DATATIME_DOT = "yyyy.MM.dd HH:mm:ss";
/**
* 日期时间(日期反斜杠)
*/
public final static String DATETIME_SLASH = "yyyy/MM/dd HH:mm:ss";
/**
* 日期时间(日期横杠)
*/
public final static String DATETIME_HORIZONTAL = "yyyy-MM-dd HH:mm:ss";
}
/**
* 将日期字符串或时间转换成时间类型 日期字符串中的日期分隔符可是:"/",".","-", 返回时间具体到秒 只提供常用的日期格式处理
*
* @param str
* @return Date
* @throws UtilException
*/
public static Date parseString(String dateString, String format) {
if (dateString == null || "".equals(dateString)
|| "null".equals(dateString.toLowerCase())) {
logger.error("The date string is null!");
return null;
}
try {
if (StringUtil.isNullOrBlank(format))
return parseString(dateString);
DateFormat df = new SimpleDateFormat(format);
return df.parse(dateString);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
*将日期字符串或时间转换成时间类型 日期字符串中的日期分隔符可是:"/",".","-", 返回时间具体到秒 只提供常用的日期格式处理
*
* @TODO TODO
*@param dateStr
*@return
*/
public static Date parseString(String dateStr) {
if (dateStr == null)
return null;
dateStr = dateStr.trim();
if (dateStr.equals(""))
return null;
DateFormat df;
try {
if (StringUtil.matchs(dateStr, "^\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}$"))
df = new SimpleDateFormat("HH:mm:ss");
else if (StringUtil.matchs(dateStr,
"^\\d{1,2}\\:\\d{1,2}\\:\\d{1,2}\\.\\d{1,3}$"))
df = new SimpleDateFormat("HH:mm:ss.SSS");
else if (StringUtil.matchs(dateStr,
"^\\d{1,2}\\.\\d{1,2}\\.\\d{1,2}$"))
df = new SimpleDateFormat("HH.mm.ss");
else if (StringUtil.matchs(dateStr, "^\\d{1,2}\\.\\d{1,2}$"))
df = new SimpleDateFormat("HH.mm");
else if (StringUtil.matchs(dateStr, "^\\d{1,2}\\:\\d{1,2}$"))
df = new SimpleDateFormat("HH:mm");
else {
dateStr = dateStr.replace("-", "/").replace(".", "/").replace(
":", "/").replace("\\", "/");
// 按概率高低排序
if (StringUtil
.matchs(dateStr, "^\\d{4}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yyyy/MM/dd");
else if (StringUtil
.matchs(dateStr,
"^\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss");
else if (StringUtil.matchs(dateStr, "^\\d{8}$"))
df = new SimpleDateFormat("yyyyMMdd");
else if (StringUtil.matchs(dateStr, "^\\d{8}\\s+\\d{6}$"))
df = new SimpleDateFormat("yyyyMMdd HHmmss");
else if (StringUtil.matchs(dateStr, "^\\d{4}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yyyy/MM");
else if (StringUtil.matchs(dateStr, "^\\d{6}$"))
df = new SimpleDateFormat("yyyyMM");
else if (StringUtil
.matchs(dateStr,
"^\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,3}$"))
df = new SimpleDateFormat("yyyy/MM/dd HH/mm/ss/SSS");
else if (StringUtil.matchs(dateStr,
"^\\d{2}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yy/MM/dd");
else if (StringUtil.matchs(dateStr, "^\\d{2}\\/\\d{2}$"))
df = new SimpleDateFormat("yy/MM");
else if (StringUtil
.matchs(dateStr,
"^\\d{2}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yy/MM/dd HH/mm/ss");
else if (StringUtil
.matchs(dateStr,
"^\\d{2}\\/\\d{1,2}\\/\\d{1,2}\\s+\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yy/MM/dd HH/mm");
else if (StringUtil
.matchs(dateStr,
"^\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss");
else if (StringUtil
.matchs(dateStr,
"^\\d{4}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,3}$"))
df = new SimpleDateFormat("yyyy/MM/dd/HH/mm/ss/SSS");
else if (StringUtil
.matchs(dateStr,
"^\\d{2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yy/MM/dd/HH/mm/ss");
else if (StringUtil.matchs(dateStr,
"^\\d{2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}\\/\\d{1,2}$"))
df = new SimpleDateFormat("yy/MM/dd/HH/mm");
else if (StringUtil.matchs(dateStr, "^\\d{6}\\s+\\d{6}$"))
df = new SimpleDateFormat("yyMMdd HHmmss");
else if (StringUtil.matchs(dateStr, "^\\d{7,9}$"))
df = new SimpleDateFormat("HHmmssSSS");
else if (StringUtil.matchs(dateStr, "^\\d{14}$"))
df = new SimpleDateFormat("yyyyMMddHHmmss");
else if (StringUtil.matchs(dateStr, "^\\d{15,17}$"))
df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
else if (StringUtil.matchs(dateStr, "^\\d{12}$"))
df = new SimpleDateFormat("yyMMddHHmmss");
else if (StringUtil.matchs(dateStr, "^\\d{4}$"))
df = new SimpleDateFormat("yyyy");
else
df = new SimpleDateFormat("yyyy/MM/dd");
}
return df.parse(dateStr);
} catch (java.text.ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 日期对象类型转换
*
* @param dt
* @return
*/
public static java.util.Date convertDateObject(Object dt) {
if (dt == null) {
System.err.println("日期不能为空,请正确输入!");
return null;
}
if (!(dt instanceof String) && !(dt instanceof java.sql.Date)
&& !(dt instanceof java.util.Date)
&& !(dt instanceof java.lang.Integer)
&& !(dt instanceof java.lang.Long))
throw new IllegalArgumentException(
"日期格式必须是String、Date、Long、Integer类型,请正确输入!");
Date result = null;
if (dt instanceof String)
result = parseString((String) dt);
else if (dt instanceof java.util.Date)
result = new java.util.Date(((java.util.Date) dt).getTime());
else if (dt instanceof java.sql.Date)
result = new java.util.Date(((java.sql.Date) dt).getTime());
else
result = parseString(dt.toString());
return result;
}
/**
* �?要改进增加非正常格式的日期转换功�?!�?20080718103020 �? 20080718 103020 将日期类型转换成字符�?
* 其格式包�?:通过DateFormat转换的标准格式,如yyyy/MM/dd HH:mm:ss或�?�yyyy-MM-dd HH:mm:ss
* 非标准的格式,如YYYY/MM/DD
*
* @param dt
* @param fmt
* @return String
* @throws UtilException
*/
public static String formatDate(Object dt, String fmt) {
if (dt == null)
return null;
DateFormat df = null;
if (fmt.equalsIgnoreCase("yyyy"))
return Integer.toString(getYear(dt));
else if (fmt.equalsIgnoreCase("MM"))
return Integer.toString(getMonth(dt));
else if (fmt.equalsIgnoreCase("dd"))
return Integer.toString(getDay(dt));
df = new SimpleDateFormat(fmt);
Date tmp = convertDateObject(dt);
if (tmp == null)
return null;
return df.format(tmp);
}
/**
* 获取当前操作系统的日期
*
* @return Date
*/
public static java.sql.Date getSqlDate() {
return getSqlDate(new java.util.Date());
}
/**
* 获取当前以sql.date的日期
*
* @param date
* @return
*/
public static java.sql.Date getSqlDate(Object date) {
if (date == null)
return null;
return new java.sql.Date(convertDateObject(date).getTime());
}
public static java.sql.Timestamp getTimestamp(Object date) {
Date tmp = convertDateObject(date);
return new Timestamp(tmp.getTime());
}
/**
* 获取当前操作系统的时间
*
* @return 当前操作系统的时间
*/
public static Date getNowTime() {
return Calendar.getInstance().getTime();
}
// Add millisecond
public static Date addMilliSecond(Object dt, long millisecond) {
Date tmp = convertDateObject(dt);
tmp.setTime(tmp.getTime() + millisecond);
return tmp;
}
public static Date addSecond(Object dt, long second) {
return addMilliSecond(dt, 1000L * second);
}
public static Date addSecond(Object dt, double second) {
Double millisecond = new Double(1000.0 * second);
return addMilliSecond(dt, millisecond.longValue());
}
public static Date addMinute(Object dt, long minute) {
return addMilliSecond(dt, 1000L * 60L * minute);
}
public static Date addMinute(Object dt, double minute) {
Double millisecond = new Double(1000.0 * 60.0 * minute);
return addMilliSecond(dt, millisecond.longValue());
}
public static Date addHour(Object dt, long hour) {
return addMilliSecond(dt, 1000L * 60L * 60L * hour);
}
public static Date addHour(Object dt, double hour) {
Double millisecond = new Double(1000.0 * 60.0 * 60.0 * hour);
return addMilliSecond(dt, millisecond.longValue());
}
public static Date addDay(Object dt, long day) {
return addMilliSecond(dt, 1000L * 60L * 60L * 24L * day);
}
public static Date addDay(Object dt, double day) {
Double millisecond = new Double(1000.0 * 60.0 * 60.0 * 24.0 * day);
return addMilliSecond(dt, millisecond.longValue());
}
// add month
public static Date addMonth(Object dt, int month) {
Date tmp = convertDateObject(dt);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(tmp);
gc.add(2, month);
return gc.getTime();
}
/**
* add year
*
* @param dt
* @param year
* @return
*/
public static Date addYear(Object dt, int year) {
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(convertDateObject(dt));
gc.add(1, year);
return gc.getTime();
}
/**
* @todo 在给定的日期上做年的加减运算
* @param dt
* @param year
* @return
*/
public static Date addYear(Object dt, double year) {
// 如果日期为空表示以当前日期为基数进行加减
Date tmp = convertDateObject(dt);
double floorYear = Math.floor(year);
double decimalYear = year - floorYear;
tmp = addYear(tmp, (new Double(floorYear)).intValue());
Calendar cal = Calendar.getInstance();
cal.setTime(tmp);
cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1);
Date nextdt = cal.getTime();
long yearMillisecond = nextdt.getTime() - tmp.getTime();
double millisecond = (double) yearMillisecond * decimalYear;
return addSecond(tmp, (long) millisecond);
}
public static int getYear() {
return getYear(null);
}
public static int getYear(Object dateValue) {
GregorianCalendar currentDate = new GregorianCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.YEAR);
}
public static String getShortYear(Object dateValue) {
int year = getYear(dateValue);
return Integer.toString(year).substring(2);
}
public static int getMonth() {
return getMonth(null);
}
/**
*
*@TODO 获取月份的英文名称
*@param dateValue
*@return
*/
public static String getMonthName(Object dateValue) {
return MONTH_ENGLISH_NAME[getMonth(dateValue) - 1];
}
public static int getMonth(Object dateValue) {
GregorianCalendar currentDate = new GregorianCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.MONTH) + 1;
}
public static int getDay() {
return getDay(null);
}
public static int getDay(Object dateValue) {
GregorianCalendar currentDate = new GregorianCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.DAY_OF_MONTH);
}
public static int getHour() {
return getHour(null);
}
public static int getHour(Object dateValue) {
GregorianCalendar currentDate = new GregorianCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute() {
return getMinute(null);
}
public static int getMinute(Object dateValue) {
GregorianCalendar currentDate = new GregorianCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.MINUTE);
}
public static int getSecond() {
return getSecond(null);
}
public static int getSecond(Object dateValue) {
GregorianCalendar currentDate = new GregorianCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.SECOND);
}
/**
* 获取当前日历
*
* @return
*/
public static GregorianCalendar getCalendar() {
return new GregorianCalendar();
}
public static int getDayOfWeek() {
return getDayOfWeek(null);
}
public static int getDayOfWeek(Object dateValue) {
GregorianCalendar currentDate = getCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
int week = currentDate.get(Calendar.DAY_OF_WEEK) - 1;
if (week == 0)
return 7;
return week;
}
/**
*
*@TODO 获取日期的中文名称
*@param dateValue
*@return
*/
public static String getDayOfWeekChinaName(Object dateValue) {
return WEEK_CHINA_NAME[getDayOfWeek(dateValue) - 1];
}
/**
*
*@TODO 获取日期的中文名称
*@param dateValue
*@return
*/
public static String getDayOfWeekEnglishName(Object dateValue) {
return WEEK_ENGLISH_NAME[getDayOfWeek(dateValue) - 1];
}
public static int getWeekOfMonth() {
return getWeekOfMonth(null);
}
/**
* 获取给定日期所在月的第几周
*
* @param dateValue
* @return
*/
public static int getWeekOfMonth(Object dateValue) {
GregorianCalendar currentDate = getCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.WEEK_OF_MONTH) - 1;
}
public static int getWeekOfYear() {
return getWeekOfYear(null);
}
/**
* 获取给定日期所在年的第几周
*
* @param dateValue
* @return
*/
public static int getWeekOfYear(Object dateValue) {
GregorianCalendar currentDate = getCalendar();
if (dateValue != null)
currentDate.setTime(convertDateObject(dateValue));
return currentDate.get(Calendar.WEEK_OF_YEAR) - 1;
}
/**
* @todo 获取相隔两个时间的周数
* @param floorDate
* @param goalDate
* @return
*/
public static double getIntervalWeeks(Object floorDate, Object goalDate) {
return getIntervalHours(floorDate, goalDate) / (7 * 24);
}
/**
* @todo 获取两个时间间隔的天数
* @param floorDate
* @param goalDate
* @return
*/
public static long getIntervalDays(Object floorDate, Object goalDate) {
return getIntervalMillSeconds(floorDate, goalDate) / (3600 * 1000 * 24);
}
/**
* @todo 获取两个时间间隔的小时数
* @param floorDate
* @param goalDate
* @return
*/
public static double getIntervalHours(Object floorDate, Object goalDate) {
return getIntervalMillSeconds(floorDate, goalDate) / (3600 * 1000);
}
/**
* @todo 获取两时间的间隔分钟
* @param floorDate
* Date
* @param goalDate
* Date
* @return double
*/
public static double getIntervalMinutes(Object floorDate, Object goalDate) {
return getIntervalMillSeconds(floorDate, goalDate) / (60 * 1000);
}
/**
* @todo 获取两时间的间隔分钟
* @param floorDate
* Date
* @param goalDate
* Date
* @return double
*/
public static double getIntervalSeconds(Object floorDate, Object goalDate) {
return getIntervalMillSeconds(floorDate, goalDate) / (1000);
}
/**
* @todo 获取两时间间隔的毫秒数
* @param floorDate
* Date
* @param goalDate
* Date
* @return double
*/
public static long getIntervalMillSeconds(Object floorDate, Object goalDate) {
return convertDateObject(goalDate).getTime()
- convertDateObject(floorDate).getTime();
}
/**
* @todo 获取两个时间之间的间隔并以格�?:xx天xx小时xx分钟xx秒输出
* @param floorDate
* Date
* @param goalDate
* Date
* @return String
*/
public static String getIntervalDate(Object floorDate, Object goalDate) {
long intervalSeconds = getIntervalMillSeconds(floorDate, goalDate) / 1000;
StringBuffer intervalDate = new StringBuffer();
if (intervalSeconds < 0)
intervalSeconds = -intervalSeconds;
long days = intervalSeconds / (24 * 60 * 60);
intervalSeconds = intervalSeconds - days * 24 * 60 * 60;
long hours = intervalSeconds / (60 * 60);
intervalSeconds = intervalSeconds - hours * 3600;
long minuts = intervalSeconds / 60;
intervalSeconds = intervalSeconds - minuts * 60;
if (days > 0)
intervalDate.append(days + "天");
if (hours > 0)
intervalDate.append(hours + "小时");
if (minuts > 0)
intervalDate.append(minuts + "分钟");
if (intervalSeconds > 0)
intervalDate.append(intervalSeconds + "秒");
return intervalDate.toString();
}
/**
* @todo 获取两时间间隔的年数
* @param floorDate
* Date
* @param goalDate
* Date
* @return double
*/
public static long getIntervalYears(Object floorDate, Object goalDate) {
return convertDateObject(goalDate).getYear()
- convertDateObject(floorDate).getYear();
}
/**
* @todo 获取两时间间隔的月数
* @param floorDate
* Date
* @param goalDate
* Date
* @return double
*/
public static long getIntervalMonths(Object floorDate, Object goalDate) {
Date date1 = convertDateObject(goalDate);
Date date2 = convertDateObject(floorDate);
return date1.getYear() * 12 + date1.getMonth() - date2.getYear() * 12
- date2.getMonth();
}
/**
* 获取给定日期的月份的天数
*
* @param date
* @return
*/
public static int getMonthDays(Object date) {
Date target = convertDateObject(date);
int year = target.getYear();
int month = target.getMonth();
GregorianCalendar beginCalendar = new GregorianCalendar();
beginCalendar.set(year, month, 1);
GregorianCalendar endCalendar = new GregorianCalendar();
endCalendar.set(year, month + 1, 1);
return new Double(getIntervalHours(beginCalendar.getTime(), endCalendar
.getTime()) / 24).intValue();
}
/**
* 将日期转化为中文格式
*
* @param dateValue
* @return
*/
public static String format2China(Object dateValue) {
Date date = convertDateObject(dateValue);
if (date == null)
return null;
GregorianCalendar currentDate = new GregorianCalendar();
currentDate.setTime(convertDateObject(dateValue));
String tmpDate;
StringBuffer result = new StringBuffer();
if (dateValue instanceof String) {
tmpDate = (String) dateValue;
if (tmpDate.length() >= 4)
result.append(currentDate.get(Calendar.YEAR) + "年");
if (tmpDate.length() >= 6)
result.append((currentDate.get(Calendar.MONTH) + 1) + "月");
if (tmpDate.length() >= 8)
result.append(currentDate.get(Calendar.DAY_OF_MONTH) + "日");
if (tmpDate.length() > 10) {
result.append(currentDate.get(Calendar.HOUR_OF_DAY) + "时");
result.append(currentDate.get(Calendar.MINUTE) + "分");
result.append(currentDate.get(Calendar.SECOND) + "秒");
}
} else {
result.append(currentDate.get(Calendar.YEAR) + "年");
result.append((currentDate.get(Calendar.MONTH) + 1) + "月");
result.append(currentDate.get(Calendar.DAY_OF_MONTH) + "日");
result.append(currentDate.get(Calendar.HOUR_OF_DAY) + "时");
result.append(currentDate.get(Calendar.MINUTE) + "分");
result.append(currentDate.get(Calendar.SECOND) + "秒");
}
return result.toString();
}
/**
*
*@TODO 转换中文日期为指定格式的英文日期形式
*@param chinaDate
*@param fmt
*@return
*/
public static String parseChinaDate(String chinaDate, String fmt) {
if (StringUtil.isNullOrBlank(chinaDate))
return null;
String tmp = chinaDate;
for (int i = 0; i < CHINA_DATE_KEYS.length; i++) {
tmp = tmp.replaceAll(CHINA_DATE_KEYS[i], CHINA_DATE_KEY_MAP[i]);
}
String[] chinaDateAry = tmp.split("-");
StringBuffer result = new StringBuffer();
for (int i = 0; i < chinaDateAry.length; i++) {
if (i == 0) {
result.append(chinaDateAry[0]);
} else {
if (chinaDateAry[i].length() == 4)
result.append(chinaDateAry[i].replaceFirst("10", ""));
else if (chinaDateAry[i].length() == 3)
result.append(chinaDateAry[i].replaceFirst("0", ""));
else if (chinaDateAry[i].length() == 2)
result.append(chinaDateAry[i]);
else
result.append("0" + chinaDateAry[i]);
}
}
if (StringUtil.isNullOrBlank(fmt))
return result.toString();
else
return formatDate(result.toString(), fmt);
}
/**
*
*@TODO 转换中文日期为英文格式
*@param chinaDate
*@return
*/
public static String parseChinaDate(String chinaDate) {
return parseChinaDate(chinaDate, null);
}
/**
*
*@TODO 获取月份的第一天
*@param objectDate
*@return
*/
public static Date firstDayOfMonth(Object objectDate) {
Date date = convertDateObject(objectDate);
if (date == null)
return null;
else {
String tmp = formatDate(date, "yyyy-MM-dd");
return parseString(tmp, "yyyy-MM");
}
}
/**
*
*@TODO 获取月份的最后一天
*@param objectDate
*@return
*/
public static Date lastDayOfMonth(Object objectDate) {
Date date = convertDateObject(objectDate);
if (date == null)
return null;
else {
String tmp = formatDate(date, "yyyy-MM-dd");
Date result = parseString(tmp, "yyyy-MM");
result = addMonth(result, 1);
result = addDay(result, -1);
return result;
}
}
/**
*
*@TODO 获取指定日期属于当年中第几个季度
*@param objDate
*@return
*/
public static int getQuarter(Object objDate) {
Date date = convertDateObject(objDate);
if (date == null)
date = DateUtil.getNowTime();
int month = getMonth(date);
return (month + 2) / 3;
}
public static void main(String[] args) throws Exception {
Date temp = DateUtil.parseString("2010-12-27");
System.err.println(temp);
System.err.println(DateUtil.formatDate(temp, "yyyy-MM-dd"));
}
}