public static String getLastYmd(String ymd) {
try {
//Date check
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
sdf.setLenient(false);
sdf.parse(ymd);
//Get year / month
int y = Integer.parseInt(ymd.substring(0, 4));
int m = Integer.parseInt(ymd.substring(4, 6));
//Get the last date of the acquired date
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, y);
cal.set(Calendar.MONTH, m - 1);
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE));
//Convert to YYYYMMDD format and return
return sdf.format(cal.getTime());
} catch (Exception ex) {
//Returns null when an exception occurs
return null;
}
}
public static String addYear(String fromYear, int year) {
//Generate SimpleDateFormat object for format conversion
DateFormat df = new SimpleDateFormat("yyyyMMdd");
//Create a Calendar object that holds the current date and time
Calendar cal = Calendar.getInstance();
try {
cal.setTime(df.parse(fromYear));
} catch (ParseException e) {
//TODO auto-generated catch block
e.printStackTrace();
}
//Add years
cal.add(Calendar.YEAR, year);
return getLastYmd(df.format(cal.getTime()));
}
Recommended Posts