import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 时间处理工具类 <br>
*
* @author Davint
* @since 5.0
* @version 1.0
*/
public class TimerUtil {
private SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
private static TimerUtil cc = new TimerUtil();
public static TimerUtil getInstance() {
return cc;
}
public static void main(String[] args) {
TimerUtil timer=TimerUtil.getInstance();
System.out.println(timer.getDaysByDate(timer.getDate("2012-04-01", "yyyy-MM-dd")));
}
//根据日期得到当月天数
public int getDaysByDate(Date date) {
Calendar ca=Calendar.getInstance();
ca.setTime(date);
int year=ca.get(Calendar.YEAR);
int month=ca.get(Calendar.MONDAY)+1;
int day=0;
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
if (month==2) {
day=29;
}
}else {
if (month==2) {
day=28;
}
}
if (month==4||month==6||month==9||month==11) {
day=30;
}
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12) {
day=31;
}
return day;
}
/**
* 上周一的日期
* 将周一作为一周的第一天
*/
public Date getLastWeekFirstDay() {
Calendar ca = Calendar.getInstance();
if (ca.get(Calendar.DAY_OF_WEEK)-1==0) {
ca.add(Calendar.DATE, -1 * 7*2);
ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return ca.getTime();
}
ca.add(Calendar.DATE, -1 * 7);
ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return ca.getTime();
}
// 本周一日期
public Date getThisWeekFirstDay() {
Calendar ca=Calendar.getInstance();
if (ca.get(Calendar.DAY_OF_WEEK)-1==0) {
ca.add(Calendar.DATE, -1);
ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return ca.getTime();
}
ca.add(Calendar.DATE, 0);
ca.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return ca.getTime();
}
//两个月前的第一天
public Date getLast2MonFirstDay() {
Calendar cal=Calendar.getInstance();
Date now=new Date();
cal.setTime(now);
cal.add(Calendar.MONTH, -1);
int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
String resultTime=year+"-"+month+"-01";
Date date =null;
try {
date=sp.parse(resultTime);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
// 上个月第一天日期
public Date getLastMonFirstDay() {
Calendar cal=Calendar.getInstance();
Date now=new Date();
cal.setTime(now);
int year=cal.get(Calendar.YEAR);
int month=cal.get(Calendar.MONTH);
String resultTime=year+"-"+month+"-01";
Date date =null;
try {
date=sp.parse(resultTime);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
// 本月第一天
public Date getThisMonFirstDay() {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH) + 2;
cal.set(Calendar.DAY_OF_MONTH, 1);
cal.add(Calendar.DAY_OF_MONTH, -1);
int day = cal.get(Calendar.DAY_OF_MONTH);
String months = "";
String days = "";
if (month > 1) {
month--;
} else {
year--;
month = 12;
}
if (!(String.valueOf(month).length() > 1)) {
months = "0" + month;
} else {
months = String.valueOf(month);
}
if (!(String.valueOf(day).length() > 1)) {
days = "0" + day;
} else {
days = String.valueOf(day);
}
String firstDay = "" + year + "-" + months + "-01";
String[] lastMonth = new String[2];
lastMonth[0] = firstDay;
try {
return sp.parse(firstDay);
} catch (ParseException e) {
}
return null;
}
// 昨天日期
public String getYesterday() {
Date today = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(today);
ca.add(Calendar.DAY_OF_YEAR, -1);
return sp.format(ca.getTime());
}
// 7天前日期
public String getSevenDayAgo() {
Date today = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(today);
ca.add(Calendar.DAY_OF_YEAR, -7);
return sp.format(ca.getTime());
}
// 3天前日期
public String getThreeDayAgo() {
Date today = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(today);
ca.add(Calendar.DAY_OF_YEAR, -3);
return sp.format(ca.getTime());
}
// 一月前日期
public String getOneMonthAgo() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -1); // 得到前一个月
long date = cal.getTimeInMillis();
Date kk = new Date(date);
return sp.format(kk);
}
// 3月前日期
public String getThreeMonthAgo() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, -3);
long date = cal.getTimeInMillis();
Date kk = new Date(date);
return sp.format(kk);
}
/**
* getDate<br>
* 格式:yyyy-MM-dd 00:00:00 eg.2013-08-19 00:00:00<br>
* @param type -1表示昨天;-2表示前天;以此类推-N表示前N天
* @return java.util.Date
*/
public Date getDate(int type) {
Date resultDate = null;
Date today = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(today);
ca.add(Calendar.DAY_OF_YEAR, type);
// 将时间转换成00:00:00
try {
resultDate = sp.parse(sp.format(ca.getTime()));
return resultDate;
} catch (ParseException e) {
e.printStackTrace();
}
return resultDate;
}
/**
* N天前的具体时间
*
* @param n 0 表示今天,-1代表昨天
* @param type yyyyMMddHHmmss、yyyy-MM-dd HH:mm:ss
* @return
*/
public String getSomeDaysAgoString(int n, String type) {
Date today = new Date();
SimpleDateFormat sp = new SimpleDateFormat();
sp.applyPattern(type);
Calendar ca = Calendar.getInstance();
ca.setTime(today);
ca.add(Calendar.DAY_OF_YEAR, n);
return sp.format(ca.getTime());
}
/**
* N天前的具体时间
*
* @param n 0 表示今天,-1代表昨天
* @return
*/
public Date getSomeDaysAgoDate(int n) {
Date today = new Date();
Calendar ca = Calendar.getInstance();
ca.setTime(today);
ca.add(Calendar.DAY_OF_YEAR, n);
return ca.getTime();
}
/**
* 格式化某一时间
*
* @param date
* @param type
* @return
*/
public String getString(Date date, String type) {
SimpleDateFormat sp = new SimpleDateFormat();
sp.applyPattern(type);
if (date != null && type != null) {
return sp.format(date);
}
return "时间格式化异常!";
}
/**
* 格式化某一时间字符串
*
* @param date
* @param type
* @return
*/
public Date getDate(String date, String type) {
SimpleDateFormat sp = new SimpleDateFormat();
sp.applyPattern(type);
try {
if (date != null && type != null) {
return sp.parse(date);
}
return null;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 某日期前、后N天的日期
* @param date 某一日期
* @param type N天 0表示当天,N或+N表示后N天的日期,-N表示前N天的日期
* @return java.util.Date
*/
public Date getDate(Date date,int type) {
Calendar cal=Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_YEAR, type);
return cal.getTime();
}
}
java 日期工具类