Basically im trying to make a countdown timer to specific date, but i dont want the output to be a standard yy/mm/dd/ss format, i want it to display the number of years and the a decimal percentage, like so : 16.39872937498273 years left until blah.. and so far i have created a system clock that updates as far as milliseconds but no further..
so how to get a date to convert to year + decimal and how do you update a clock farther than milliseconds?
so far this is my code, only counts up and displays system time, i need to change it to count down and to a date.
import javax.swing.*;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class Base extends JFrame{
private JPanel p1;
private JLabel time;
public static void main(String[] args){
Base myFrame = new Base();
myFrame.pack();
myFrame.setTitle("Digital Clock");
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setLocationRelativeTo(null);
myFrame.setVisible(true);
}//main()
public Base(){
System.out.println(currentTime());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
JLabel time = new JLabel(currentTime());
time.setFont(new Font("Gulim", Font.BOLD, 20));
time.setForeground(Color.blue);
p1.add(time);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.CENTER);
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println(currentTime());
time.setText(currentTime());
}
};
Timer t = new Timer(50, taskPerformer);
t.start();
}
public String currentTime(){
DateFormat df = new SimpleDateFormat("MM/dd/yy hh:mm:ssSSS");
Date dateobj = new Date();
String currentTime = df.format(dateobj);
return currentTime;
}
public String checkTime(int t){
String time1;
if (t < 10){
time1 = ("0"+t);
}
else{
time1 = (""+t);
}
return time1;
}
public String amP(int ap){
String amPm;
if( ap == 0)
amPm = "AM";
else
amPm = "PM";
return amPm;
}
}