DateUtil { public static Calendar strToCalendar(String dateStr) { DateTimeFormatter format = DateTimeFormat.forPattern(TIME_PATTERN); DateTime dateTime = DateTime.parse(dateStr, format); return dateTime.toCalendar(Locale.CHINA); } private  DateUtil(); static String getLastDayStr(); static Date getLastDay(); static String getLastWeekStart(String date); static String getWeekStart(String date); static String getOneMonthAgo(); static String getNDay(int n); static Calendar strToCalendar(String dateStr); static Date dateStr2Date(String str); static Date timeStr2Date(String str); static boolean checkNMonthLegal(String dateFrom, String dataTo, int monthNumber); static boolean checkNowBeforeDate(Date date); static boolean checkNowBeforeDate(String dateStr); static boolean checkDateInterval(String startStr, String endStr, String dataStr); static Date getDelayTime(Date date, int second); static List<Date> getRandomDateList(Date startDate, Date endDate, int count);  }
@Test public void strToCalendar() throws Exception { String dateStr = "2018-03-31 22:22:22"; Calendar calendar = DateUtil.strToCalendar(dateStr); System.out.println("calendar, before add, getTime：" + calendar.getTime()); calendar.add(Calendar.MONTH, -1); System.out.println("calendar, after add, getTime：" + calendar.getTime()); System.out.println("calendar, day：" + calendar.get(Calendar.DAY_OF_YEAR)); }