/*
* 创建日期 2004-11-3
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package com.pub.date;
import java.util.Calendar;
import java.util.Date;
import java.util.ArrayList;
import java.text.SimpleDateFormat;
import com.pub.str.StringFunction;
/**
* @author explorerwen
*
* TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class DateFormat implements Comparable {
//年
protected String year;
//月
protected String month;
//日
protected String day;
//小时
protected String hour;
//分钟
protected String minute;
//秒钟
protected String second;
/**
* 构造一个DateFormat,值为当前系统日期/时间
*/
public DateFormat() {
Calendar date = Calendar.getInstance();
year = String.valueOf(date.get(Calendar.YEAR));
month = String.valueOf(date.get(Calendar.MONTH) + 1);
day = String.valueOf(date.get(Calendar.DATE));
year = StringFunction.updateZero(year, 4);
month = StringFunction.updateZero(month, 2);
day = StringFunction.updateZero(day, 2);
hour = String.valueOf(date.get(Calendar.HOUR_OF_DAY));
minute = String.valueOf(date.get(Calendar.MINUTE));
second = String.valueOf(date.get(Calendar.SECOND));
hour = StringFunction.updateZero(hour, 2);
minute = StringFunction.updateZero(minute, 2);
second = StringFunction.updateZero(second, 2);
}
/**
* 根据指定日期构造一个DateFormat,如果指定的日期不合法,则抛出一个错误信息
*
* @param _date
* 指定的日期
*/
public DateFormat(String _date) {
this(_date, false);
}
public DateFormat(String _date, boolean _timeFlag) {
if (_date == null) {
throw new NullPointerException();
}
String inputDate;
if (_timeFlag) {
inputDate = StringFunction.getDateTimeFormat(_date);
if (!checkDateTime(inputDate))
throw new Error("日期不合法");
} else {
inputDate = StringFunction.getDateFormat(_date);
if (!checkDate(inputDate))
throw new Error("日期不合法");
}
year = inputDate.substring(0, 4);
month = inputDate.substring(4, 6);
day = inputDate.substring(6, 8);
if (_timeFlag) {
hour = inputDate.substring(8, 10);
minute = inputDate.substring(10, 12);
second = inputDate.substring(12, 14);
} else {
hour = "00";
minute = "00";
second = "00";
}
}
/**
* 根据指定的日期和时间构造一个DateFormat, 如果指定的日期和时间不合法,则抛出一个错误信息
*
* @param _date
* 指定的日期
* @param _ho
* 指定的小时
* @param _mi
* 指定的分钟
* @param _se
* 指定的秒钟
*/
public DateFormat(String _date, String _ho, String _mi, String _se) {
this();
if (!checkDate(_date))
throw new NullPointerException("日期不合法");
if (!checkTime(_ho, _mi, _se))
throw new NullPointerException("时间不合法");
year = _date.substring(0, 4);
month = _date.substring(4, 6);
day = _date.substring(6, 8);
hour = _ho;
minute = _mi;
second = _se;
}
public DateFormat(Date d) {
Calendar date = Calendar.getInstance();
date.setTime(d);
year = String.valueOf(date.get(Calendar.YEAR));
month = String.valueOf(date.get(Calendar.MONTH) + 1);
day = String.valueOf(date.get(Calendar.DATE));
year = StringFunction.updateZero(year, 4);
month = StringFunction.updateZero(month, 2);
day = StringFunction.updateZero(day, 2);
hour = String.valueOf(date.get(Calendar.HOUR_OF_DAY));
minute = String.valueOf(date.get(Calendar.MINUTE));
second = String.valueOf(date.get(Calendar.SECOND));
hour = StringFunction.updateZero(hour, 2);
minute = StringFunction.updateZero(minute, 2);
second = StringFunction.updateZero(second, 2);
}
// ------------------------------------------------------------------------------
/**
* 获取当前系统日期/时间
*
* @return 当前系统日期/时间
*/
public synchronized static DateFormat getCurrentDate() {
return new DateFormat();
}
// ------------------------------------------------------------------------------
/**
* 设定日期, 如果设定的日期不合法,则抛出一个出错信息
*
* @param _date
* 设定的日期值
*/
public void setDate(String _date) {
if (!checkDate(_date))
throw new NullPointerException("日期不合法");
year = _date.substring(0, 4);
month = _date.substring(4, 6);
day = _date.substring(6, 8);
}
/**
* 设定日期, 如果设定的日期不合法,则抛出一个错误信息
*
* @param _yyyy
* 年
* @param _mm
* 月
* @param _dd
* 日
*/
public void setDate(int _yyyy, int _mm, int _dd) {
String yyyy = StringFunction.updateZero(String.valueOf(_yyyy), 4);
String mm = StringFunction.updateZero(String.valueOf(_mm), 2);
String dd = StringFunction.updateZero(String.valueOf(_dd), 2);
setDate(yyyy + mm + dd);
}
/**
* 设定时间, 如果设定的时间不合法,则抛出一个错误信息
*
* @param _ho
* 小时
* @param _mi
* 分钟
* @param _se
* 秒钟
*/
public void setTime(String _ho, String _mi, String _se) {
if (!checkTime(_ho, _mi, _se))
return;
hour = _ho;
minute = _mi;
second = _se;
}
// ------------------------------------------------------------------------------
//取出成员变量的值/以规范输出的方式取出成员变量的值
public String getYear() {
return year;
}
public String getMonth() {
return month;
}
public String getDay() {
return day;
}
public String getMonthDate() {
return year + month;
}
public String getDate() {
return year + month + day;
}
public String getHour() {
return hour;
}
public String getMinute() {
return minute;
}
public String getSecond() {
return second;
}
public String getTime() {
return hour + minute + second;
}
public String getPrintMonth() {
return year + "-" + month;
}
public String getPrintDate() {
return year + "-" + month + "-" + day;
}
public String getPrintTime() {
return hour + ":" + minute + ":" + second;
}
public String getPrintDateTime() {
return getPrintDate() + " " + getPrintTime();
}
public String getSQLDate() {
return "TO_DATE('" + getDate() + "','YYYYMMDD')";
}
// ------------------------------------------------------------------------------
/**
* 检查日期是否合法
* @param Date _date
* 需要检查的日期
* @return 如果日期合法则为true,否则为false
*/
public synchronized static boolean checkDate(Date _date){
SimpleDateFormat dtFmt = new SimpleDateFormat("yyyyMMdd");
String date = dtFmt.format(_date);
boolean flag = checkDate(date);
return flag;
}
//--------------------------------------------------------------------------------
/**
* 检查日期是否合法
* @param _date
*需要检查的日期
* @return 如果日期合法则为true,否则为false
*/
public synchronized static boolean checkDate(String _date) {
if (_date == null) {
return false;
}
if (_date.length() < 8)
return false;
String yy = _date.substring(0, 4);
String mm = _date.substring(4, 6);
String dd = _date.substring(6, 8);
try {
int iyy = Integer.parseInt(yy);
int imm = Integer.parseInt(mm);
int idd = Integer.parseInt(dd);
if (iyy > 2050 || iyy < 1950) {
return false;
}
if (imm < 1 || imm > 12)
return false;
int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm));
if (idd < 1 || idd > iMaxMonthDay)
return false;
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
return false;
}
}
/**
* 检查日期是否合法
* @param Date _date
* 需要检查的日期
* @return 如果日期合法则为true,否则为false
*/
public synchronized static boolean checkDateTime(Date _date){
SimpleDateFormat dtFmt = new SimpleDateFormat("yyyyMMddHHmmss");
String date = dtFmt.format(_date);
System.out.println(date);
boolean flag = checkDateTime(date);
return flag;
}
/**
* 检查日期是否合法
*
* @param _date
* 需要检查的日期
* @return 如果日期合法则为true,否则为false
*/
public synchronized static boolean checkDateTime(String _date) {
if (_date == null) {
return false;
}
if (_date.length() < 14)
return false;
String yy = _date.substring(0, 4);
String mm = _date.substring(4, 6);
String dd = _date.substring(6, 8);
String ho = _date.substring(8, 10);
String mi = _date.substring(10, 12);
String se = _date.substring(12, 14);
try {
int iyy = Integer.parseInt(yy);
int imm = Integer.parseInt(mm);
int idd = Integer.parseInt(dd);
if (iyy > 2050 || iyy < 1950) {
return false;
}
if (imm < 1 || imm > 12)
return false;
int iMaxMonthDay = Integer.parseInt(getMaxMonthDay(yy, mm));
if (idd < 1 || idd > iMaxMonthDay)
return false;
int iho = Integer.parseInt(ho);
int imi = Integer.parseInt(mi);
int ise = Integer.parseInt(se);
if (iho < 0 || iho > 24)
return false;
if (imi < 0 || imi > 60)
return false;
if (ise < 0 || ise > 60)
return false;
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
return false;
}
}
/**
* 检查时间是否合法
*
* @param _ho
* 小时
* @param _mi
* 分钟
* @param _se
* 秒钟
* @return 如果合法则为true,否则为false
*/
public static synchronized boolean checkTime(String _ho, String _mi,
String _se) {
try {
int iho = Integer.parseInt(_ho);
int imi = Integer.parseInt(_mi);
int ise = Integer.parseInt(_se);
if (iho < 0 || iho > 24)
return false;
if (imi < 0 || imi > 60)
return false;
if (ise < 0 || ise > 60)
return false;
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
return false;
}
}
// ------------------------------------------------------------------------------
//
/**
* 获得某月的最大的天数
*
* @param yy
* 年
* @param mm
* 月
* @return 该月的最大天数
*/
public static synchronized String getMaxMonthDay(String yy, String mm) {
Calendar cal = Calendar.getInstance();
try {
int y = Integer.parseInt(yy);
int m = Integer.parseInt(mm);
cal.set(y, m - 1, 1);
} catch (Exception e) {
System.err.println(" DataFunction Exception " + e.getMessage());
}
String maxDay = String.valueOf(cal
.getActualMaximum(Calendar.DAY_OF_MONTH));
return maxDay;
}
/**
* 获得某月的最大天数
*
* @param _yyyymm
* 年月
* @return 该月的最大天数
*/
public static synchronized String getMaxMonthDay(String _yyyymm) {
String yy = _yyyymm.substring(0, 4);
String mm = _yyyymm.substring(4, 6);
return getMaxMonthDay(yy, mm);
}
// ------------------------------------------------------------------------------
//
/**
* 得到过去的某个月
*
* @param _mm
* 向前推算的月数
* @return 该月的日期
*/
public DateFormat getPreviousMonth(int _mm) {
if (_mm < 0)
return getNextMonth(-_mm);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
while (imm - _mm <= 0) {
_mm -= 12;
iyy--;
}
imm -= _mm;
String yy = String.valueOf(iyy);
String mm = String.valueOf(imm);
yy = StringFunction.updateZero(yy, 4);
mm = StringFunction.updateZero(mm, 2);
DateFormat df = new DateFormat(yy + mm + "01");
return df;
}
/**
* 得到将来的某个月
*
* @param _mm
* 向后推算的月数
* @return 该月的日期
*/
public DateFormat getNextMonth(int _mm) {
if (_mm < 0)
return getPreviousMonth(-_mm);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
while (_mm + imm > 12) {
_mm -= 12;
iyy++;
}
imm += _mm;
String yy = String.valueOf(iyy);
String mm = String.valueOf(imm);
yy = StringFunction.updateZero(yy, 4);
mm = StringFunction.updateZero(mm, 2);
DateFormat df = new DateFormat(yy + mm + "01");
return df;
}
/**
* 得到上个月
*
* @return 该月的日期
*/
public DateFormat getPreviousMonth() {
return getPreviousMonth(1);
}
/**
* 得到下个月
*
* @return 该月的日期
*/
public DateFormat getNextMonth() {
return getNextMonth(1);
}
// ------------------------------------------------------------------------------
//
/**
* 得到过去的某日
*
* @param _day
* 向前推算的天数
* @return 该日的日期
*/
public DateFormat getPreviousDay(int _day) {
if (_day < 0)
return getNextDay(-_day);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
int idd = Integer.parseInt(day);
DateFormat df = new DateFormat();
df.setDate(iyy, imm, idd);
if (idd - _day > 0) {
idd -= _day;
df.setDate(iyy, imm, idd);
} else {
_day -= idd;
df = df.getPreviousMonth();
df.setDate(df.getMonthDate() + getMaxMonthDay(df.getMonthDate()));
df = df.getPreviousDay(_day);
}
return df;
}
/**
* 得到将来的某日
*
* @param _day
* 向后推算的天数
* @return 该日的日期
*/
public DateFormat getNextDay(int _day) {
if (_day < 0)
return getPreviousDay(-_day);
int iyy = Integer.parseInt(year);
int imm = Integer.parseInt(month);
int idd = Integer.parseInt(day);
DateFormat df = new DateFormat();
df.setDate(iyy, imm, idd);
if (idd + _day <= Integer
.parseInt(DateFormat.getMaxMonthDay(df.getMonthDate()))) {
idd += _day;
df.setDate(iyy, imm, idd);
} else {
_day -= Integer.parseInt(DateFormat.getMaxMonthDay(df.getMonthDate()))
- idd + 1;
df = df.getNextMonth();
df.setDate(df.getMonthDate() + "01");
df = df.getNextDay(_day);
}
return df;
}
// ------------------------------------------------------------------------------
//
/**
* 获得某日的过去几个月的日期
*
* @param _date
* 指定的日期
* @param _month
* 向前推算的月数
* @return 结果的日期
*/
public synchronized static DateFormat getPreviousMonth(String _date,
int _month) {
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getPreviousMonth(_month);
}
/**
* 获得某日的将来几个月的日期
*
* @param _date
* 指定的日期
* @param _month
* 向后推算的日期
* @return 结果的日期
*/
public synchronized static DateFormat getNextMonth(String _date, int _month) {
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getNextMonth(_month);
}
/**
* 获得某日的过去若干天的日期
*
* @param _date
* 指定的日期
* @param _day
* 向前推算的日期
* @return 结果的日期
*/
public synchronized static DateFormat getPreviousDay(String _date, int _day) {
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getPreviousDay(_day);
}
/**
* 获得某日的将来若干天的日期
*
* @param _date
* 指定的日期
* @param _day
* 向后推算的日期
* @return 结果的日期
*/
public synchronized static DateFormat getNextDay(String _date, int _day) {
if (!DateFormat.checkDate(_date)) {
throw new NullPointerException("日期不正确");
}
return new DateFormat(_date).getNextDay(_day);
}
// ------------------------------------------------------------------------------
//
/**
* 比较两个日期的前后
*
* @param _date1
* 日期1
* @param _date2
* 日期2
* @return 比较的结果, 如果>0,则_date1>_date2;=0,两个日期相等; <0,则_date1 <_date2
*/
public synchronized static int compare(String _date1, String _date2) {
if (!checkDate(_date1)) {
throw new NullPointerException("日期不正确");
}
if (!checkDate(_date2)) {
throw new NullPointerException("日期不正确");
}
int nDate1 = Integer.parseInt(_date1);
int nDate2 = Integer.parseInt(_date2);
return nDate1 - nDate2;
}
// ------------------------------------------------------------------------------
public String toString() {
if (getHour() == null || getMinute() == null || getSecond() == null) {
return getDate();
}
return getDate() + getTime();
}
// ------------------------------------------------------------------------------
public boolean equals(Object o) {
if (o instanceof DateFormat) {
return toString().equals(o.toString());
} else {
DateFormat obj = new DateFormat(o.toString(), true);
return equals(obj);
}
}
// ------------------------------------------------------------------------------
/**
* 比较大小
*
* @param o
* @return
*/
public int compareTo(Object o) {
if (o instanceof DateFormat) {
return toString().compareTo(o.toString());
} else {
DateFormat obj = new DateFormat(o.toString(), true);
return compareTo(obj);
}
}
// ------------------------------------------------------------------------------
/**
* 返回两个日期的距离天数
*
* @param startDate
* 开始日期
* @param endDate
* 结束日期
* @return
*/
public static long getPeriodDayCount(String startDate, String endDate) {
Date d1 = new DateFormat(startDate).toDate();
Date d2 = new DateFormat(endDate).toDate();
long periodTime = d2.getTime() - d1.getTime();
return periodTime / 24 / 60 / 60 / 1000;
}
// ------------------------------------------------------------------------------
public Date toDate() {
Calendar c = Calendar.getInstance();
int iy = Integer.parseInt(getYear());
c.set(Calendar.YEAR, Integer.parseInt(getYear()));
c.set(Calendar.MONTH, Integer.parseInt(getMonth()) - 1);
c.set(Calendar.DATE, Integer.parseInt(getDay()));
c.set(Calendar.HOUR_OF_DAY, Integer.parseInt(getHour()));
c.set(Calendar.MINUTE, Integer.parseInt(getMinute()));
c.set(Calendar.SECOND, Integer.parseInt(getSecond()));
return c.getTime();
}
/**
* 返回一个日期是星期几
* @param _date
* @return
* @throws Exception
*/
public static String getWeek(Date _date){
SimpleDateFormat dtFormat = new SimpleDateFormat("E");
String week = dtFormat.format(_date);
return week;
}
/**
* 返回一个日期是星期几
* @param _date
* @return
* @throws Exception
*/
public static String getWeek(String _date)throws Exception{
if(!checkDate(_date))throw new NullPointerException("日期不合法");
SimpleDateFormat dtFormat = new SimpleDateFormat("yyyyMMdd");
Date date = dtFormat.parse(_date);
String week = getWeek(date);
return week;
}
public static ArrayList week(){
ArrayList aList = new ArrayList();
aList.add("星期日");
aList.add("星期一");
aList.add("星期二");
aList.add("星期三");
aList.add("星期四");
aList.add("星期五");
aList.add("星期六");
return aList;
}
/**
* 返回一个时间所在的一周的时间段
* @param Date _date
* @return 一周的时间段 eg:2004-11-01---2004-11-06
*/
public String getDateWeek(Date _date)throws Exception{
SimpleDateFormat dtFormat = new SimpleDateFormat("yyyyMMdd");
String date = dtFormat.format(_date);
return getDateWeek(date);
}
/**
* 返回一个时间所在的一周的时间段
* @param String _date
* @return 一周的时间段 eg:2004-11-01---2004-11-06
*/
public String getDateWeek(String _date)throws Exception{
ArrayList aList = new ArrayList();
aList = week();
String aWeek = getWeek(_date);
int prodt =0;
int nextdt =0;
int i =0;
while(i < aList.size()){
if(aWeek.equals(aList.get(i))){
prodt = i;
nextdt = 6 - i;
}
i++;
}
DateFormat getNextDay = getPreviousDay(_date,prodt);
DateFormat nextdate = getNextDay(_date,nextdt);
return getNextDay.getPrintDate()+"---"+nextdate.getPrintDate();
}
/**
* 返回一个时间的_date的后_day天是星期几
* @param Date _date
* @param int _day
* @return
* @throws Exception
*/
public static String getNextWeek(Date _date,int _day)throws Exception{
SimpleDateFormat dtFormat = new SimpleDateFormat("yyyyMMdd");
String date = dtFormat.format(_date);
return getNextWeek(date,_day);
}
/**
* 返回一个时间的_date的后_day天是星期几
* @param String _date
* @param int _day
* @return
* @throws Exception
*/
public static String getNextWeek(String _date,int _day)throws Exception{
DateFormat dtFormat = new DateFormat();
dtFormat = DateFormat.getNextDay(_date,_day);
return getWeek(dtFormat.getYear()+dtFormat.getMonth()+dtFormat.getDay());
}
/**
* 返回一个时间的_date的前_day天是星期几
* @param Date _date
* @param int _day
* @return
* @throws Exception
*/
public static String getPreviousWeek(Date _date,int _day)throws Exception{
SimpleDateFormat dtFormat = new SimpleDateFormat("yyyyMMdd");
String date = dtFormat.format(_date);
return getPreviousWeek(date,_day);
}
/**
* 返回一个时间的_date的前_day天是星期几
* @param String _date
* @param int _day
* @return
* @throws Exception
*/
public static String getPreviousWeek(String _date,int _day)throws Exception{
DateFormat dtFormat = new DateFormat();
dtFormat = DateFormat.getPreviousDay(_date,_day);
return getWeek(dtFormat.getYear()+dtFormat.getMonth()+dtFormat.getDay());
}
// ------------------------------------------------------------------------------
public static void main(String[] args) {
try {
Date year = new Date();
DateFormat dtFmt = new DateFormat();
String week = DateFormat.getPreviousWeek(year,3);
System.out.println(week);
} catch (Exception e) {
e.printStackTrace();
}
}
}