3

i need to parse a String value and get it as date object. Am using the following snippet

  java.util.Date temp = new SimpleDateFormat("HH:mm:ss").parse("14:58:00");

My output is : Thu Jan 01 14:58:00 IST 1970

But what i need is only 14:58:00 as Date object is there any way to achieve this? If not what is the other way around? Please help.

EDIT I need this way because,

I have a Jspinner(in a Jpanel) for selecting time in HH:mm:ss format . The scenario is I will open the JPanel to select the time and click on save button so the values selected will be saved in a variable(x).Now if i open the Jpanel again the previously selected values should be shown which will be read from the variable(x).

For this am using following code,

    SpinnerModel Startmodel = new SpinnerDateModel();
    StarttimeSpinner = new JSpinner(Startmodel);
    Date date = new SimpleDateFormat("HH:mm:ss").parse("14:58:00");
    StarttimeSpinner.setValue(date);

On parse line am facing the problem. where i need only time(14:58:00) as date object.

NOTE: I have tried the below code( it will work but wont allow us to change value again)

    StarttimeSpinner = new JSpinner();
    Date time = new SimpleDateFormat("HHmmss", Locale.ENGLISH).parse("000000");
    SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    String dateString = formatter.format(time);
    StarttimeSpinner.setModel(new SpinnerListModel(new String[]{dateString}));
Baby
  • 5,062
  • 3
  • 30
  • 52
user3164187
  • 1,382
  • 3
  • 19
  • 50
  • 8
    A Date corresponds to a specific instant in time, which includes a day and a time. So to answer your question: no you can't get rid of the day part of the date. You should explain ***why*** you want to do that because there probably is a better way. – assylias Jan 17 '14 at 09:09
  • 2
    for more info on what can `Date` object be, refer [Date Documentation](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html) – Baby Jan 17 '14 at 09:12
  • Your dateString has different format... `HHmmss` vs. `HH:mm:ss`... – Betlista Jan 17 '14 at 09:22
  • @Betlista the code under NOTE is working as i used format(time), but its not allowing to change values again but automatically reassigns to old value. – user3164187 Jan 17 '14 at 09:25
  • 1
    This is an duplicity for this - http://stackoverflow.com/questions/9532663/formatting-the-date-on-a-jspinner – Betlista Jan 17 '14 at 09:25

1 Answers1

0

You want to use new SimpleDateFormat("HH:mm:ss").formatDate(date) (you can use same instance of formatter) for output probably...

Betlista
  • 10,327
  • 13
  • 69
  • 110