使用SimpleDateFormat的问题 [英] Problems using SimpleDateFormat
问题描述
显然,我缺少一些基本的东西。我在格式化jspinner的值时遇到问题。我尝试了几种不同的方法并继续收到错误,没有跟踪它们,除了它与我如何从jspinner中获取值有关。
Apparently, I'm missing something fundamental. I'm having a problem with formatting the value of a jspinner. I've tried a couple different ways and keep receiving an error, didn't keep track of them, other than it has to do with how I'm trying to grab the value from jspinner.
以下是微调代码:
//setup date format for both spinners
SimpleDateFormat datePattern = new SimpleDateFormat("MM/dd/yyyy");
JSpinner dateFrom = new JSpinner(new SpinnerDateModel());
dateFrom.setEditor(new JSpinner.DateEditor(dateFrom, datePattern.toPattern()));
JPanel dateFromPanel = new JPanel(new GridLayout());
dateFromPanel.add(dateFrom);
dateFromPanel.setBorder(new TitledBorder("Date - From"));
以下是我目前正在尝试获取格式的方式:
Here is how I'm currently trying to get the format:
SimpleDateFormat sdfSource = new SimpleDateFormat("MM/dd/yyyy");
Date from = sdfSource.parse(dateFrom.getValue().toString());
SimpleDateFormat sdfDestination = new SimpleDateFormat("MM/dd/yyyy");
String dosFrom = sdfDestination.format(from);
当前错误:
线程main中的异常java.text.ParseException:无法解析的日期:2006年10月23日星期一00:00:00
Current error: Exception in thread "main" java.text.ParseException: Unparseable date: "Mon Oct 23 00:00:00 EDT 2006"
推荐答案
我怀疑问题是这样的:
dateFrom.getValue().toString()
我怀疑 dateFrom.getValue()
只返回日期
- 当然不会没有任何相关的格式。你应该只使用它来代替解析:
I suspect dateFrom.getValue()
is just returning a Date
- which of course doesn't have any associated format. Instead of parsing that, you should just cast it:
Date from = (Date) dateFrom.getValue();
我当然可能是错的......但那是我的第一个想法。
I could be wrong, of course... but that would be my first thought.
这篇关于使用SimpleDateFormat的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!