简体   繁体   English

在java.sql.Time中localTime可以连续增加1秒吗?

[英]Can localTime be added by 1 second continually in java.sql.Time?

In java.time.LocalTime , there is addSeconds() method.java.time.LocalTime ,有addSeconds()方法。 Can it be used for adding 1 second continually?可以用来连续增加1秒吗?

LocalTime localTime = Time.valueOf("00:00:00").toLocalTime();
localTime = localTime.plusSeconds(1L);
String output = localTime.toString();
whiteTime = Time.valueOf(output);

I added this part of error message from Intellij.我从 Intellij 添加了这部分错误消息。

00:00:58
respond to action of white: Black Time
00:00:59
respond to action of white: Black Time
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException
    at java.sql/java.sql.Time.valueOf(Time.java:109)
    at ui.TimerPanel.blackTimerTikTok(TimerPanel.java:71)
    at util.GameModel$2.actionPerformed(GameModel.java:87)
    at java.desktop/javax.swing.Timer.fireActionPerformed(Timer.java:317)
    at java.desktop/javax.swing.Timer$DoPostEvent.run(Timer.java:249)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

It looks it makes an error when it reached 00:00:59 .当它到达00:00:59时,它看起来会出错。 How to make it 00:01:00 for continous counting for seconds?如何使其00:01:00连续计数秒?

The problem is that you are using java.sql.Time.valueOf(String) and depend on LocalTime.toString() format which may not print seconds because:问题是您正在使用java.sql.Time.valueOf(String)并依赖于LocalTime.toString()格式,该格式可能不会打印秒数,因为:

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.所使用的格式将是输出完整时间值的最短格式,其中省略部分隐含为零。

You should use Time.valueOf(LocalTime) :您应该使用Time.valueOf(LocalTime)

LocalTime localTime = Time.valueOf("00:00:59").toLocalTime();
localTime = localTime.plusSeconds(1);
System.out.println(Time.valueOf(localTime)); // 00:01:00

To add to what @Karol Dowbecki answered, if you need to use a String representation of your LocalTime you can use DateTimeFormatter to keep the format consistent:要添加到什么@Karol Dowbecki回答,如果你需要用你的字符串表示LocalTime ,你可以使用DateTimeFormatter保持一致的格式:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");

String output = localTime.format(formatter);
whiteTime = Time.valueOf(output);

This will output 00:01:00 when the time rolls around and not give you the error这将在时间滚动时输出00:01:00并且不会给您错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM