public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, 0);//年 cal.add(Calendar.MONTH, 0);//月 cal.add(Calendar.DATE, -1);//日,比当前时间小1天 cal.add(Calendar.HOUR_OF_DAY, -1);//时,HOUR_OF_DAY是24小时制,HOUR是12小时制 cal.add(Calendar.MINUTE, 0);//分 cal.add(Calendar.SECOND, 0);//秒 Date time = cal.getTime(); System.out.println(sdf.format(time)); }
public static void main(String[] args) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Calendar cal = Calendar.getInstance();//得到当前时间 cal.set(2013, 5, 4, 13, 44, 51);//年月日时分秒(月份0代表1月) cal.set(Calendar.YEAR, 2014);//年 cal.set(Calendar.MONTH, 7);//月(月份0代表1月) cal.set(Calendar.DATE, 11);//日 cal.set(Calendar.HOUR_OF_DAY, 15);//时 cal.set(Calendar.MINUTE, 33);//分 cal.set(Calendar.SECOND, 32);//秒 Date time = cal.getTime(); System.out.println(sdf.format(time));//2014-08-11 15:33:32 }