//專業(yè)日期轉(zhuǎn)換工具類,記得導(dǎo)入坐標(biāo),無私奉獻(xiàn)絕對好用
<dependency>
? ? <groupId>org.apache.commons</groupId>
? ? <artifactId>commons-lang3</artifactId>
? ? <version>3.4</version>
? ? </dependency>
public class DateUtils extends org.apache.commons.lang3.time.DateUtils{
private static final DateFormat mmddFormat = new SimpleDateFormat("MM-dd");
private static final DateFormat hhmmFormat = new SimpleDateFormat("HH:mm");
? ? private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
? ? ? ? ? ? "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss",
? ? ? ? ? ? "yyyy.MM.dd HH:mm", "yyyy.MM", "yyyyMMdd","yyyyMM" };
public static int compareHM(String hm1,String hm2){
try{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HHmmss");
Date date1 = format.parse("2001-01-01 "+hm1+"00");
Date date2 = format.parse("2001-01-01 "+hm2+"00");
if(date1.getTime()>date2.getTime())
return 1;
else if(date1.getTime()< date2.getTime())
return -1;
else
return 0;
}
catch (ParseException e){
}
return 0;
}
public static Date StrToDate1(String str,String format){
try{
if(StringUtil.isEmpty(format))
format = "yyyy/MM/dd";
java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(str);
}catch(ParseException pe){
return null;
}
}
public static Date StrToDate1(String str){
return StrToDate1(str,"yyyy/MM/dd");
}
public static int compareHM2(String hm1,String hm2){
try{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = format.parse("2001-01-01 "+hm1+":00");
Date date2 = format.parse("2001-01-01 "+hm2+":00");
if(date1.getTime()>date2.getTime())
return 1;
else if(date1.getTime()< date2.getTime())
return -1;
else
return 0;
}
catch (ParseException e){
}
return 0;
}
/**
* 這樣寫才能避免對象的任意創(chuàng)建,達(dá)到簡便又能節(jié)省內(nèi)存空間
* @author XuGuo
* @since 2009-07-23
* @param date
* @return
*/
public static String formatMD(Date date){
return date==null?"":mmddFormat.format(date);
}
public static String formatHM(Date date){
return date==null?"":hhmmFormat.format(date);
}
public static String formatDateTime(Date date,String format){
if (date==null) return "";
if (format==null) return date.toString();
DateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
public static String fromatY(Date date){
return date==null?"":formatDateTime(date,"yyyy");
}
public static String formatY0M0D(Date date){
return date==null?"":formatDateTime(date,"yyyyMMdd");
}
public static String formatMMHHSS(Date date){
return date==null?"":formatDateTime(date,"HHmmss");
}
public static String formatYMD(Date date){
return date==null?"":formatDateTime(date,"yyyy-MM-dd");
}
public static String formatYMDHM(Date date){
return date==null?"":formatDateTime(date,"yyyy-MM-dd HH:mm");
}
public static String formatDateTimeByDate(Date date){
return date==null?"":formatDateTime(date,"yyyy-MM-dd HH:mm:ss");
}
public static String formatYMDHMByDate(Date date){
return date==null?"":formatDateTime(date,"yyyyMMddHHmm");
}
? ? public static boolean showNew(Date time){
? ? ? ? if (time==null) return false;
? ? ? ? return DateUtils.addDays(time,3).compareTo(new Date())>=0;
? ? }
? ? public static Date addDays(Date srcDate, int addDays)
? ? {
? ? return getNextDayCurrDay(srcDate,addDays);
? ? }? ?
? ? public static String addDays(String strDate, int addDays) {
? ? Date? date = StrToDate(strDate);
? ? return formatYMD(addDays(date,addDays));
? ? }
? ? public static Date addMinutes(Date srcDate, int minutes)
? ? {
? ? ? ? Calendar cal = Calendar.getInstance();
? ? ? ? cal.setTime(srcDate);
? ? ? ? cal.add(Calendar.MINUTE, minutes);
? ? ? ? return cal.getTime();
? ? }? ? ?
? ? public static Date getNextDayCurrDay(Date currDate,int i){
? ? if(currDate==null) return null;
? ? GregorianCalendar gc = new GregorianCalendar();
? ? gc.setTime(currDate);
? ? gc.add(GregorianCalendar.DATE, i);
? ? return gc.getTime();
? ? }
public static int getCurrDay(){
? ? Calendar now = Calendar.getInstance();
? ? return now.get(Calendar.DAY_OF_WEEK);
? ? }
/**
* 字符串轉(zhuǎn)化為日期
* @param str 被轉(zhuǎn)化的字符串
* @param format 轉(zhuǎn)化格式
* @return 返回日期
* @throws ParseException
* @author sys53
* @serialData 2007-11-03
*/
public static Date StrToDate(String str,String format){
try{
if(StringUtil.isEmpty(format))
format = "yyyy-MM-dd";
java.text.SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse(str);
}catch(ParseException pe){
return null;
}
}
/**?
? * 得到一個(gè)時(shí)間延后或前移幾天的時(shí)間,nowdate為時(shí)間,delay為前移或后延的分鐘?
? */?
public static String getNextDay(Date nowdate, int delay) {?
try{?
? String mdate = "";?
? long myTime = (nowdate.getTime() / 1000) + delay * 60 ;?
? nowdate.setTime(myTime * 1000);?
? mdate = formatYMDHMByDate(nowdate);?
? return mdate;?
? }catch(Exception e){?
? return formatYMDHMByDate(nowdate);?
? }?
}? ?
/**
* 字符串轉(zhuǎn)化為日期,默認(rèn)格式為:yyyy-MM-dd
* @param str
* @return
* @throws ParseException
*/
public static Date StrToDate(String str){
return StrToDate(str,"yyyy-MM-dd");
}
/**
* 判斷某天是否在某個(gè)星期時(shí)間內(nèi) 比如"2009-05-10" 是否在"1,2,4,5"星期內(nèi)
* @param strDate
* @param week
* @return
*/
public static boolean isExistInWeek(String strDate,String week){
Date date = StrToDate(strDate);
int days = dayOfWeek(date);
if(week.indexOf(String.valueOf(days))>=0){
return true;
}
return false;
}
//判斷日期為星期幾,1為星期一,6為星期六,7為星期天,依此類推?
? public static int? dayOfWeek(Date date)? {?
? //首先定義一個(gè)calendar,必須使用getInstance()進(jìn)行實(shí)例化?
? ? ? Calendar? aCalendar=Calendar.getInstance();?
? ? ? //里面野可以直接插入date類型?
? ? ? aCalendar.setTime(date);?
? ? ? //計(jì)算此日期是一周中的哪一天?
? ? ? int? x=aCalendar.get(Calendar.DAY_OF_WEEK);
? ? ? if(x==1)
? ? ? x=7;
? ? ? else
? ? ? x = x - 1;
? ? ? return? x;?
? }
/**
* 轉(zhuǎn)換字符串日期類型為 "yyyy-MM-dd" 類型
* @param date? 06MAY09
* @return
*
*/
public static String getParsedDate(String strDate){
String[] monIntArray = {"01","02","03","04","05","06","07","08","09","10","11","12"};
String[] monStrArray = {"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
String year = "20"+strDate.substring(5,7);
String month = strDate.substring(2,5);
String day = strDate.substring(0,2);
for(int i=0;i<monStrArray.length;i++){
if(monStrArray[i].equalsIgnoreCase(month)){
month = monIntArray[i];
break;
}
}
return year+"-"+month+"-"+day;
}
public static long getNumOfDays(String date1,String date2){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
? ? ? ? Date d1;
try {
d1 = df.parse(date1);
Date d2 = df.parse(date2);
long diff = Math.abs(d2.getTime()-d1.getTime());
return (long)(diff/(1000*60*60*24));
} catch (ParseException e) {
}
? ? ? ? return 0;
}
public static long getDiffDays(String date1,String date2){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
? ? ? ? Date d1;
? ? ? ? long diff = 0;
try {
d1 = df.parse(date1);
Date d2 = df.parse(date2);
long diff_1 = d2.getTime()-d1.getTime();
if(diff_1>=0){
diff = Math.abs(diff_1);
return (long)(diff/(1000*60*60*24));
}
else{
return (long)(diff_1/(1000*60*60*24));
}
} catch (ParseException e) {
}
? ? ? ? return 0;
}
public static int getDiffDays(Date start,Date end){
long diff = Math.abs(end.getTime()-start.getTime());
return (int)(diff/(1000*60*60*24));
}
public static long getNumOfDays(Date date1,Date date2){
? ? ? ? long diff = Math.abs(date2.getTime()-date1.getTime());
? ? ? ? return (long)(diff/(1000*60*60*24));
}
/**
* 斷判兩個(gè)日期之間時(shí)差是否在5分鐘以上
* @param d1 日期1
* @param d2 日期2
* @return 返回true兩個(gè)日期之間相差5分鐘以上,false相差十分鐘以內(nèi).
*/
public static boolean compare(Date d1,Date d2)
{
if((d2.getTime()-d1.getTime())>600000l){
return true;
}
return false;
}
/**
* 獲取某天是星期幾
* @param d
* @return
*/
@SuppressWarnings("deprecation")
public static String getTheDay(Date d){
return "日一二三四五六".charAt(d.getDay())+"";
}
/**
* 獲取得某年的第幾周的起始日期和結(jié)束日期
* @param year 年份
* @param week 第幾周
* @return String 數(shù)組, [0] 起始日期? ? [1] 結(jié)束日期
*/
public static Date []? weekDateEx(int year ,int week){
if(week<1||week>52)return null;
Date d[] = new Date[2];
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.WEEK_OF_YEAR, week);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
d[0] = c.getTime();
c.add(Calendar.DATE, 6);
d[1] = c.getTime();
return d;
}
public static String []? weekDate(int year ,int week){
if(week<1||week>52)return null;
String s [] = new String [2];
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.WEEK_OF_YEAR, week);
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
s[0] = formatYMD(c.getTime());
c.add(Calendar.DATE, 6);
s[1] = formatYMD(c.getTime());
return s;
}
/**
* 獲取得某年的第幾月的起始日期和結(jié)束日期
* @param year 年份
* @param month 第幾月
* @return String 數(shù)組, [0] 起始日期? ? [1] 結(jié)束日期
*/
public static Date []? monthDateEx(int year ,int month){
if(month<1||month>12) return null;
Date s [] = new Date [2];
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, 1);
s[0] = c.getTime();
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
s[1] = c.getTime();
return s;
}
public static String []? monthDate(int year ,int month){
if(month<1||month>12) return null;
String s [] = new String [2];
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month-1);
c.set(Calendar.DAY_OF_MONTH, 1);
s[0] = formatYMD(c.getTime());
c.add(Calendar.MONTH, 1);
c.add(Calendar.DATE, -1);
s[1] = formatYMD(c.getTime());
return s;
}
/**
* 獲取得某年的第幾季度的起始日期和結(jié)束日期
* @param year 年份
* @param season 第幾季
* @return String 數(shù)組, [0] 起始日期? ? [1] 結(jié)束日期
*/
public static Date []? seasonDateEx(int year ,int season){
if(season<1||season>4)return null;
String y= String.valueOf(year);
Date []s = new Date [2];
switch(season){
case 1 : s[0]= StrToDate(y+"-01-01");s[1]= StrToDate(y+"-03-31");break;
case 2 : s[0]= StrToDate(y+"-04-01");s[1]= StrToDate(y+"-06-30");break;
case 3 : s[0]= StrToDate(y+"-07-01");s[1]= StrToDate(y+"-09-30");break;
case 4 : s[0]= StrToDate(y+"-10-01");s[1]= StrToDate(y+"-12-31");break;
}
return s;
}
public static String []? seasonDate(int year ,int season){
if(season<1||season>4)return null;
String y= String.valueOf(year);
String []s = new String [2];
switch(season){
case 1 : s[0]= y+"-01-01";s[1]= y+"-03-31";break;
case 2 : s[0]= y+"-04-01";s[1]= y+"-06-30";break;
case 3 : s[0]= y+"-07-01";s[1]= y+"-09-30";break;
case 4 : s[0]= y+"-10-01";s[1]= y+"-12-31";break;
}
return s;
}
/**
* 獲取某年某月有多少天 如:20090225 返回28
* @param strDate 某天
* @return
*/
@SuppressWarnings({ "static-access", "deprecation" })
public static int getDaysOfMonth(String strDate){
int day = 0;
Calendar cal = Calendar.getInstance();
//格式化日期
SimpleDateFormat dformat = new SimpleDateFormat("yyyymmdd");
try {
Date date = dformat.parse(strDate);
cal.setTime(date);
//在當(dāng)前月份上加一,由于JAVA種JAN為0,所以這里加2
cal.add(cal.MONTH, 2);
//設(shè)置日期為1號
cal.set(cal.DATE, 1);
//向前退一天
cal.add(cal.DAY_OF_MONTH, -1);
date = cal.getTime();
//得到當(dāng)前日,即是本月的天數(shù)
day = date.getDate();
} catch (Exception e) {
}
return day;
}
@SuppressWarnings("deprecation")
public static int getDateOfMonth(Date date){
return date.getDate();
}
/**
* 獲取當(dāng)前日期的下個(gè)月的若干天后的日期
* @param days
* @return
*/
public static String getDateInNextMonthOfNextDays(int days){
return formatYMD(getNextDayCurrDay(StrToDate(getNextMonthFirst()),days));
}
/**?
? ? * 得到二個(gè)日期間的間隔天數(shù)?
? ? */?
public static String getTwoDay(String sj1, String sj2) {?
? ? SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");?
? ? long day = 0;?
? ? try {?
? ? ? java.util.Date date = myFormatter.parse(sj1);?
? ? ? java.util.Date mydate = myFormatter.parse(sj2);?
? ? ? day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);?
? ? } catch (Exception e) {?
? ? ? return "";?
? ? }?
? ? return day + "";?
}?
/**?
? ? * 兩個(gè)時(shí)間之間的天數(shù)?
? ? *?
? ? * @param date1?
? ? * @param date2?
? ? * @return?
? ? */?
public static long getDays(String date1, String date2) {?
? ? if (date1 == null || date1.equals(""))?
? ? ? return 0;?
? ? if (date2 == null || date2.equals(""))?
? ? ? return 0;?
? ? // 轉(zhuǎn)換為標(biāo)準(zhǔn)時(shí)間?
? ? SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");?
? ? java.util.Date date = null;?
? ? java.util.Date mydate = null;?
? ? try {?
? ? ? date = myFormatter.parse(date1);?
? ? ? mydate = myFormatter.parse(date2);?
? ? } catch (Exception e) {?
? ? }?
? ? long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);?
? ? return day;?
}
// 計(jì)算當(dāng)月最后一天,返回字符串?
public static String getDefaultDay(){? ?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.set(Calendar.DATE,1);//設(shè)為當(dāng)前月的1號?
? ? lastDate.add(Calendar.MONTH,1);//加一個(gè)月,變?yōu)橄略碌?號?
? ? lastDate.add(Calendar.DATE,-1);//減去一天,變?yōu)楫?dāng)月最后一天?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
// 上月第一天?
public static String getPreviousMonthFirst(){? ?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.set(Calendar.DATE,1);//設(shè)為當(dāng)前月的1號?
? ? lastDate.add(Calendar.MONTH,-1);//減一個(gè)月,變?yōu)橄略碌?號?
? ? //lastDate.add(Calendar.DATE,-1);//減去一天,變?yōu)楫?dāng)月最后一天?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
//獲取當(dāng)月第一天?
public static String getFirstDayOfMonth(){? ?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.set(Calendar.DATE,1);//設(shè)為當(dāng)前月的1號?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
//獲取當(dāng)天時(shí)間? ?
public static String getNowTime(String dateformat){?
? ? Date? now? =? new? Date();? ? ?
? ? SimpleDateFormat? dateFormat? =? new? SimpleDateFormat(dateformat);//可以方便地修改日期格式? ? ?
? ? String? hehe? = dateFormat.format(now);? ? ?
? ? return hehe;?
}?
// 獲得當(dāng)前日期與本周日相差的天數(shù)?
private static int getMondayPlus() {?
? ? Calendar cd = Calendar.getInstance();?
? ? // 獲得今天是一周的第幾天,星期日是第一天,星期二是第二天......?
? ? int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK)-1;? ? ? ? //因?yàn)榘粗袊Y拜一作為第一天所以這里減1?
? ? if (dayOfWeek == 1) {?
? ? ? ? return 0;?
? ? } else {?
? ? ? ? return 1 - dayOfWeek;?
? ? }?
}?
// 獲得下周星期日的日期?
public static String getNextSunday() {?
? ? int mondayPlus = getMondayPlus();?
? ? GregorianCalendar currentDate = new GregorianCalendar();?
? ? currentDate.add(GregorianCalendar.DATE, mondayPlus + 7+6);?
? ? Date monday = currentDate.getTime();?
? ? DateFormat df = DateFormat.getDateInstance();?
? ? String preMonday = df.format(monday);?
? ? return preMonday;?
}?
//獲得上月最后一天的日期?
public static String getPreviousMonthEnd(){?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.add(Calendar.MONTH,-1);//減一個(gè)月?
? ? lastDate.set(Calendar.DATE, 1);//把日期設(shè)置為當(dāng)月第一天? ?
? ? lastDate.roll(Calendar.DATE, -1);//日期回滾一天,也就是本月最后一天? ?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
//獲得下個(gè)月第一天的日期?
public static String getNextMonthFirst(){?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.add(Calendar.MONTH,1);//減一個(gè)月?
? ? lastDate.set(Calendar.DATE, 1);//把日期設(shè)置為當(dāng)月第一天? ?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
//獲得下個(gè)月最后一天的日期?
public static String getNextMonthEnd(){?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.add(Calendar.MONTH,1);//加一個(gè)月?
? ? lastDate.set(Calendar.DATE, 1);//把日期設(shè)置為當(dāng)月第一天? ?
? ? lastDate.roll(Calendar.DATE, -1);//日期回滾一天,也就是本月最后一天? ?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
//獲得明年最后一天的日期?
public static String getNextYearEnd(){?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.add(Calendar.YEAR,1);//加一個(gè)年?
? ? lastDate.set(Calendar.DAY_OF_YEAR, 1);?
? lastDate.roll(Calendar.DAY_OF_YEAR, -1);?
? str=sdf.format(lastDate.getTime());?
? return str;? ?
}?
//獲得明年第一天的日期?
public static String getNextYearFirst(){?
? ? String str = "";?
? ? SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");? ? ?
? ? Calendar lastDate = Calendar.getInstance();?
? ? lastDate.add(Calendar.YEAR,1);//加一個(gè)年?
? ? lastDate.set(Calendar.DAY_OF_YEAR, 1);?
? ? str=sdf.format(lastDate.getTime());?
? ? return str;? ?
}?
//獲得本年有多少天?
@SuppressWarnings("unused")
private static int getMaxYear(){?
? ? Calendar cd = Calendar.getInstance();?
? ? cd.set(Calendar.DAY_OF_YEAR,1);//把日期設(shè)為當(dāng)年第一天?
? ? cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滾一天。?
? ? int MaxYear = cd.get(Calendar.DAY_OF_YEAR);? ?
? ? return MaxYear;?
}?
private static int getYearPlus(){?
? ? Calendar cd = Calendar.getInstance();?
? ? int yearOfNumber = cd.get(Calendar.DAY_OF_YEAR);//獲得當(dāng)天是一年中的第幾天?
? ? cd.set(Calendar.DAY_OF_YEAR,1);//把日期設(shè)為當(dāng)年第一天?
? ? cd.roll(Calendar.DAY_OF_YEAR,-1);//把日期回滾一天。?
? ? int MaxYear = cd.get(Calendar.DAY_OF_YEAR);?
? ? if(yearOfNumber == 1){?
? ? ? ? return -MaxYear;?
? ? }else{?
? ? ? ? return 1-yearOfNumber;?
? ? }?
}?
//獲得本年第一天的日期?
public static String getCurrentYearFirst(){?
? ? int yearPlus = getYearPlus();?
? ? GregorianCalendar currentDate = new GregorianCalendar();?
? ? currentDate.add(GregorianCalendar.DATE,yearPlus);?
? ? Date yearDay = currentDate.getTime();?
? ? DateFormat df = DateFormat.getDateInstance();?
? ? String preYearDay = df.format(yearDay);?
? ? return preYearDay;?
}?
//獲得本年最后一天的日期 *?
public static String getCurrentYearEnd(){?
? ? Date date = new Date();?
? ? SimpleDateFormat? dateFormat? =? new? SimpleDateFormat("yyyy");//可以方便地修改日期格式? ? ?
? ? String? years? = dateFormat.format(date);? ? ?
? ? return years+"-12-31";?
}?
//獲得上年第一天的日期 *?
public static String getPreviousYearFirst(){?
? ? Date date = new Date();?
? ? SimpleDateFormat? dateFormat? =? new? SimpleDateFormat("yyyy");//可以方便地修改日期格式? ? ?
? ? String? years? = dateFormat.format(date); int years_value = Integer.parseInt(years);? ?
? ? years_value--;?
? ? return years_value+"-1-1";?
}?
//獲得本季度?
public static String getThisSeasonTime(int month){?
? ? int array[][] = {{1,2,3},{4,5,6},{7,8,9},{10,11,12}};?
? ? int season = 1;?
? ? if(month>=1&&month<=3){?
? ? ? ? season = 1;?
? ? }?
? ? if(month>=4&&month<=6){?
? ? ? ? season = 2;?
? ? }?
? ? if(month>=7&&month<=9){?
? ? ? ? season = 3;?
? ? }?
? ? if(month>=10&&month<=12){?
? ? ? ? season = 4;?
? ? }?
? ? int start_month = array[season-1][0];?
? ? int end_month = array[season-1][2];?
? ? Date date = new Date();?
? ? SimpleDateFormat? dateFormat? =? new? SimpleDateFormat("yyyy");//可以方便地修改日期格式? ? ?
? ? String? years? = dateFormat.format(date);? ? ?
? ? int years_value = Integer.parseInt(years);?
? ? int start_days =1;//years+"-"+String.valueOf(start_month)+"-1";//getLastDayOfMonth(years_value,start_month);?
? ? int end_days = getLastDayOfMonth(years_value,end_month);?
? ? String seasonDate = years_value+"-"+start_month+"-"+start_days+";"+years_value+"-"+end_month+"-"+end_days;?
? ? return seasonDate;?
}?
/**?
? * 獲取某年某月的最后一天?
? * @param year 年?
? * @param month 月?
? * @return 最后一天?
? */?
private static int getLastDayOfMonth(int year, int month) {?
? ? ? if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8?
? ? ? ? ? ? ? ? || month == 10 || month == 12) {?
? ? ? ? ? ? return 31;?
? ? ? }?
? ? ? if (month == 4 || month == 6 || month == 9 || month == 11) {?
? ? ? ? ? ? return 30;?
? ? ? }?
? ? ? if (month == 2) {?
? ? ? ? ? ? if (isLeapYear(year)) {?
? ? ? ? ? ? ? ? return 29;?
? ? ? ? ? ? } else {?
? ? ? ? ? ? ? ? return 28;?
? ? ? ? ? ? }?
? ? ? }?
? ? ? return 0;?
}?
/**?
* 是否閏年?
* @param year 年?
* @return?
*/?
public static boolean isLeapYear(int year) {?
? ? return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);?
}?
/**
* 是否是同一天的時(shí)間判斷,主要是正對有些時(shí)間帶時(shí)分秒,有些時(shí)間不帶時(shí)分秒
* @author XuGuo
* @since 2009-04-13
* @param d1
* @param d2
* @return
*/
public static boolean isTheSameDay(Date d1,Date d2) {
Calendar c1 = Calendar.getInstance();?
Calendar c2 = Calendar.getInstance();?
c1.setTime(d1);?
c2.setTime(d2);?
return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))?
&& (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));?
}
/**
* 是否是同一天的時(shí)間判斷,主要是正對有些時(shí)間帶時(shí)分秒,有些時(shí)間不帶時(shí)分秒
* @param d1
* @param d2
* @return
*/
public static boolean isTheSameMonth(Date d1,Date d2){
Calendar c1 = Calendar.getInstance();?
Calendar c2 = Calendar.getInstance();?
c1.setTime(d1);?
c2.setTime(d2);?
return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
&& (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH));?
}
/**
* 判斷某一日期是否在在其他兩個(gè)日期范圍內(nèi)
* @param fromDate
* @param date1
* @param date2
* @return
*/
public static boolean isDateBetweenTwoDates(String fromDate, Date date1,
Date date2) {
Date date = DateUtils.StrToDate(fromDate,"yyyy-MM-dd") ;
if(date.compareTo(date1) >= 0 && date.compareTo(date2) <= 0) {
return true ;
}
return false;
}
/**
* 獲取2個(gè)日期之間所有日期的列表
* @param sdate
* @param edate
* @return
*/
public static List<String> getDayList(String sdate,String edate){
List<String> days = null;
if(!StringUtil.isEmpty(sdate)&&!StringUtil.isEmpty(edate)){
days = new ArrayList<String>();
Calendar cal1 = Calendar.getInstance();
cal1.setTime(DateUtils.StrToDate(sdate,"yyyy-MM-dd"));
Calendar cal2 = Calendar.getInstance();
cal2.setTime(DateUtils.StrToDate(edate,"yyyy-MM-dd"));
while(cal1.compareTo(cal2)<=0){
int year = cal1.get(Calendar.YEAR);
int month = cal1.get(Calendar.MONTH) + 1;
int day = cal1.get(Calendar.DAY_OF_MONTH);
String d = year+ "-" + month + "-" + day;
days.add(d);
cal1.add(Calendar.DAY_OF_YEAR, 1);
}
}
return days;
}
/**
* 獲取2個(gè)日期之間的占用天數(shù)<br/>
* 例如:date1-2012-5-1 15:00,date2:2012-5-3 10:00,則占用天數(shù)為3天<br/>
*/
public static int getOccupyDays(Date date1,Date date2){
if(date1!=null&&date2!=null){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int y1 = cal1.get(Calendar.YEAR);
int y2 = cal2.get(Calendar.YEAR);
int d1 = cal1.get(Calendar.DAY_OF_YEAR);
int d2 = cal2.get(Calendar.DAY_OF_YEAR);
if(y1<y2){
cal1.set(y1, 11, 31);
int d = cal1.get(Calendar.DAY_OF_YEAR)-d1+1;
for(y1++;y1<y2;y1++){
cal1.set(y1, 11, 31);
d += cal1.get(Calendar.DAY_OF_YEAR);
}
d += d2;
return d;
}
else if(y2<y1){
cal2.set(y2, 11, 31);
int d = cal2.get(Calendar.DAY_OF_YEAR)-d2+1;
for(y2++;y2<y1;y2++){
cal2.set(y2, 11, 31);
d += cal2.get(Calendar.DAY_OF_YEAR);
}
d += d1;
return d;
}
else{
return Math.abs(d1-d2)+1;
}
}
if((date1==null&&date2!=null)||(date1!=null&&date2==null)){
return 1;
}
return 0;
}
/**
* 獲取當(dāng)天的起止時(shí)間 如:2012-5-1 00:00到2012-5-1 23:59
* @return
*/
public static Date[] getCurDayBeginAndEnd(){
Date[] d = new Date[2];
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 0);
cal.set(Calendar.MINUTE, 0);
d[0] = cal.getTime();
cal.set(Calendar.HOUR, 23);
cal.set(Calendar.MINUTE, 59);
d[1] = cal.getTime();
return d;
}
/**
* 根據(jù)時(shí)間字符串獲取 日期 yyyy-MM-dd
* @return
*/
public static Date getParseDateFormat(String date){
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date1=null;
try {
date1 = format.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return date1;
}
/**
* 計(jì)算N年后的日期? yyyy-MM-dd
* @return
*/
public static Date getDateAddYears(Date date,BigDecimal n){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date);
int year = cal1.get(Calendar.YEAR);
int months = cal1.get(Calendar.MONTH);
int days = cal1.get(Calendar.DAY_OF_MONTH);
int yearCount = n.intValue();
int monthCount = n.subtract(new BigDecimal(yearCount)).multiply(new BigDecimal(10)).intValue();
year = year+yearCount;
months = months+monthCount;
cal1.set(year, months, days);
return cal1.getTime();
}
/**
* 獲取2個(gè)日期之間的占用年 ,Date2-date1 , 0-1年之間算1年,Date2-date1<0,算1年
* @param date1
* @param date2
* @return
*/
public static int getOccupyYears(Date date1,Date date2){
if(date1!=null&&date2!=null){
if (date2.before(date1)) {
return 1;
}
Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
int y1 = cal1.get(Calendar.YEAR);
int y2 = cal2.get(Calendar.YEAR);
int d1 = cal1.get(Calendar.DAY_OF_YEAR);
int d2 = cal2.get(Calendar.DAY_OF_YEAR);
if (y1==y2) {
return 1;
}else {
if (d2<=d1) {
return y2-y1;
}else {
return y2-y1+1;
}
}
}
return 1;
}
public static int? getDateMonth(String date){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(StrToDate(date));
int m = cal1.get(Calendar.MONTH);
return m+1;
}
public static int? getDateYear(String date){
Calendar cal1 = Calendar.getInstance();
cal1.setTime(StrToDate(date));
int y = cal1.get(Calendar.YEAR);
return y+1900;
}
/**
* 傳個(gè)時(shí)間,判斷小時(shí)差
*/
public static Long gethourDate(Date beforeDate){
Date date = new Date();
long nh=1000*60*60;
long nd = 1000*24*60*60;
long ddf = date.getTime()-beforeDate.getTime();
long day = ddf/nd;
long hour = ddf%nd/nh + day*24;
return hour;
}
/**
* 取給定日期的前一天
*
*/
public static String getBeforeDate(String sdate){
Date date;
String beforeday="";
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int day = c.get(Calendar.DATE);
c.set(Calendar.DATE, day-1);
beforeday = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return beforeday;
}
? ? /**
? ? * 日期型字符串轉(zhuǎn)化為日期 格式
? ? * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm",
? ? * "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
? ? * "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
? ? */
? ? public static Date parseDate(Object str) {
? ? ? ? if (str == null) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? return parseDate(str.toString(), parsePatterns);
? ? ? ? } catch (ParseException e) {
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
? ? /**
? ? * 得到月份字符串 格式(MM)
? ? */
? ? public static String getMonth(Date date) {
? ? ? ? return formatDate(date, "MM");
? ? }
? ? /**
? ? * 得到年份字符串 格式(yyyy)
? ? */
? ? public static String getYear(Date date) {
? ? ? ? return formatDate(date, "yyyy");
? ? }
? ? /**
? ? * 得到日期字符串 默認(rèn)格式(yyyy-MM-dd) pattern可以為:"yyyy-MM-dd" "HH:mm:ss" "E"
? ? */
? ? public static String formatDate(Date date, Object... pattern) {
? ? ? ? String formatDate = null;
? ? ? ? if (pattern != null && pattern.length > 0) {
? ? ? ? ? ? formatDate = DateFormatUtils.format(date, pattern[0].toString());
? ? ? ? } else {
? ? ? ? ? ? formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
? ? ? ? }
? ? ? ? return formatDate;
? ? }
}