【问题标题】:How to combine time for a second and minute in java如何在java中结合一秒和一分钟的时间
【发布时间】:2021-07-02 07:09:56
【问题描述】:

我喜欢为我的项目合并时间还有其他方法吗?例如对于我的 yml 侦听器,都配置 minute:1 和 second: 18 所以我期望的时间是 78

下面是我的代码;

下面是我的yml;

listener:
  info:
    hour: 0
    minute: 0
    second: 18
    milliSecond: 0

下面是我的监听器配置;

@Configuration
public class EventListenerConfiguration {

    @Value("${listener.info.hour}")
    private int listenerInfoHour;

    @Value("${listener.info.minute}")
    private int listenerInfoMinute;

    @Value("${listener.info.second}")
    private int listenerInfoSecond;

    @Value("${listener.info.milliSecond}")
    private int listenerInfoMilliSecond;

  
    public int getTotalMilliSecondTimeForInfoForSecond() {
        return listenerErrorSecond * 1000 + listenerErrorMilliSecond + listenerErrorMinute * 6000 + listenerErrorHour * 360_000;
    }

    public int getTotalMilliSecondTimeForErrorForMinute() {
        return listenerErrorMinute * 60_000 + listenerErrorMilliSecond + listenerErrorSecond * 6000 + listenerErrorHour * 360_000;
    }

    public int getTotalMilliSecondTimeForErrorForHour() {
        return listenerErrorHour * 3_600_000 + listenerErrorMilliSecond + listenerErrorMinute * 6000 + listenerErrorSecond * 360_000;
    }

下面是我的消费类;

@Autowired
 EventListenerConfiguration eventListenerConfiguration;

private long lastReceivedMessage = System.currentTimeMillis();

@Scheduled(fixedDelayString = "${listenScheduled}", initialDelay = 1000)
    private void distanceBetweenLastReceivedMessageAndCurrentTime() {

        long currentTime = System.currentTimeMillis() - lastReceivedMessage;

        if (currentTime >= eventListenerConfiguration.getConcanteTotalMilliSecondTimeForErrorForSecondAndMinute()) {

            publishEvent("event info", EventSeverityStatus.ERROR, EventTypeStatus.ALERT, null);

所以我尝试了“getConcanteTotalMilliSecondTimeForErrorForSecondAndMinute()”方法,但不幸的是它没有连接秒和分钟。

    public int getConcanteTotalMilliSecondTimeForErrorForSecondAndMinute() {
        return getTotalMilliSecondTimeForErrorForSecond() + getTotalMilliSecondTimeForErrorForMinute();
    }

【问题讨论】:

  • 连接是什么意思?可以举个例子吗?
  • listener: info: hour: 0 minute: 0 second: 18 而不是 listener: info: hour: 0 minute: 1 second: 18 所以时间会显示为 78

标签: java spring multithreading datetime java-8


【解决方案1】:

不要自己做时间数学。使用库方法。您的代码将更清晰、更自然地阅读,并且更容易让您的读者相信它是正确的。在一段时间内使用 java.time 中的 Duration 类,现代 Java 日期和时间 API。

    long totalMilliseconds = Duration.ofHours(0)
                                     .plusMinutes(1)
                                     .plusSeconds(18)
                                     .plusMillis(0)
                                     .toMillis();
    System.out.println("" + totalMilliseconds + " milliseconds");

这个例子 sn-p 的输出是:

78000 毫秒

我假设您班级中的listenerInfoHour 代替0 作为ofHours() 的参数。同样.plusMillis(listenerInfoMilliSecond)。为了一个最小、简单和完整的示例,我只是在示例中放置了硬编码的数字。

链接

Oracle tutorial: Date Time 解释如何使用 java.time。

【讨论】:

    【解决方案2】:

    我需要这样计算:

    public int getTotalMilliSecondTime() {
        return listenerErrorMilliSecond + listenerErrorSecond * 1_000 + listenerErrorMinute * 60_000 + listenerErrorHour * 3_600_000;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2022-01-07
      • 2014-09-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多