/*
* Created on 2010-5-27
*
*/
package ch01;
/**
* @author hohhi
*/
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.GregorianCalendar;
public class DateUtil {
public static int DATETYPE_YYMMDD = 1;
public static int DATETYPE_YYYYMMDD = 2;
/**
* 得到当前日期
*
* @return
*/
public static String getCurrentDate() {
return getCurrentDate("yyyy-MM-dd");
}
/**
* 得到当前时间
*
* @return
*/
public static String getCurrentTime() {
return getCurrentDate("HH:mm:ss");
}
/**
* 得到当前日期及时间
*
* @return
*/
public static String getCurrentDateTime() {
return getCurrentDate("yyyy-MM-dd HH:mm:ss");
}
public static String getCurrentDateTimeNoSign() {
return getCurrentDate("yyyyMMddHHmmss");
}
public static String getSimpleCurrentDate() {
return getCurrentDate("yyMMdd");
}
public static String getCurrentDateNoSign() {
return getCurrentDate("yyyyMMdd");
}
/**
* 得到当前日期,格式由用户自定义,默认为格式为:yyyy-MM-dd HH:mm:ss
*
* @param dateFormat
* @return
*/
public static String getCurrentDate(String dateFormat) {
if (dateFormat == null)
dateFormat = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
Calendar now = Calendar.getInstance();
return sdf.format(now.getTime());
}
/**
* 得到当前日期N天后的日期
*/
public static String getAddSpecDate(String dateType, String dateStr,
int amount) throws ParseException {
int dateWay = 0;
dateType = dateType.toUpperCase();
if (dateType.equals("YEAR"))
dateWay = Calendar.YEAR;
if (dateType.equals("MONTH"))
dateWay = Calendar.MONTH;
if (dateType.equals("DATE"))
dateWay = Calendar.DATE;
Calendar now = Calendar.getInstance();
now.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateStr));
now.add(dateWay, amount);
return (new SimpleDateFormat("yyyy-MM-dd").format(now.getTime()));
}
/**
* 得到当前日期N天后的日期
*/
public static String getAddCurrentDate(String dateType, int amount)
throws Exception {
int dateWay = 0;
dateType = dateType.toUpperCase();
if (dateType.equals("YEAR"))
dateWay = Calendar.YEAR;
if (dateType.equals("MONTH"))
dateWay = Calendar.MONTH;
if (dateType.equals("DATE"))
dateWay = Calendar.DATE;
Calendar now = Calendar.getInstance();
now.add(dateWay, amount);
return (new SimpleDateFormat("yyyy-MM-dd").format(now.getTime()));
}
public static String getAddMonthCurrentDate(String dateType, int amount)
throws Exception {
int dateWay = 0;
dateType = dateType.toUpperCase();
if (dateType.equals("YEAR"))
dateWay = Calendar.YEAR;
if (dateType.equals("MONTH"))
dateWay = Calendar.MONTH;
if (dateType.equals("DATE"))
dateWay = Calendar.DATE;
Calendar now = Calendar.getInstance();
now.add(dateWay, amount);
return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now
.getTime()));
}
public static String getAddCurrentDateTime(String dateType, int amount)
throws Exception {
return getAddCurrentDate(dateType, amount) + " 00:00:00";
}
public static String getAddMonthCurrentDateTime(String dateType, int amount)
throws Exception {
return getAddMonthCurrentDate(dateType, amount);
}
public static String getBeginDateOfCurrMonth(String dateType, int amount)
throws Exception {
String currDate = getAddCurrentDate(dateType, amount);
String beginDate = currDate.substring(0, 8) + "01 00:00:00";
return beginDate;
}
public static String getEndDateOfCurrMonth(String dateType, int amount)
throws Exception {
String currDate = getAddCurrentDate(dateType, amount);
String beginDate = currDate.substring(0, 8) + "28 00:00:00";
return beginDate;
}
public static String getAddSpecDateTime(String dateType, String dateStr,
int amount) throws ParseException {
int dateWay = 0;
dateType = dateType.toUpperCase();
if (dateType.equals("YEAR"))
dateWay = Calendar.YEAR;
if (dateType.equals("MONTH"))
dateWay = Calendar.MONTH;
if (dateType.equals("DATE"))
dateWay = Calendar.DATE;
if (dateType.equals("HOUR"))
dateWay = Calendar.HOUR;
if (dateType.equals("MINUTE"))
dateWay = Calendar.MINUTE;
if (dateType.equals("SECOND"))
dateWay = Calendar.SECOND;
if (dateWay == 0)
return "";
String dateFormatStr = "yyyy-MM-dd HH:mm:ss";
Calendar now = Calendar.getInstance();
now.setTime(new SimpleDateFormat(dateFormatStr).parse(dateStr));
now.add(dateWay, amount);
return (new SimpleDateFormat(dateFormatStr).format(now.getTime()));
}
public static String getAddSpecDateTimeByHour(String dateStr, int addNum)
throws Exception {
return getAddSpecDateTime("hour", dateStr, addNum);
}
public static String getAddSpecDateTimeByMinute(String dateStr, int addNum)
throws Exception {
return getAddSpecDateTime("minute", dateStr, addNum);
}
public static String getAddSpecDateTimeBySecond(String dateStr, int addNum)
throws Exception {
return getAddSpecDateTime("second", dateStr, addNum);
}
// 判断时间date1是否在时间date2之前,时间格式 2010-5-27 16:16:34
public static boolean isDateBefore(String date1, String date2) {
try {
DateFormat df = DateFormat.getDateTimeInstance();
return df.parse(date1).before(df.parse(date2));
} catch (ParseException e) {
System.out.print("[Date:] " + e.getMessage());
return false;
}
}
public static String getCurDate(int i) {
String dateSTR = "";
switch (i) {
case 1:
dateSTR = getSimpleCurrentDate();
break;
case 2:
dateSTR = getCurrentDateNoSign();
break;
// case ...
default:
dateSTR = getCurrentDateTime();
}
return dateSTR;
}
public static String toFormat(int i) {
return "";
}
public static String getBeginDateOfCurrMonths(String dateType, int amount)
throws Exception {
String currDate = getAddCurrentDate(dateType, amount);
String beginDate = currDate.substring(0, 8) + "01";
return beginDate;
}
public static String getEndDateOfCurrMonths(String dateType, int amount)
throws Exception {
String currDate = getAddCurrentDate(dateType, amount);
String beginDate = currDate.substring(0, 8) + "29";
return beginDate;
}
// 获得上月29日
public static String getBegindate() {
Date d = new Date();
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
gc.setTime(d);
gc.add(2, -1);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), 29);
return sf.format(gc.getTime());
}
// 获得本月28日
public static String getEnddate() {
Date d = new Date();
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
gc.setTime(d);
gc.add(2, 0);
gc.set(gc.get(Calendar.YEAR), gc.get(Calendar.MONTH), 28);
return sf.format(gc.getTime());
}
public static String getCurrentDateTime(String dateType, int amount)
throws Exception {
return getAddCurrentDate(dateType, amount);
}
public static String getDateString(Date date){
if(date==null)
return null;
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(date);
}
public static String getDateTimeString(Date date) {
if (date == null)
return null;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(date);
}
public static Timestamp getTimestamp(String dateString){
if(dateString==null)
return null;
return Timestamp.valueOf(dateString);
}
public static java.sql.Date getDate(String dateString){
if(dateString==null)
return null;
return java.sql.Date.valueOf(dateString);
}
public static String getTime(Timestamp time){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
return sdf.format(time);
}
/**
* 判断给定的日期字符串是否比当前日期早
* @param date
* @return 如果给定的日期字符串比当前日期早 则返回true
*/
public static boolean isBeforeCurrentDate(String date){
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date currentDate=new Date();
try {
Date d=format.parse(format.format(currentDate));
return format.parse(date).before(d);
} catch (ParseException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args){
System.out.println(DateUtil.isBeforeCurrentDate("2010-5-27"));
}
}