1  import java.util.Calendar;
  2  import java.util.TimeZone;
  3  import java.util.GregorianCalendar;
  4  import java.text.*;
  5  import java.util.Locale;
  6  import javafx.animation.KeyFrame;
  7  import javafx.animation.Timeline;
  8  import javafx.event.ActionEvent;
  9  import javafx.event.EventHandler;
 10  import javafx.geometry.Pos;
 11  import javafx.scene.control.Label;
 12  import javafx.scene.layout.BorderPane;
 13  import javafx.util.Duration;
 14  
 15  public class WorldClock extends BorderPane {
 16    private TimeZone timeZone = TimeZone.getTimeZone("EST");
 17    private Locale locale = Locale.getDefault();
 18    private ClockPane clock = new ClockPane(); // Still clock
 19    private Label lblDigitTime = new Label();
 20  
 21    public WorldClock() {
 22      setCenter(clock);
 23      setBottom(lblDigitTime);
 24      BorderPane.setAlignment(lblDigitTime, Pos.CENTER);
 25      
 26      EventHandler<ActionEvent> eventHandler = e -> {
 27        setCurrentTime(); // Set a new clock time
 28      };
 29      
 30      // Create an animation for a running clock
 31      Timeline animation = new Timeline(
 32        new KeyFrame(Duration.millis(1000), eventHandler));    
 33      animation.setCycleCount(Timeline.INDEFINITE);
 34      animation.play(); // Start animation
 35      
 36      // Resize the clock 
 37      widthProperty().addListener(ov -> clock.setWidth(getWidth()));
 38      heightProperty().addListener(ov -> clock.setHeight(getHeight()));
 39    }
 40  
 41    public void setTimeZone(TimeZone timeZone) {
 42      this.timeZone = timeZone;
 43    }
 44  
 45    public void setLocale(Locale locale) {
 46      this.locale = locale;
 47    }
 48      
 49    private void setCurrentTime() {
 50      Calendar calendar = new GregorianCalendar(timeZone, locale);
 51      clock.setHour(calendar.get(Calendar.HOUR));
 52      clock.setMinute(calendar.get(Calendar.MINUTE));
 53      clock.setSecond(calendar.get(Calendar.SECOND));
 54  
 55      // Display digit time on the label
 56      DateFormat formatter = DateFormat.getDateTimeInstance
 57        (DateFormat.MEDIUM, DateFormat.LONG, locale);
 58      formatter.setTimeZone(timeZone);
 59      lblDigitTime.setText(formatter.format(calendar.getTime()));
 60    }
 61  }