31 public void printFormatDate(Date d) {
32 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
33 System.out.println(df.format(d));
34 }
35
36 public void test1() {// 下個月第一天 37 Date d = new Date();
38 d = DateUtils.ceiling(d, Calendar.MONTH);// 進位月份 39 printFormatDate(d);// 2016-02-01 00:00:00 40 }
41
42 public void test2() {// 下個月第一天 43 Date d = new Date();
44 d = DateUtils.addMonths(d, 1);// 月+1 45 d = DateUtils.setDays(d, 1);// 設置日為1號 46 d = DateUtils.truncate(d, Calendar.DATE);// 過濾時分秒 47 printFormatDate(d);// 2016-02-01 00:00:00 48 }
49
50 public void test3() {// 當月最后一天最后一秒 51 Date d = new Date();
52 d = DateUtils.ceiling(d, Calendar.MONTH);// 進位月份 53 d = DateUtils.addMilliseconds(d, -1);// 減少1秒 54 printFormatDate(d);// 2016-01-31 23:59:59 55 }
56
57 public void test4() {// 當月第一天第一秒 58 Date d = new Date();
59 d = DateUtils.truncate(d, Calendar.MONTH);// 截取時間到月份 60 printFormatDate(d);// 2016-01-01 00:00:00 61 }
62
63 public void test5() {// 下個月的這個時候 64 Date d = new Date();
65 d = DateUtils.addMonths(d, 1);
66 printFormatDate(d);// 2016-02-21 09:46:02 67 }
68
69 public void test6() {// 昨天的這個時候 70 Date d = new Date();
71 d = DateUtils.addDays(d, -1);// 增加1月,如果下個月沒有這1天,那就不加 72 printFormatDate(d);// 2016-01-20 09:46:48 73 }
74
75 public void test7() {// addMonth的注意點:2月沒有29號 76 Date d = new Date();
77 d = DateUtils.setDays(d, 29);
78 d = DateUtils.addMonths(d, 1);// 並沒有增加 79 printFormatDate(d);// 2016-01-29 09:47:45 80 }
81
82 public void test8() {// 這個月15號 83 Date d = new Date();
84 d = DateUtils.setDays(d, 15);
85 d = DateUtils.truncate(d, Calendar.DATE);// 截取時間到日 86 printFormatDate(d);// 2016-01-15 00:00:00 87 }
88
89 public void test9() {// 輸出包含今天的這個星期的每一天,星期第一天是周日 90 Date d = new Date();
91 Iterator<Calendar> c = DateUtils.iterator(d, DateUtils.RANGE_WEEK_SUNDAY);
92 System.out.println();
93 while (c.hasNext()) {
94 printFormatDate(new Date(c.next().getTimeInMillis()));
95 }
96 }
97
98 public void test10() {// 今天是今年第幾天 99 Date d = new Date();
100 System.out.println(DateUtils.getFragmentInDays(d, Calendar.YEAR));// 21101 }
102
103 public void test11() {// 這個月第一個周日104 Date d = new Date();
105 d = DateUtils.setDays(d, 1);
106 while (true) {
107 Calendar c = Calendar.getInstance();
108 c.setTime(d);
109 if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
110 printFormatDate(d);// 2016-01-03 10:31:43111 break;
112 } else {
113 d = DateUtils.addDays(d, 1);
114 }
115 }
116 }
117
118 public void test12() {// 距2月1號還有多少天119 Date d = new Date();
120 Date d2 = new Date();
121 d2 = DateUtils.ceiling(d, Calendar.MONTH);// 2.1號122 long day2 = DateUtils.getFragmentInDays(d2, Calendar.YEAR);//2.1是今年第幾天?123 long day1 = DateUtils.getFragmentInDays(d, Calendar.YEAR);//今天是今年第幾天?124 System.out.println(day2 - day1);//11125 }
01.獲取當月的天數
Calendar a = Calendar.getInstance();
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
02.根據年 月 獲取對應的月份 天數
Calendar a = Calendar.getInstance();
a.set(Calendar.YEAR, year);
a.set(Calendar.MONTH, month - 1);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
03.根據日期 找到對應日期的 星期
String dayOfweek = "-1";
try {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date myDate = myFormatter.parse(date);
SimpleDateFormat formatter = new SimpleDateFormat("E");
String str = formatter.format(myDate);
dayOfweek = str;
} catch (Exception e) {
System.out.println("錯誤!");
}
/**
* 計算開始日期到結束日期之間月數
* @param begDate 開始日期
* @param endDate 結束日期
* @return
*/
public static BigDecimal calculationAmount(Date begDate,Date endDate) {
Calendar cal1 = Calendar.getInstance();
cal1.setTime(begDate);
cal1.add(Calendar.MONTH, 1);
cal1.set(Calendar.DAY_OF_MONTH, cal1.getActualMinimum(Calendar.DAY_OF_MONTH));
Date preMonth =cal1.getTime(); //開始日期的下一個月
Calendar cal2 = Calendar.getInstance();
cal2.setTime(endDate);
cal2.add(Calendar.MONTH, -1);
cal2.set(Calendar.DAY_OF_MONTH, cal2.getActualMaximum(Calendar.DAY_OF_MONTH));
Date lastMonth = cal2.getTime(); //結束日期的上一個月
long midValue = 0;//開始日期的下一個月和結束日期的上一個月之間有多少月
if(preMonth.before(lastMonth)) {
midValue = DataTimeUtils.differentDate("M", preMonth, lastMonth)+1;
}
long begDateNum = DataTimeUtils.differentDate("D", begDate, getEndMonthDate(begDate))+1;//開始日期到當前月底的的天數
long begMouthNum = getCurrentMonthDay(begDate);//開始日期當前月有多少天
BigDecimal begValue = new BigDecimal(begDateNum).divide(new BigDecimal(begMouthNum),6,BigDecimal.ROUND_HALF_UP);
long endDateNum = DataTimeUtils.differentDate("D", getPreMonthDate(endDate),endDate)+1;//結束日期當月的第一天到結束日期的天數
long endMouthNum = getCurrentMonthDay(endDate);//結束日期當前月有多少天
BigDecimal endValue = new BigDecimal(endDateNum).divide(new BigDecimal(endMouthNum),6,BigDecimal.ROUND_HALF_UP);
BigDecimal sumDate =begValue.add(endValue.add(new BigDecimal(midValue)));
return sumDate;
}
/**
* 判斷日期是否在自然季內
*/
public static int getNaturalSeason(Date begDate) {
Calendar beg = Calendar.getInstance();//設置開始日期
beg.setTime(begDate);
if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=3) {
return 1;
}else if((beg.get(Calendar.MONTH)+1)>=4 && (beg.get(Calendar.MONTH)+1)<=6) {
return 2;
}else if((beg.get(Calendar.MONTH)+1)>=7 && (beg.get(Calendar.MONTH)+1)<=9) {
return 3;
}else if((beg.get(Calendar.MONTH)+1)>=10 && (beg.get(Calendar.MONTH)+1)<=12) {
return 4;
}
return 0;
}
/**
* 判斷日期是否在自然半年內
*/
public static int getNaturalHalfYear(Date begDate) {
Calendar beg = Calendar.getInstance();//設置開始日期
beg.setTime(begDate);
if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=6) {
return 1;
}else if((beg.get(Calendar.MONTH)+1)>=7 && (beg.get(Calendar.MONTH)+1)<=12) {
return 2;
}
return 0;
}
/**
* 判斷日期是否在自然年內
*/
public static int getNaturalYear(Date begDate) {
Calendar beg = Calendar.getInstance();//設置開始日期
beg.setTime(begDate);
if((beg.get(Calendar.MONTH)+1)>0 && (beg.get(Calendar.MONTH)+1)<=12) {
return 1;
}
return 0;
}
/**
* 獲取任意時間的月第一天
* @param repeatDate
* @return
*/
public static Date getPreMonthDate(Date repeatDate){
return DateUtils.truncate(repeatDate,Calendar.MARCH);
}
/**
* 獲取任意時間的月的最后一天
* @param repeatDate
* @return
*/
public static Date getEndMonthDate(Date repeatDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(repeatDate);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return calendar.getTime();
}
/**
* 根據日期計算當月有多少天
* @param date
* @return
*/
public static long getCurrentMonthDay(Date date) {
Calendar a = Calendar.getInstance();
a.setTime(date);
a.set(Calendar.DATE, 1);
a.roll(Calendar.DATE, -1);
int maxDate = a.get(Calendar.DATE);
return maxDate;
}
/**
* 判斷時間格式
*/
public static boolean isValidDate(String str) {
boolean convertSuccess=true;
// 指定日期格式為四位年/兩位月份/兩位日期,注意yyyy-MM-dd區分大小寫;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
// 設置lenient為false. 否則SimpleDateFormat會比較寬松地驗證日期,比如2007-02-29會被接受,並轉換成2007-03-01
format.setLenient(false);
format.parse(str);
} catch (ParseException e) {
// 如果throw java.text.ParseException或者NullPointerException,就說明格式不對
convertSuccess=false;
}
return convertSuccess;
}
/**
*
* @param ts MinimumGuaranteeDomain 的list
* @return false 時間交叉 true 時間不交叉
*/
public static boolean check(List<MinimumGuaranteeDomain> ts ) {
Collections.sort(ts, new MinimumGuaranteeDomain());
boolean flag=true;
for(int i=0; i<ts.size()-1;i++) {
MinimumGuaranteeDomain item1=ts.get(i);;
MinimumGuaranteeDomain item2=ts.get(i+1);
Date end = new Date(DataTimeUtils.getTimeLong(item1.getEnddate())*1000);
Date beg = new Date(DataTimeUtils.getTimeLong(item2.getBegdate())*1000);
if(DataTimeUtils.differentDate("D",end,beg)<0) {
flag=false;
break;
}
}
return flag ;
}
