Advertisement
pwtenny

Untitled

Jun 16th, 2025
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | Source Code | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.time.ZonedDateTime;
  4. import java.time.ZoneId;
  5. import java.time.format.DateTimeFormatter;
  6. import javax.swing.border.EmptyBorder;
  7.  
  8. public class TimeDisplayApp extends JFrame {
  9.  
  10.     private JLabel telAvivTimeLabel;
  11.     private JLabel tehranTimeLabel;
  12.  
  13.     public TimeDisplayApp() {
  14.         // Set the native Windows look and feel
  15.         try {
  16.             UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
  17.         }
  18.         catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
  19.             e.printStackTrace();
  20.             // Fallback to default if Windows look and feel is not available
  21.         }
  22.  
  23.         setTitle("World Clocks");
  24.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  25.         setSize(300, 150);
  26.         setLocationRelativeTo(null); // Center the window
  27.  
  28.         // Create a panel with a GridLayout for the labels
  29.         JPanel panel = new JPanel(new GridLayout(2, 1));
  30.         panel.setBorder(new EmptyBorder(10, 10, 10, 10)); // Add some padding
  31.  
  32.         telAvivTimeLabel = new JLabel();
  33.         tehranTimeLabel = new JLabel();
  34.  
  35.         // Set font for better readability
  36.         Font font = new Font("SansSerif", Font.BOLD, 24);
  37.         telAvivTimeLabel.setFont(font);
  38.         tehranTimeLabel.setFont(font);
  39.  
  40.         // Center the text in the labels
  41.         telAvivTimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
  42.         tehranTimeLabel.setHorizontalAlignment(SwingConstants.CENTER);
  43.  
  44.         panel.add(telAvivTimeLabel);
  45.         panel.add(tehranTimeLabel);
  46.  
  47.         add(panel);
  48.  
  49.         // Timer to update the time every second
  50.         Timer timer = new Timer(1000, e -> updateTimes());
  51.         timer.start();
  52.  
  53.         // Initial update
  54.         updateTimes();
  55.     }
  56.  
  57.     private void updateTimes() {
  58.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("hh:mm:ss a");
  59.  
  60.         // Tel Aviv time
  61.         ZoneId telAvivZone = ZoneId.of("Asia/Jerusalem");
  62.         ZonedDateTime telAvivDateTime = ZonedDateTime.now(telAvivZone);
  63.         telAvivTimeLabel.setText("Tel Aviv: " + telAvivDateTime.format(formatter));
  64.  
  65.         // Tehran time
  66.         ZoneId tehranZone = ZoneId.of("Asia/Tehran");
  67.         ZonedDateTime tehranDateTime = ZonedDateTime.now(tehranZone);
  68.         tehranTimeLabel.setText("Tehran: " + tehranDateTime.format(formatter));
  69.     }
  70.  
  71.     public static void main(String[] args) {
  72.         SwingUtilities.invokeLater(() -> {
  73.             new TimeDisplayApp().setVisible(true);
  74.         });
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement