我已经编写了下面的代码,但如果当前日期时间是2022-07-03 09:48:05.448并且我添加30 分钟,我的响应将返回2022-07-03 09:79:05.448。
但分钟永远不可能是 79,它应该改为小时......
public static String getExpiryDate(int additionalMinutesToCurrentMinute) {
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int month = now.getMonthValue();
int day = now.getDayOfMonth();
int hour = now.getHour();
int minute = now.getMinute() + additionalMinutesToCurrentMinute;
int second = now.getSecond();
int millis = now.get(ChronoField.MILLI_OF_SECOND); // Note: no direct getter available.
String expiryDateAndTime = String.format("%d-%02d-%02d %02d:%02d:%02d.%03d", year, month, day, hour, minute, second, millis);
return expiryDateAndTime;
}
Run Code Online (Sandbox Code Playgroud)