/** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateTime) { if (null == dateTime) { return null; } Integer minute = dateTime.getMinute(); minute = minute/5*5; return LocalDateTime.of(dateTime.getYear(), dateTime.getMonth(), dateTime.getDayOfMonth(), dateTime.getHour(), minute, 0); } /** * 最近的五分钟 * @param date * @return */ public static LocalDateTime getNear5(String date) { if (null == date) { return null; } LocalDateTime dateTime = LocalDateTime.parse(date,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); return getNear5(dateTime); } public static void main(String[] args) { LocalDateTime dateTime = LocalDateTime.now(); System.out.println(getNear5(dateTime)); }