The method in the Tools preview:
Tools Code:
public class DateUtil { /*** * 从yyyy-MM-dd HH:mm格式转成yyyyMMddHH格式 * @param dateStr * @return */ public static String formatStr(String dateStr){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm"); try { Date date = simpleDateFormat.parse(dateStr); simpleDateFormat = new SimpleDateFormat("yyyyMMddHH"); return simpleDateFormat.format(date); } catch (ParseException e) { e.printStackTrace(); } return null; } /*** * 获取指定日期的凌晨 * @return */ public static Date toDayStartHour(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); Date start = calendar.getTime(); return start; } /*** * 时间增加N分钟 * @param date * @param minutes * @return */ public static Date addDateMinutes(Date date,int minutes){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, minutes);// 24小时制 date = calendar.getTime(); return date; } / ** * * hour time increments N * @param hour * @return * / public static a Date addDateHour (a Date DATE, int hour) { Calendar Calendar = Calendar.getInstance (); calendar.setTime (DATE); Calendar.add (Calendar.HOUR, hour); // 24-hour DATE = calendar.getTime (); return DATE; } / ** * * menu acquisition time * @return * / public static List <a Date> getDateMenus () { //Defines a List <Date> set, all storage period List <Date> = a dates new new the ArrayList <a Date> (); // cycle 12 times a Date DATE = toDayStartHour ( new new a Date ()); // morning for ( int I = 0; I <12 is; I ++ ) { // incremented each time for 2 hours, to save time in each increment of List <Date> set dates.add (addDateHour (DATE, I * 2 )); } // Analyzing time range to which the current time belongs to a Date now = new new a Date (); for (a Date CDate: a dates) { // begin time <= current time <start time +2 hours if(cdate.getTime()<=now.getTime() && now.getTime()<addDateHour(cdate,2).getTime()){ now = cdate; break; } } //当前需要显示的时间菜单 List<Date> dateMenus = new ArrayList<Date>(); for (int i = 0; i <5 ; i++) { dateMenus.add(addDateHour(now,i*2)); } return dateMenus; } /*** * 时间转成yyyyMMddHH * @param date * @return */ public static String date2Str(Date date){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHH"); return simpleDateFormat.format(date); } }