Package com.xcfh.util;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Calendar;
Import Java.util.Date;
/**
*
* @ClassName: Beforeorafterdate
*
* @Description: Gets the date before (after) x days before the current input date
* @author Pinetree
* @date December 29, 2014 3:52:46
* @version
*/
public class Beforeorafterdate {
/**
*
* Check the date before the current date (after) x days
*
* @param date Current
* @param days (if day is a negative number, the number of days before this date)
* @return Yyyy-mm-dd
*/
Public String befornumday (date date, Int. day) {
Calendar C = calendar.getinstance ();
C.settime (date);
C.add (Calendar.day_of_year, day);
return new SimpleDateFormat ("Yyyy-mm-dd"). Format (C.gettime ());
}
/**
*
* Check the date before the current date (after) x days
*
* @param date Current
* @param days (if day is a negative number, the number of days before this date)
* @return YyyyMMdd
*/
Public String befornumberday (date date, Int. day) {
Calendar C = calendar.getinstance ();
C.settime (date);
C.add (Calendar.day_of_year, day);
return new SimpleDateFormat ("YyyyMMdd"). Format (C.gettime ());
}
/**
* Check the date before the current date (after) x days
*
* @param millis Current date milliseconds
* @param days (if day is a negative number, the number of days before this date)
* @return long milliseconds only show to days, time is all 0
* @throws ParseException
*/
Public long Befordatenum (long Millis, Int. day) throws ParseException {
Calendar C = calendar.getinstance ();
C.settimeinmillis (Millis);
C.add (Calendar.day_of_year, day);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = new Date (C.gettimeinmillis ());
Date newdate = Sdf.parse (Sdf.format (date));
return Newdate.gettime ();
}
/**
* Check the date before the current date (after) x days
*
* @param millis Current date milliseconds
* @param days (if day is a negative number, the number of days before this date)
* @return Yyyy-mm-dd
*/
Public String beforlongdate (Long Millis, Int. day) {
Calendar C = calendar.getinstance ();
C.settimeinmillis (Millis);
C.add (Calendar.day_of_year, day);
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date date = new Date (C.gettimeinmillis ());
return Sdf.format (date);
}
public static void Main (string[] args) {
try{
beforeorafterdate bd = new Beforeorafterdate ();
Long nowdate = System.currenttimemillis ();
System.out.println ("nowdate =" + nowdate);
Long befordate = Bd.befordatenum (nowdate, 3);
System.out.println ("befordate =" + befordate);
Date date = new Date (befordate);
System.out.println ("millisecond value result date =" + date.tolocalestring ());
System.out.println ("yyyymmdd Result date =" + Bd.befornumberday (new Date (nowdate), 3));
System.out.println ("yyyy-mm-dd Result date =" + Bd.befornumday (new Date (nowdate), 3));
System.out.println ("Millisecond value gets date result =" + Bd.beforlongdate (nowdate, 3));
}catch (Exception e) {
E.printstacktrace ();
}
}
}
Gets the date before the current date (after) x days