packageorg.apache.pwd.util;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.Date;
publicclassDateFormatUtil{
/**12小时制*/
privatestaticfinalStringDATE_FORMAT_12_HOUR="yyyy/MM/ddhh:mm:ss";
/**24小时制*/
privatestaticfinalStringDATE_FORMAT_24_HOUR="yyyy/MM/ddHH:mm:ss";
/**
*根据格式返回日期字符串
*
*@paramdateInfo
*日期
*@paramstringFormat
*格式化字符串
*@return日期字符串
*/
publicstaticStringgetStringDate(DatedateInfo,StringstringFormat){
if(isNullOrEmpty(stringFormat)){
returnnull;
}
SimpleDateFormatsdf=newSimpleDateFormat(stringFormat);
Stringformated=sdf.format(dateInfo);
returnformated;
}
/**
*根据格式返回日期
*
*@paramdateInfo
*日期字符串
*@paramstringFormat
*格式化字符串
*@return日期
*@throwsException
*例外
*/
publicstaticDategetFormatDate(StringdateInfo,StringstringFormat)throwsException{
if(isNullOrEmpty(stringFormat)){
returnnull;
}
SimpleDateFormatsdf=newSimpleDateFormat(stringFormat);
Dateformated=sdf.parse(dateInfo);
returnformated;
}
/**
*根据年月日生成Calendar对象
*
*@paramyear
*年
*@parammonth
*月
*@paramday
*日
*@returnCalendar对象
*/
publicstaticCalendarcreateCalendar(intyear,intmonth,intday){
Calendarcal=Calendar.getInstance();
if(year
year=2000+year;
}
cal.set(year,month-1,day);
returncal;
}
/**
*根据日期生成Calendar对象
*
*@paramdate
*日期
*@returnCalendar对象
*/
publicstaticCalendarcreateCalendar(intdate){
intyear=date/1000;
intmonth=(date%10000)/100;
intday=date%100;
Calendarcal=Calendar.getInstance();
cal.set(year,month-1,day);
returncal;
}
/**
*返回年份
*
*@return年份
*@throwsException
*例外
*/
publicstaticIntegergetNowYear()throwsException{
Calendarcal=Calendar.getInstance();
intyear=cal.get(Calendar.YEAR);
returnyear;
}
/**
*返回月份
*
*@return月份
*@throwsException
*例外
*/
publicstaticIntegergetNowMonth()throwsException{
Calendarcal=Calendar.getInstance();
intmonth=cal.get(Calendar.MONTH)+1;
returnmonth;
}
/**
*返回当前日期
*
*@return日期
*/
publicstaticIntegergetNowDay(){
Calendarcal=Calendar.getInstance();
intday=cal.get(Calendar.DAY_OF_MONTH);
returnday;
}
/**
*返回某月份的第一天
*
*@paramyear
*年
*@parammonth
*月
*@return某月份的第一天
*/
publicstaticIntegergetFirstDayOfMonth(intyear,intmonth){
Calendarcal=createCalendar(year,month,1);
intday=cal.get(Calendar.DAY_OF_MONTH);
returnday;
}
/**
*返回某月份的最后一天
*
*@paramyear
*年
*@parammonth
*月
*@return某月份的最后一天
*/
publicstaticIntegergetLastDayOfMonth(intyear,intmonth){
Calendarcal=createCalendar(year,month,1);
intlastDay=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
returnlastDay;
}
/**
*计算两个日期之间相差的天数
*
*@paramstartDate
*开始日期
*@paramendDate
*结束日期
*@return两个日期之间相差的天数
*/
publicstaticIntegerdaysBetweenData(IntegerstartDate,IntegerendDate){
if(startDate==null||endDate==null){
returnnull;
}
CalendarstartCal=createCalendar(startDate);
CalendarendCal=createCalendar(endDate);
Calendarcal=null;
if(startCal.before(endCal)){
cal=startCal;
startCal=endCal;
endCal=cal;
}
Longlg=(startCal.getTimeInMillis()-endCal.getTimeInMillis())/1000/60/60/24;
returnlg.intValue();
}
/**
*判断是否为闰年
*
*@paramobj
*输入年份
*@return是否为闰年
*@throwsException
*例外
*/
publicstaticbooleanisLeapYear(Tobj)throwsException{
SimpleDateFormatsdf=newSimpleDateFormat("yyyy/MM/dd");
Calendarcalendar=Calendar.getInstance();
intyear=0;
if(objinstanceofString){
DatedateInfo=sdf.parse((String)obj);
calendar.setTime(dateInfo);
year=calendar.get(Calendar.YEAR);
}elseif(objinstanceofDate){
calendar.setTime((Date)obj);
year=calendar.get(Calendar.YEAR);
}else{
returnfalse;
}
if((year%4==0&&year%100!=0)||(year%100==0&&year%400==0)){
System.out.println("是闰年");
returntrue;
}
System.out.println("不是闰年");
returnfalse;
}
/**
*
*
*@paramstringFormat
*@return
*/
publicstaticbooleanisNullOrEmpty(StringstringFormat){
return(stringFormat==null||stringFormat.length()==0)?true:false;
}
publicstaticvoidmain(String[]args)throwsException{
StringdateInfo="/12/12";
Calendarcal=Calendar.getInstance();
cal.setTime(newSimpleDateFormat("yyyy/MM/dd").parse(dateInfo));
//isLeapYear(cal.getTime());
System.out.println(getNowDay());
System.out.println(getFirstDayOfMonth(,12));
System.out.println(getLastDayOfMonth(,9));
//System.out.println(getStringDate(newDate(),"yyyy/MM/ddHH:mm:ss"));
System.out.println(daysBetweenData(0303,0304));
}
}