Created
January 1, 2015 07:29
-
-
Save jherico/40e655690fe770916af7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.awt.BorderLayout; | |
| import java.awt.Dimension; | |
| import java.awt.Font; | |
| import java.time.Duration; | |
| import java.time.LocalDate; | |
| import java.time.LocalDateTime; | |
| import java.time.LocalTime; | |
| import java.time.temporal.ChronoUnit; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.ScheduledExecutorService; | |
| import java.util.concurrent.TimeUnit; | |
| import javax.swing.JFrame; | |
| import javax.swing.JLabel; | |
| public class Countdown { | |
| private static final Duration TIMEPOINT = Duration.ofSeconds(7037); | |
| private static final LocalDateTime MIDNIGHT = LocalDate.now().atTime(LocalTime.of(23, 59, 59)).plus(2, ChronoUnit.SECONDS); | |
| private JLabel timeLabel = new JLabel(""); | |
| private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); | |
| /** | |
| * Create the GUI and show it. For thread safety, this method should be | |
| * invoked from the event-dispatching thread. | |
| */ | |
| private Countdown() { | |
| // Create and set up the window. | |
| JFrame frame = new JFrame("FrameDemo"); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| frame.getContentPane().add(timeLabel, BorderLayout.CENTER); | |
| timeLabel.setPreferredSize(new Dimension(175, 100)); | |
| Font labelFont = timeLabel.getFont(); | |
| // Set the label's font size to the newly determined size. | |
| timeLabel.setFont(new Font(labelFont.getName(), Font.PLAIN, 48)); | |
| // Display the window. | |
| frame.pack(); | |
| frame.setVisible(true); | |
| executor.scheduleAtFixedRate(()->{ | |
| javax.swing.SwingUtilities.invokeLater(()->{ | |
| LocalDateTime now = LocalDateTime.now(); | |
| long until = now.until(MIDNIGHT, ChronoUnit.SECONDS); | |
| Duration x = TIMEPOINT.minus(until, ChronoUnit.SECONDS); | |
| timeLabel.setText(formatTime(until) + " \n" + formatTime(x.get(ChronoUnit.SECONDS))); | |
| }); | |
| }, 100, 100, TimeUnit.MILLISECONDS); | |
| } | |
| public static String formatTime(long time) { | |
| long hours = 0, minutes = 0, seconds = 0; | |
| while (time >= 3600) { | |
| time -= 3600; | |
| ++hours; | |
| } | |
| while (time >= 60) { | |
| time -= 60; | |
| ++minutes; | |
| } | |
| seconds = time; | |
| return String.format("%02d:%02d:%02d", hours, minutes, seconds); | |
| } | |
| public static void main(String[] args) { | |
| // Schedule a job for the event-dispatching thread: | |
| // creating and showing this application's GUI. | |
| javax.swing.SwingUtilities.invokeLater(new Runnable() { | |
| @Override | |
| public void run() { | |
| new Countdown(); | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment