日期:2014-05-17 浏览次数:20804 次
public static String twoYearsTime(String fmt) {
Calendar cal = new GregorianCalendar();
int year = cal.get(Calendar.YEAR)+2;//yy 直接计算年数+2
int month = cal.get(Calendar.MONTH) + 1;//MM
int day = cal.get(Calendar.DATE);//dd
int hour = cal.get(Calendar.HOUR_OF_DAY);//HH
int minute = cal.get(Calendar.MINUTE);//mm
if (fmt.indexOf("yy") != -1) {
fmt = fmt.replaceAll("yy", String.valueOf(year).substring(2));
}
if (fmt.indexOf("MM") != -1) {
fmt = fmt.replaceAll("MM", month < 10 ? "0" + String.valueOf(month)
: String.valueOf(month));
}
if(fmt.indexOf("HH")!=-1){
fmt = fmt.replaceAll("HH", hour<10 ? "0" + String.valueOf(hour):String.valueOf(hour));
}
if (fmt.indexOf("dd") != -1) {
fmt = fmt.replaceAll("dd", day < 10 ? "0" + String.valueOf(day)
: String.valueOf(day));
}
if (fmt.indexOf("mm") != -1) {
fmt = fmt.replaceAll(
"mm",
minute < 10 ? "0" + String.valueOf(minute) : String
.valueOf(minute));
}
return fmt;
}
public static void main(String[] args) {
String time = twoYearsTime("yyMMddHHmm");//预定格式为yyMMddHHmm
System.out.println(time);
}