import java.util.Calendar;
import java.util.Date;
import java.text.*;
public class DateDealWith
{
public DateDealWith(){
}
//取得系统当前时间,格式为yyyy-mm-dd
public String getCurrentDate(){
Calendar rightNow = Calendar.getInstance();
int year=rightNow.get(rightNow.YEAR);
int month=rightNow.get(rightNow.MONTH)+1;
int day=rightNow.get(rightNow.DATE);
return year+"-"+month+"-"+day;
}
//取得系统当前时间,格式为yyyy年mm月dd日
public String getCurrentDate1(){
Calendar rightNow = Calendar.getInstance();
int year=rightNow.get(rightNow.YEAR);
int month=rightNow.get(rightNow.MONTH)+1;
int day=rightNow.get(rightNow.DATE);
return year+"年"+month+"月"+day+"日";
}
//取得系统当前时间前n个月的相对应的一天
public String getNMonthBeforeCurrentDay(int n){
Calendar c = Calendar.getInstance();
c.add(c.MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得系统当前时间后n个月的相对应的一天
public String getNMonthAfterCurrentDay(int n){
Calendar c = Calendar.getInstance();
c.add(c.MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得系统当前时间前n天,格式为yyyy-mm-dd
public String getNDayBeforeCurrentDate(int n){
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得系统当前时间后n天,格式为yyyy-mm-dd
public String getNDayAfterCurrentDate(int n){
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//---------------------------------------------------------------------
//取过去一个时间对应的系统当年的一天
public String getCurrentDateAfterPastDate(String sPastDate){
if(sPastDate!=null && !sPastDate.equals("")){
Date date=switchStringToDate(sPastDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int iPastYear=c.get(c.YEAR);
Calendar c1 = Calendar.getInstance();
int iCurrentYear=c1.get(c1.YEAR);
c.add(c.YEAR,iCurrentYear-iPastYear);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}else{
return null;
}
}
//-----------------------------------------------------------------
//将一个日期字符串转化成日期
public Date switchStringToDate(String sDate){
Date date=null;
try{
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
date=df.parse(sDate);
}catch(Exception e){
System.out.println("日期转换失败:"+e.getMessage());
}
return date;
}
//输入两个字符串型的日期,比较两者的大小
public boolean compareTwoDateBigOrSmall(String fromDate,String toDate){
Date dateFrom = this.switchStringToDate(fromDate);
Date dateTo = this.switchStringToDate(toDate);
if(dateFrom.before(dateTo)){
return true;
}else{
return false;
}
}
//将一个日期字符串转化成Calendar
public Calendar switchStringToCalendar(String sDate){
Date date = switchStringToDate(sDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
//将一个日期转化成Calendar
public Calendar switchStringToCalendar(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
//------------------------------------------------------------------------
//取得某个时间前n个月的相对应的一天
public String getNMonthBeforeOneDay(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间后n个月的相对应的一天
public String getNMonthAfterOneDay(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间前n天,格式为yyyy-mm-dd
public String getNDayBeforeOneDate(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间后n天,格式为yyyy-mm-dd
public String getNDayAfterOneDate(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//判断系统当前时间是不是润年
public boolean isRunNian(){
java.util.Calendar rightNow = java.util.Calendar.getInstance();
int year=rightNow.get(rightNow.YEAR);
if(0==year%4 && (year%100!=0 || year%400==0)){
return true;
}else{
return false;
}
}
}
import java.util.Date;
import java.text.*;
public class DateDealWith
{
public DateDealWith(){
}
//取得系统当前时间,格式为yyyy-mm-dd
public String getCurrentDate(){
Calendar rightNow = Calendar.getInstance();
int year=rightNow.get(rightNow.YEAR);
int month=rightNow.get(rightNow.MONTH)+1;
int day=rightNow.get(rightNow.DATE);
return year+"-"+month+"-"+day;
}
//取得系统当前时间,格式为yyyy年mm月dd日
public String getCurrentDate1(){
Calendar rightNow = Calendar.getInstance();
int year=rightNow.get(rightNow.YEAR);
int month=rightNow.get(rightNow.MONTH)+1;
int day=rightNow.get(rightNow.DATE);
return year+"年"+month+"月"+day+"日";
}
//取得系统当前时间前n个月的相对应的一天
public String getNMonthBeforeCurrentDay(int n){
Calendar c = Calendar.getInstance();
c.add(c.MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得系统当前时间后n个月的相对应的一天
public String getNMonthAfterCurrentDay(int n){
Calendar c = Calendar.getInstance();
c.add(c.MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得系统当前时间前n天,格式为yyyy-mm-dd
public String getNDayBeforeCurrentDate(int n){
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得系统当前时间后n天,格式为yyyy-mm-dd
public String getNDayAfterCurrentDate(int n){
Calendar c = Calendar.getInstance();
c.add(c.DAY_OF_MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//---------------------------------------------------------------------
//取过去一个时间对应的系统当年的一天
public String getCurrentDateAfterPastDate(String sPastDate){
if(sPastDate!=null && !sPastDate.equals("")){
Date date=switchStringToDate(sPastDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int iPastYear=c.get(c.YEAR);
Calendar c1 = Calendar.getInstance();
int iCurrentYear=c1.get(c1.YEAR);
c.add(c.YEAR,iCurrentYear-iPastYear);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}else{
return null;
}
}
//-----------------------------------------------------------------
//将一个日期字符串转化成日期
public Date switchStringToDate(String sDate){
Date date=null;
try{
SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
date=df.parse(sDate);
}catch(Exception e){
System.out.println("日期转换失败:"+e.getMessage());
}
return date;
}
//输入两个字符串型的日期,比较两者的大小
public boolean compareTwoDateBigOrSmall(String fromDate,String toDate){
Date dateFrom = this.switchStringToDate(fromDate);
Date dateTo = this.switchStringToDate(toDate);
if(dateFrom.before(dateTo)){
return true;
}else{
return false;
}
}
//将一个日期字符串转化成Calendar
public Calendar switchStringToCalendar(String sDate){
Date date = switchStringToDate(sDate);
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
//将一个日期转化成Calendar
public Calendar switchStringToCalendar(Date date){
Calendar c = Calendar.getInstance();
c.setTime(date);
return c;
}
//------------------------------------------------------------------------
//取得某个时间前n个月的相对应的一天
public String getNMonthBeforeOneDay(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间后n个月的相对应的一天
public String getNMonthAfterOneDay(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间前n天,格式为yyyy-mm-dd
public String getNDayBeforeOneDate(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH,-n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//取得某个时间后n天,格式为yyyy-mm-dd
public String getNDayAfterOneDate(String sDate,int n){
Calendar c = switchStringToCalendar(sDate);
c.add(c.DAY_OF_MONTH,n);
return ""+c.get(c.YEAR)+"-"+(c.get(c.MONTH)+1)+"-"+c.get(c.DATE);
}
//判断系统当前时间是不是润年
public boolean isRunNian(){
java.util.Calendar rightNow = java.util.Calendar.getInstance();
int year=rightNow.get(rightNow.YEAR);
if(0==year%4 && (year%100!=0 || year%400==0)){
return true;
}else{
return false;
}
}
}