Search code examples
javaswingjtextfield

Converting string (hh:mm:ss) to integer


I'm having a problem converting time in the hh:mm:ss format to an integer.

Using JTextField I enter time in the 24 hour format but I've used a string and don't know how to turn it into a integer.

My code

Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat (" hh:mm:ss");
JTextField Stime = new JTextField(); 

String St = Stime.getText();      
Object[] message = {  "Insert Start Time (HH:MM:SS) ", Stime };
Stime.setText(ft.format(dNow));

I've used the JTextField because I'm going to allow the user to enter a different time within another JTextFieldand then calculate the difference

I can convert the string st into an integer and get no errors when I compile but lots of text when I run it which I think are caused by the semicolons but don't know how to fix it.


Solution

  • First, use HH for 24 hour format. (You had a space in front too.)

    SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
    
    Date answeredTime = ft.parse(st);
    long asSecondsSince1970 = answeredTime.getTime();