public static String before30Day(String time) { DateFormat dateFromat = new SimpleDateFormat(TIME_STAMP_STANDARD); try { Date parse = dateFromat.parse(time); Calendar cal = Calendar.getInstance();// get a calendar by using default timezone and language cal.setTime(parse); cal.add(Calendar.DAY_OF_MONTH, -30); Date date = cal.getTime(); return dateFromat.format(date); } catch (ParseException e) { e.printStackTrace(); } return null; }
public static void main(String[] args) { //获取东八区时间 Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); //获取年 int year = c.get(Calendar.YEAR); //获取月份,会少一天。所以+1 int month = c.get(Calendar.MONTH) + 1; //获取当前天数 int day = c.get(Calendar.DAY_OF_MONTH); c.add(Calendar.YEAR,5); c.add(Calendar.DATE, +10); //获取当前小时 int h= c.get(Calendar.HOUR_OF_DAY); //获取当前分钟 int m = c.get(Calendar.MINUTE); //获取当前秒 int se = c.get(Calendar.SECOND); SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //格式化时间格式 String Now = s.format(c.getTime()); //获得当前日期 format(Obj):格式化一个对象以生成一个字符串。 System.out.println("当前时间:" + year + "-" + month + "-"+ day + " "+h+":" + m +":" + se); System.out.println("当前日期Now:" + Now); }