java time to increase the time to get a new time (date)

SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd")

LocalDate expirationDate
String expirDate="0"
Date date = df.parse (expirationDate as String) // specified date
Plus the number of days specified date expirDate = addDate (date, im.expiry2y) .format ( "yyyy-MM-dd") //
Get a new date
/ ** 
* Term increase in the number of days to give a new date
* @param DATE
* @param Day
* @return
* @throws Exception
* /
static ADDDATE a Date (DATE a Date, Long Day) throws Exception {
Long Time = date.getTime (); // get the date specified number of milliseconds
day = day * 24 * 60 * 60 * 1000; // number of days to add to the converted number of milliseconds
time + = day; // milliseconds obtained by adding new
return new date (time); // number of milliseconds to convert into a date
}


The following code is the increase in the number of years to date years
                        // turn the LocalDate DATE 
ZoneID ID of ZoneID ID of ZoneId.systemDefault = ()
ZonedDateTime ZDT = exdm.expirationDate.atStartOfDay (ZoneID ID of)
// add two years to the number of years
Calendar RightNow = Calendar.getInstance ()
rightNow.setTime (Date.from (ZDT. toInstant ()))
rightNow.add (Calendar.YEAR, 2) // two years to increase the number of years to get a new date
expirDate = df.format (rightNow.getTime ())




SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd")
Integer daysToExpire=180
Integer warnShelfLife = 60 // number of days of Advent
if (isValidDate (batchPart)) { // determines whether the correct time format 
formatter.setLenient (to false)
a Date newDate = formatter.parse (batchPart)
Formatter the SimpleDateFormat new new = ( "the MM-dd-YYYY")
String formatter.format DS = ( newDate)
a Date formatter.parse D = (DS)
expirDate = df.format (new new a Date (d.getTime () + ((im.daysToExpire: 0) - (im.warnShelfLife:?? 0)) * 24 * 60 * 1000L * 60)) AS String

}
the result is a new date
/ ** 
* determine whether the correct date format
* @param str
* @return
* /
static boolean isValidDate (String str) {
boolean to true convertSuccess =
// specify the date format is a four-year / two month / date two, attention yyyy / MM / dd case sensitive;
SimpleDateFormat the format = new new SimpleDateFormat ( "yyyyMMdd")
the try {
// lenient to set to false.
// otherwise SimpleDateFormat verify date may be less, such 2007/02/29 will be accepted, and converted to 2007/03/01
format.setLenient (to false)
format.parse (STR)
} the catch (the Throwable T) {
convertSuccess = to false
}
return convertSuccess
}

================== ========================================= Here's how to find online === ================================================== ==
com.date.test Package;


Import java.text.ParseException;
Import the java.text.SimpleDateFormat;
Import java.util.Date;


public the Test class {

public static void main (String [] args) {throws a ParseException
the SimpleDateFormat the dateFormat new new = the SimpleDateFormat ( "yyyy-MM-dd" ); // date format
date date = dateFormat.parse ( "2015-07-31" ); // specified date
date newDate = addDate (date, 20 ); // add the specified date 20 days
System.out.println (dateFormat.format (date)); // the output formatter date
System.out.println (dateFormat.format (newDate));
}


public static ADDDATE a date (dATE a date, Long Day) a ParseException {throws
 Long Time = date.getTime (); // get the date specified number of milliseconds
 day = day * 24 * 60 * 60 * 1000; // number of days to add to the converted number of milliseconds
 time + = day; // phase obtained by adding the new number of milliseconds
 return new Date (time); // number of milliseconds to convert into a date
}
}




Guess you like

Origin www.cnblogs.com/joker331/p/11590151.html