In java.time.LocalTime
, there is addSeconds()
method.
Can it be used for adding 1 second continually?
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.
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
. How to make it 00:01:00
for continous counting for seconds?
The problem is that you are using java.sql.Time.valueOf(String)
and depend on LocalTime.toString()
format which may not print seconds because:
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)
:
LocalTime localTime = Time.valueOf("00:00:59").toLocalTime();
localTime = localTime.plusSeconds(1);
System.out.println(Time.valueOf(localTime)); // 00:01:00