Gets the date before the current date (after) x days

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.