小编Ola*_*ide的帖子

在当前时间基础上添加 30 分钟

我已经编写了下面的代码,但如果当前日期时间是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)

java java-time localdatetime

2
推荐指数
1
解决办法
4377
查看次数

标签 统计

java ×1

java-time ×1

localdatetime ×1