首页 > 代码库 > java获取当前时间前一个月的年月日时分秒

java获取当前时间前一个月的年月日时分秒

public static String getLastMonthTime(){
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH)+1;
int date = calendar.get(Calendar.DATE);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
return year+"-"+(month<10?"0"+month:month)+"-"+(date<10?"0"+date:date)+" "+(hour<10?"0"+hour:hour)+":"+(minute<10?"0"+minute:minute)+":"+(second<10?"0"+second:second);
}