java 獲取日期相差天數 月數 獲得伺服器時間
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author zhangfj @email [email protected]
* @date 2012-4-19 下午10:01:13
* 對日期進行處理
*/
public class DateUtil {
public static Date getSystemTime(){
return getSystemTime("1");
}
public static String getStrSystemTime(){
return getStrSystemTime("1");
}
/**
* 獲取系統時間
* @param type: 1 獲取日期 2 獲取詳細時間
* @return 日期型別時間
* @throws ParseException
*/
public static Date getSystemTime(String type) {
String Pattern ="yyyy-MM-dd";
if(type.equals("1")){
Pattern ="yyyy-MM-dd";
}
if(type.equals("2")){
Pattern ="yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(Pattern);
Calendar calendar = Calendar.getInstance();
sdf.format(calendar.getTime());
Date date = null;
try {
date = sdf.parse(sdf.format(calendar.getTime()));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
/**
* 獲取系統時間
* @param type: 1 獲取日期 2 獲取詳細時間
* @return 字串型別時間
* @throws ParseException
*/
public static String getStrSystemTime(String type) {
String Pattern ="yyyy-MM-dd";
if(type.equals("1")){
Pattern ="yyyy-MM-dd";
}
if(type.equals("2")){
Pattern ="yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat formatter = new SimpleDateFormat(Pattern);
Calendar calendar = Calendar.getInstance();
formatter.format(calendar.getTime());
String strdate = null;
strdate = formatter.format(calendar.getTime());
return strdate;
}
/**
* 獲取日期相差天數
* @param
* @return 日期型別時間
* @throws ParseException
*/
public static Long getDiffDay(String beginDate,String endDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Long checkday=0l; //開始結束相差天數
try {
checkday = (formatter.parse(endDate).getTime()- formatter.parse(beginDate).getTime())/(1000*24*60*60);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
checkday=null;
}
return checkday;
}
public static Long getDiffDay(Date beginDate,Date endDate) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String strBeginDate = format.format(beginDate);
String strEndDate = format.format(endDate);
return getDiffDay(strBeginDate,strEndDate);
}
/**
* 獲取日期相月數
* @param
* @return 日期型別時間
* @throws ParseException
*/
public static int getDiffMonth(String beginDate,String endDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date dbeginDate = null;
Date dendDate = null;
try {
dbeginDate = formatter.parse(beginDate);
dendDate = formatter.parse(endDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getDiffMonth(dbeginDate,dendDate);
}
public static int getDiffMonth(Date beginDate,Date endDate) {
Calendar calbegin = Calendar.getInstance();
Calendar calend = Calendar.getInstance();
calbegin.setTime(beginDate);
calend.setTime(endDate);
int m_begin = calbegin.get(Calendar.MONTH)+1; //獲得合同開始日期月份
int m_end = calend.get(Calendar.MONTH)+1; //獲得合同結束日期月份
int checkmonth = m_end-m_begin+(calend.get(Calendar.YEAR)-calbegin.get(Calendar.YEAR))*12;//獲得合同結束日期於開始的相差月份
return checkmonth;
}
}