我想在我的代码中向下舍入到最后5分钟,但它无效:
Date whateverDateYouWant = new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(whateverDateYouWant);
int unroundedMinutes = calendar.get(Calendar.MINUTE);
int mod = unroundedMinutes % 5;
calendar.add(Calendar.MINUTE, -(mod < 3 ? -mod : (5-mod)));
SimpleDateFormat format1 = new SimpleDateFormat("dd.MM.yyyy HH:mm");
String formattedDate = format1.format(calendar.getTime());
所以,如果它的08:16应该是08:15,如果它应该是09:49那么它应该是09:45而如果它是12点那么应该是08:15 :55
答案 0 :(得分:1)
替换此行:
calendar.add(Calendar.MINUTE, -(mod < 3 ? -mod : (5-mod)));
有了这个:
calendar.add(Calendar.MINUTE, unroundedMinutes == 0 ? -5 : -mod);