I want to add a clock to my application. I get the unixtime from the web through json, but if I ask for it every second, after a while it declines the answer. So now I want to get the unixtime only once, when I start the application, then increase the long value by one, every second and display that.
private void setDateLabel() throws IOException, JSONException {
long unixtime = Unixtime.getTime();
Timeline clock = new Timeline(new KeyFrame(Duration.ZERO, e -> {
try {
Date date = new Date(unixtime * 1000L);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatDate = sdf.format(date);
dateLabel.setText(formatDate);
unixtime++;
} catch (Exception ex) {
Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
}
}), new KeyFrame(Duration.seconds(1)));
clock.setCycleCount(Animation.INDEFINITE);
clock.play();
}
I have this, but it is not working.
The Unixtime.getTime() does not do anything else than giving back a long number.