?? thandle.java
字號:
package stat;
import java.util.*;
import java.sql.Time;
import java.sql.Timestamp;
//日期與時間的處理類
public class THandle {
/**
* 獲取日期date所在的月份的實際天數
*/
public static int getMonthDays(Date date) {
GregorianCalendar currtime = new GregorianCalendar();
currtime.setTime(date);
int year = currtime.get(Calendar.YEAR);
boolean leap = currtime.isLeapYear(year);
int month = currtime.get(GregorianCalendar.MONTH) + 1;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: {
return 31;
}
case 2: {
if (leap) {
return 29;
}
else {
return 28;
}
}
case 4:
case 6:
case 9:
case 11: {
return 30;
}
}
return 0;
}
/**
* 獲取日期date所在月份前n個月的第一天的字符串表示
* 比如date是“2006-3-5”,n = 4
* 那么就返回“2005-11-01”
*/
public static String getStartDateofNMonth(Date date, int n) {
GregorianCalendar currtime = new GregorianCalendar();
currtime.setTime(date);
currtime.add(GregorianCalendar.MONTH, -n);
int year = currtime.get(Calendar.YEAR);
int month = currtime.get(GregorianCalendar.MONTH) + 1;
return year + "-" + month + "-01";
}
/**
* 獲取日期date所在月份前n個月的最后一天的字符串表示
* 比如date是“2006-3-5”,n = 4
* 那么就返回“2005-11-30”
*/
public static String getEndDateofNMonth(Date date, int n) {
GregorianCalendar currtime = new GregorianCalendar();
currtime.setTime(date);
currtime.add(GregorianCalendar.MONTH, -n);
int year = currtime.get(Calendar.YEAR);
int month = currtime.get(GregorianCalendar.MONTH) + 1;
int days = getMonthDays(currtime.getTime());
return year + "-" + month + "-" + days;
}
/**
* 獲取日期date所在月份前n個月的月份字符串表示
* 比如date是“2006-3-5”,n = 4
* 那么就返回“2005年11月”
*/
public static String getNMonth(Date date, int n) {
GregorianCalendar currtime = new GregorianCalendar();
currtime.setTime(date);
currtime.add(GregorianCalendar.MONTH, -n);
int year = currtime.get(Calendar.YEAR);
int month = currtime.get(GregorianCalendar.MONTH) + 1;
return year + "年" + month + "月";
}
public static String getLastDate(Date date, int n){
GregorianCalendar currtime = new GregorianCalendar();
currtime.setTime(date);
currtime.add(GregorianCalendar.DAY_OF_MONTH, -n);
int year = currtime.get(Calendar.YEAR);
int month = currtime.get(GregorianCalendar.MONTH) + 1;
int days = currtime.get(GregorianCalendar.DAY_OF_MONTH);
return year + "-" + month + "-" + days;
}
public static String getNextHour(int n){
return n + ":00:00";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -