package smile.xlw.test;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 描述:
* 作者:liqijing
* 修改日期:2015-5-23下午07:58:08
* E-mail: lijinginsistsmile@163.com
*/
public class DateTime {
public static void main(String[] args) {
int count = getday("1994-07-25");
System.out.println("人生中第"+count+"天");
String date = getReDay(7607,"1994-07-25");
System.out.println(date);
String dateTime = getReservationVisiDay(10);
System.out.println("十天后:"+dateTime);
}
/**
* 方法名称: getday
* 描述:根据你输入出生日期到当今天人生中第多少天
* 例如:1994-07-25到2015-05-23(7607)
* 作者: lijing
* 修改日期:2015-5-23下午07:35:40
* @param date
* @return
*/
public static int getday(String date){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date time = null ;
try {
time = sdf.parse(date);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
Calendar endday = Calendar.getInstance();
cal.setTime(time);
return getIntervalDays(cal, endday);
}
/**
* 方法名称: getIntervalDays
* 描述:根据你输入出生日期到当今天人生中第多少天
* 1994-07-25到2015-05-23(7607)
* 作者: lijing
* 修改日期:2015-5-23下午07:35:37
* @param startday
* @param endday
* @return
*/
public static int getIntervalDays(Calendar startday,Calendar endday){
if(startday.after(endday)){
Calendar cal=startday;
startday=endday;
endday=cal;
}
long sl=startday.getTimeInMillis();
long el=endday.getTimeInMillis();
long ei=el-sl;
return (int)(ei/(1000*60*60*24));
}
/**
* 方法名称: getReservationVisiDay
* 描述:当前日期加+day
* 例如:当期日期2015-05-23+10()
* 作者: liqijing
* 修改日期:2015-5-23下午07:17:23
* @param day
* @return
*/
public static String getReservationVisiDay(int day){
String thatDay = "" ;
if("今天".equals(day)){
thatDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
} else {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
cal.add(Calendar.DATE, day);
thatDay = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
}
return thatDay ;
}
/**
* 方法名称: getReDay
* 描述:根据你传入天数,出生日期
* 作者: liqijing
* 修改日期:2015-5-23下午07:35:34
* @param day
* @return
*/
public static String getReDay(int day,String dayTime){
String thatDay = "" ;
Date date = null;
try {
date = new SimpleDateFormat("yyyy-MM-dd").parse(dayTime);
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if("今天".equals(day)){
thatDay = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
} else {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, day);
thatDay = (new SimpleDateFormat("yyyy-MM-dd")).format(cal.getTime());
}
return thatDay ;
}
}