/**
* <p>Title:Toten</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: WinChannel </p>
* @author bxz
* @version 1.0
*/
package com.winchannel.common.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.logging.Log;
/**
* 2005-7-26 14:41:31
*/
public class DateUtility {
// private static Log log = LogUtil.getLoger(DateUtility.class);
private static final long ONE_DAY = 86400000l;
/**
* 获取当前日期字符串 格式为YYYY-MM-DD
*
* @return java.lang.String
*/
public static String getCurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = df.format(new Date());
return s;
}
/**
* 得到当前年,格式如:2008
* @return
*/
public static String getCurrentYear() {
SimpleDateFormat df = new SimpleDateFormat("yyyy");
String s = df.format(new Date());
return s;
}
/**
* 得到当前月,格式如:02
* @return
*/
public static String getCurrentMonth() {
SimpleDateFormat df = new SimpleDateFormat("MM");
String s = df.format(new Date());
return s;
}
public static String getDayInWeek(String sDate) {
Date date = strToDate(sDate);
SimpleDateFormat df = new SimpleDateFormat("EEE");
String s = df.format(date);
return s;
}
/**
* 返回日期类型
* @param str
* @return
*/
public static Date strToDate(String str) {
Date date = null;
if (str != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
date = df.parse(str);
} catch (ParseException e) {
//log.error("DateParse Error!");
}
}
return date;
}
/**
* 返回日期类型
* @param str
* @return
*/
public static Date strToDateTime(String str) {
Date date = null;
if (str != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = df.parse(str);
} catch (ParseException e) {
//log.error("DateParse Error!");
}
}
return date;
}
public static String dateTimeToStr(Date date) {
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (date != null) {
str = df.format(date);
}
return str;
}
public static String dateToStr(Date date) {
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
if (date != null) {
str = df.format(date);
}
return str;
}
/**
* 转化成中文类型的日期
* @param date
* @return
*/
public static String dateToStrCh(Date date) {
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
if (date != null) {
str = df.format(date);
}
return str;
}
/**
* 在原有的日期上面加i天
* @param date
* @param i
* @return
*/
public static Date add(Date date, int i) {
date = new Date(date.getTime() + i * ONE_DAY);
return date;
}
/**
* 加1天
* @param date
* @return
*/
public static Date add(Date date) {
return add(date, 1);
}
/**
* 减1天
* @param date
* @return
*/
public static Date sub(Date date) {
return add(date, -1);
}
public static String getBeforeDate() {
Date date = DateUtility.sub(new Date());
return DateUtility.dateToStr(date);
}
/**
* 得到当前时间,格式:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getCurrentDateTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = df.format(new Date());
return s;
}
/**
* 得到当前时间(星期),格式:yyyy年MM月dd日 EEE
* @return
*/
public static String getCurrentDateWeek() {
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 EEE");
String s = df.format(new Date());
return s;
}
/**
* 得到当前时间(星期),格式:EEE, d MMM yyyy
* @return
*/
public static String getCurrentDateWeekEn() {
SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy ",
new Locale("en"));
String s = df.format(new Date());
return s;
}
/**
* 返回月份之间的差。
* @param startYear
* @param startMonth
* @param endYear
* @param endMonth
* @return
*/
public static int compareMonth(String startYear, String startMonth,
String endYear, String endMonth) {
return (Integer.parseInt(endYear) - Integer.parseInt(startYear)) * 12
+ (Integer.parseInt(endMonth) - Integer.parseInt(startMonth));
}
/**
*
* @param sDate
* @return
*/
public static String getYearMonth(String sDate) {
Date date1 = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = null;
try {
date1 = df.parse(sDate);
df.applyPattern("yyMM");
s = df.format(date1);
} catch (ParseException e) {
return s;
}
return s;
}
/**
*
* @param date
* @return
*/
public static String getYearMonth(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyMM");
String s = null;
s = df.format(date);
return s;
}
public static String getMonthDay(Date date) {
SimpleDateFormat df = new SimpleDateFormat("MM月dd日");
String s = null;
s = df.format(date);
return s;
}
public static String getYearMonthDay(String sDate) {
Date date1 = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = null;
try {
date1 = df.parse(sDate);
df.applyPattern("yyMMdd");
s = df.format(date1);
} catch (ParseException e) {
return s;
}
return s;
}
public static String getStartQueryTime(String date) {
return DateUtility.dateToStr(DateUtility.strToDate(date)) + " 00:00:00";
}
public static String getEndQueryTime(String date) {
return DateUtility.dateToStr(DateUtility.strToDate(date)) + " 23:59:59";
}
/**
*
* @param sDate
* @return
*/
public static String getMonth(String sDate) {
Date date1 = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String s = null;
try {
date1 = df.parse(sDate);
df.applyPattern("MM");
s = df.format(date1);
} catch (ParseException e) {
return s;
}
return s;
}
/**
*
* @param sDate1
* @param sDate2
* @return
*/
public static int compareDate(String sDate1, String sDate2) {
Date date1 = null;
Date date2 = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
date1 = dateFormat.parse(sDate1);
date2 = dateFormat.parse(sDate2);
} catch (ParseException e) {
}
long dif = 0;
if (date2.after(date1))
dif = (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24;
else
dif = (date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24;
return (int) dif;
}
public static int getDate(String sDate, String sTag) {
int iSecondMinusPos = sDate.lastIndexOf('-');
if (sTag.equalsIgnoreCase("y")) {
return Integer.parseInt(sDate.substring(0, 4));
} else if (sTag.equalsIgnoreCase("m")) {
return Integer.parseInt(sDate.substring(5, iSecondMinusPos));
} else
return Integer.parseInt(sDate.substring(iSecondMinusPos + 1));
}
public static int getDayOfWeek() {
Calendar toDay = Calendar.getInstance();
toDay.setFirstDayOfWeek(Calendar.MONDAY);
int ret = toDay.get(Calendar.DAY_OF_WEEK) - 1;
if (ret == 0) {
ret = 7;
}
return ret;
}
public static String getFirstDayOfMonth() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(firstDate);
}
public static String getFirstDayOfMonth(java.util.Calendar ca) {
// Calendar ca = Calendar.getInstance();
// ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(firstDate);
}
/**
* @author 包忠华
* @description 获取当前时间的下一年
* @return String
*/
public static String getNextYearDate() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
ca.add(Calendar.YEAR, 1);
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getNextYearDateBystr(String str) {
Date d=DateUtility.strToDate(str);
Calendar ca = Calendar.getInstance();
ca.setTime(d);
ca.set(Calendar.DAY_OF_MONTH, 1);
ca.add(Calendar.YEAR, 1);
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getLastDayOfMonth() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getLastDayOfMonth(Calendar ca) {
// Calendar ca = Calendar.getInstance();
// ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getDayInWeek() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date());
String ret = String.valueOf(ca.get(Calendar.DAY_OF_WEEK) - 1);
return ret;
}
public static String getYearAndMonths(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yy-MM");
String s = null;
s = df.format(date);
return s;
}
public static String getYearAndMonth(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
String s = null;
s = df.format(date);
return s;
}
public final static Timestamp getTimeOfStr ( String dateStr ) {
DateFormat df = new SimpleDateFormat ( "yyyy-MM" );
Timestamp time = null;
try {
java.util.Date da = df.parse ( dateStr );
time = new Timestamp ( da.getTime () );
}
catch ( Exception e ) {
e.printStackTrace ();
}
return time;
}
}
* <p>Title:Toten</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2006</p>
* <p>Company: WinChannel </p>
* @author bxz
* @version 1.0
*/
package com.winchannel.common.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.apache.commons.logging.Log;
/**
* 2005-7-26 14:41:31
*/
public class DateUtility {
// private static Log log = LogUtil.getLoger(DateUtility.class);
private static final long ONE_DAY = 86400000l;
/**
* 获取当前日期字符串 格式为YYYY-MM-DD
*
* @return java.lang.String
*/
public static String getCurrentDate() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = df.format(new Date());
return s;
}
/**
* 得到当前年,格式如:2008
* @return
*/
public static String getCurrentYear() {
SimpleDateFormat df = new SimpleDateFormat("yyyy");
String s = df.format(new Date());
return s;
}
/**
* 得到当前月,格式如:02
* @return
*/
public static String getCurrentMonth() {
SimpleDateFormat df = new SimpleDateFormat("MM");
String s = df.format(new Date());
return s;
}
public static String getDayInWeek(String sDate) {
Date date = strToDate(sDate);
SimpleDateFormat df = new SimpleDateFormat("EEE");
String s = df.format(date);
return s;
}
/**
* 返回日期类型
* @param str
* @return
*/
public static Date strToDate(String str) {
Date date = null;
if (str != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
date = df.parse(str);
} catch (ParseException e) {
//log.error("DateParse Error!");
}
}
return date;
}
/**
* 返回日期类型
* @param str
* @return
*/
public static Date strToDateTime(String str) {
Date date = null;
if (str != null) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = df.parse(str);
} catch (ParseException e) {
//log.error("DateParse Error!");
}
}
return date;
}
public static String dateTimeToStr(Date date) {
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
if (date != null) {
str = df.format(date);
}
return str;
}
public static String dateToStr(Date date) {
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
if (date != null) {
str = df.format(date);
}
return str;
}
/**
* 转化成中文类型的日期
* @param date
* @return
*/
public static String dateToStrCh(Date date) {
String str = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
if (date != null) {
str = df.format(date);
}
return str;
}
/**
* 在原有的日期上面加i天
* @param date
* @param i
* @return
*/
public static Date add(Date date, int i) {
date = new Date(date.getTime() + i * ONE_DAY);
return date;
}
/**
* 加1天
* @param date
* @return
*/
public static Date add(Date date) {
return add(date, 1);
}
/**
* 减1天
* @param date
* @return
*/
public static Date sub(Date date) {
return add(date, -1);
}
public static String getBeforeDate() {
Date date = DateUtility.sub(new Date());
return DateUtility.dateToStr(date);
}
/**
* 得到当前时间,格式:yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getCurrentDateTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String s = df.format(new Date());
return s;
}
/**
* 得到当前时间(星期),格式:yyyy年MM月dd日 EEE
* @return
*/
public static String getCurrentDateWeek() {
SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 EEE");
String s = df.format(new Date());
return s;
}
/**
* 得到当前时间(星期),格式:EEE, d MMM yyyy
* @return
*/
public static String getCurrentDateWeekEn() {
SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy ",
new Locale("en"));
String s = df.format(new Date());
return s;
}
/**
* 返回月份之间的差。
* @param startYear
* @param startMonth
* @param endYear
* @param endMonth
* @return
*/
public static int compareMonth(String startYear, String startMonth,
String endYear, String endMonth) {
return (Integer.parseInt(endYear) - Integer.parseInt(startYear)) * 12
+ (Integer.parseInt(endMonth) - Integer.parseInt(startMonth));
}
/**
*
* @param sDate
* @return
*/
public static String getYearMonth(String sDate) {
Date date1 = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = null;
try {
date1 = df.parse(sDate);
df.applyPattern("yyMM");
s = df.format(date1);
} catch (ParseException e) {
return s;
}
return s;
}
/**
*
* @param date
* @return
*/
public static String getYearMonth(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyMM");
String s = null;
s = df.format(date);
return s;
}
public static String getMonthDay(Date date) {
SimpleDateFormat df = new SimpleDateFormat("MM月dd日");
String s = null;
s = df.format(date);
return s;
}
public static String getYearMonthDay(String sDate) {
Date date1 = null;
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String s = null;
try {
date1 = df.parse(sDate);
df.applyPattern("yyMMdd");
s = df.format(date1);
} catch (ParseException e) {
return s;
}
return s;
}
public static String getStartQueryTime(String date) {
return DateUtility.dateToStr(DateUtility.strToDate(date)) + " 00:00:00";
}
public static String getEndQueryTime(String date) {
return DateUtility.dateToStr(DateUtility.strToDate(date)) + " 23:59:59";
}
/**
*
* @param sDate
* @return
*/
public static String getMonth(String sDate) {
Date date1 = null;
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
String s = null;
try {
date1 = df.parse(sDate);
df.applyPattern("MM");
s = df.format(date1);
} catch (ParseException e) {
return s;
}
return s;
}
/**
*
* @param sDate1
* @param sDate2
* @return
*/
public static int compareDate(String sDate1, String sDate2) {
Date date1 = null;
Date date2 = null;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
try {
date1 = dateFormat.parse(sDate1);
date2 = dateFormat.parse(sDate2);
} catch (ParseException e) {
}
long dif = 0;
if (date2.after(date1))
dif = (date2.getTime() - date1.getTime()) / 1000 / 60 / 60 / 24;
else
dif = (date1.getTime() - date2.getTime()) / 1000 / 60 / 60 / 24;
return (int) dif;
}
public static int getDate(String sDate, String sTag) {
int iSecondMinusPos = sDate.lastIndexOf('-');
if (sTag.equalsIgnoreCase("y")) {
return Integer.parseInt(sDate.substring(0, 4));
} else if (sTag.equalsIgnoreCase("m")) {
return Integer.parseInt(sDate.substring(5, iSecondMinusPos));
} else
return Integer.parseInt(sDate.substring(iSecondMinusPos + 1));
}
public static int getDayOfWeek() {
Calendar toDay = Calendar.getInstance();
toDay.setFirstDayOfWeek(Calendar.MONDAY);
int ret = toDay.get(Calendar.DAY_OF_WEEK) - 1;
if (ret == 0) {
ret = 7;
}
return ret;
}
public static String getFirstDayOfMonth() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(firstDate);
}
public static String getFirstDayOfMonth(java.util.Calendar ca) {
// Calendar ca = Calendar.getInstance();
// ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(firstDate);
}
/**
* @author 包忠华
* @description 获取当前时间的下一年
* @return String
*/
public static String getNextYearDate() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
ca.add(Calendar.YEAR, 1);
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getNextYearDateBystr(String str) {
Date d=DateUtility.strToDate(str);
Calendar ca = Calendar.getInstance();
ca.setTime(d);
ca.set(Calendar.DAY_OF_MONTH, 1);
ca.add(Calendar.YEAR, 1);
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getLastDayOfMonth() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getLastDayOfMonth(Calendar ca) {
// Calendar ca = Calendar.getInstance();
// ca.setTime(new Date()); // someDate 为你要获取的那个月的时间
ca.set(Calendar.DAY_OF_MONTH, 1);
Date firstDate = ca.getTime();
ca.add(Calendar.MONTH, 1);
ca.add(Calendar.DAY_OF_MONTH, -1);
Date lastDate = ca.getTime();
return dateToStr(lastDate);
}
public static String getDayInWeek() {
Calendar ca = Calendar.getInstance();
ca.setTime(new Date());
String ret = String.valueOf(ca.get(Calendar.DAY_OF_WEEK) - 1);
return ret;
}
public static String getYearAndMonths(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yy-MM");
String s = null;
s = df.format(date);
return s;
}
public static String getYearAndMonth(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
String s = null;
s = df.format(date);
return s;
}
public final static Timestamp getTimeOfStr ( String dateStr ) {
DateFormat df = new SimpleDateFormat ( "yyyy-MM" );
Timestamp time = null;
try {
java.util.Date da = df.parse ( dateStr );
time = new Timestamp ( da.getTime () );
}
catch ( Exception e ) {
e.printStackTrace ();
}
return time;
}
}