[英]SimpleDateFormat parse for time
i need to parse a String value and get it as date object. 我需要解析一个String值并将其作为日期对象。 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
我的输出是: 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? 但是我只需要14:58:00
因为Date对象有什么方法可以实现? 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 . 我有一个Jspinner(在Jpanel中)用于以HH:mm:ss格式选择时间。 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). 场景是我将打开JPanel来选择时间,然后单击Save按钮,这样选定的值将保存在变量(x)中。现在,如果我再次打开Jpanel,则应显示先前选择的值,并从中读取变量(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. 我只需要时间(14:58:00)作为日期对象。
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}));
您可能想使用new SimpleDateFormat("HH:mm:ss").formatDate(date)
(可以使用相同的new SimpleDateFormat("HH:mm:ss").formatDate(date)
实例)进行输出...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.