如何将日历日期设置为 10 分钟?
how to ceil and floor a Calendar Date by 10 minnutes?
有一种更好的方法可以将日期时间四舍五入(上限和下限)以下代码的十分钟:
public void hintOfTime() {
Date orario = new SimpleDateFormat("hh:mm").parse(usaServizio_standard.setStr());
// I have skipped the try catch for an easy read
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(orario);
int minutes = calendar.get(Calendar.MINUTE);
if(minutes>=1 && minutes<=9) {
usaServizio_standard.hint("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":00 or "+calendar.get(Calendar.HOUR+1)+":10");
}else if(minutes>=11 && minutes<=19) {
usaServizio_standard.hint("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":10 or "+calendar.get(Calendar.HOUR+1)+":20");
}else if(minutes>=21 && minutes<=29) {
usaServizio_standard.hint("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":20 or "+calendar.get(Calendar.HOUR+1)+":30");
}else if(minutes>=31 && minutes<=39) {
usaServizio_standard.consiglio("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":30 or "+calendar.get(Calendar.HOUR+1)+":40");
}else if(minutes>=41 && minutes<=49) {
usaServizio_standard.consiglio("not valid date","Try with "+ calendar.get(Calendar.HOUR+1)+":40 or "+calendar.get(Calendar.HOUR+1)+":50");
}else {
usaServizio_standard.hint("not valid date","Try with"+ calendar.get(Calendar.HOUR+1)+":50 or "+calendar.get(Calendar.HOUR+2)+":00");
}
}
这个解决方案工作正常但太丑了。我不知道做同样的工作是否有更好更简短的解决方案。
java.time
我建议您使用 java.time,现代 Java 日期和时间 API,作为您的工作时间。
String timeString = "09:56";
LocalTime time = LocalTime.parse(timeString);
int minute = time.getMinute();
if (minute % 10 != 0) {
// truncate to whole 10 minutes
LocalTime firstSuggestedTime = time.withMinute(minute / 10 * 10);
LocalTime secondSuggestedTime = firstSuggestedTime.plusMinutes(10);
System.out.format("Not a valid time; try with %s or %s.%n",
firstSuggestedTime, secondSuggestedTime);
}
这个例子的输出是:
Not a valid time; try with 09:50 or 10:00.
Link
Oracle tutorial: Date Time 解释如何使用 java.time。
有一种更好的方法可以将日期时间四舍五入(上限和下限)以下代码的十分钟:
public void hintOfTime() {
Date orario = new SimpleDateFormat("hh:mm").parse(usaServizio_standard.setStr());
// I have skipped the try catch for an easy read
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(orario);
int minutes = calendar.get(Calendar.MINUTE);
if(minutes>=1 && minutes<=9) {
usaServizio_standard.hint("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":00 or "+calendar.get(Calendar.HOUR+1)+":10");
}else if(minutes>=11 && minutes<=19) {
usaServizio_standard.hint("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":10 or "+calendar.get(Calendar.HOUR+1)+":20");
}else if(minutes>=21 && minutes<=29) {
usaServizio_standard.hint("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":20 or "+calendar.get(Calendar.HOUR+1)+":30");
}else if(minutes>=31 && minutes<=39) {
usaServizio_standard.consiglio("not valid date","Try with: "+ calendar.get(Calendar.HOUR+1)+":30 or "+calendar.get(Calendar.HOUR+1)+":40");
}else if(minutes>=41 && minutes<=49) {
usaServizio_standard.consiglio("not valid date","Try with "+ calendar.get(Calendar.HOUR+1)+":40 or "+calendar.get(Calendar.HOUR+1)+":50");
}else {
usaServizio_standard.hint("not valid date","Try with"+ calendar.get(Calendar.HOUR+1)+":50 or "+calendar.get(Calendar.HOUR+2)+":00");
}
}
这个解决方案工作正常但太丑了。我不知道做同样的工作是否有更好更简短的解决方案。
java.time
我建议您使用 java.time,现代 Java 日期和时间 API,作为您的工作时间。
String timeString = "09:56";
LocalTime time = LocalTime.parse(timeString);
int minute = time.getMinute();
if (minute % 10 != 0) {
// truncate to whole 10 minutes
LocalTime firstSuggestedTime = time.withMinute(minute / 10 * 10);
LocalTime secondSuggestedTime = firstSuggestedTime.plusMinutes(10);
System.out.format("Not a valid time; try with %s or %s.%n",
firstSuggestedTime, secondSuggestedTime);
}
这个例子的输出是:
Not a valid time; try with 09:50 or 10:00.
Link
Oracle tutorial: Date Time 解释如何使用 java.time。