解析日期。无法在java中解析相同的格式

时间:2017-05-26 22:28:36

标签: java string date parsing

我正在从包含日期,收盘价,交易量等的.csv文件中解析日期。

   SimpleDateFormat  sdf = new SimpleDateFormat("dd-MMM-yy");

   for(int i = 0; i<pastModel.getRowCount(); i++){

       if(pastModel.getValueAt(i, 0) != null ){

           myDate = sdf.parse(pastModel.getValueAt(i, 0).toString());


       s1.addOrUpdate(new Day(myDate), std.change(pastModel.getValueAt(i, 4).toString()));

       }

   }

我的csv文件包含以下数据。

5-May-17,2.60,2.64,2.60,2.61,830666

4-May-17,2.62,2.64,2.59,2.59,1204889

3-May-17,2.63,2.65,2.61,2.62,917924

2-May-17,2.69,2.69,2.62,2.62,1386661

28-Apr-17,2.69,2.72,2.68,2.69,1503999

27-Apr-17,2.71,2.73,2.68,2.69,1688354

26-Apr-17,2.71,2.75,2.69,2.70,5044999

25-Apr-17,2.67,2.72,2.66,2.70,4989761

24-Apr-17,2.68,2.69,2.66,2.66,1341020

21-Apr-17,2.67,2.68,2.63,2.64,1177714

我可以解析日期到4月28日但是当我到了4月28日 - 4月17日我得到了以下输出。 csv文件中的所有日期格式都相同,但我不知道为什么会出现这种错误。

java.text.ParseException: Unparseable date: "28-Apr-17"
at java.text.DateFormat.parse(Unknown Source)
at bistx.Bistx.createDataset(Bistx.java:1331)
at bistx.Bistx.chart(Bistx.java:1213)
at bistx.Bistx.runChart(Bistx.java:1349)
at bistx.Bistx$13.actionPerformed(Bistx.java:655)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

您可能希望将区域设置添加到SimpleDateFormat,如下所示:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);

答案 1 :(得分:0)

尝试将Locale格式添加到SimpleDateFormat参数化构造函数中。

因此,如果我添加US区域设置,代码将如下所示。

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yy", Locale.US);

这就是我在SimpleDateFormat的javadocs中找到的用于将语言环境添加到其构造函数的内容。

  

/ **        *使用给定的模式构造SimpleDateFormat        *默认的默认日期格式符号        * {@link java.util.Locale.Category #FORMAT FORMAT}语言环境。        * 注意:此构造函数可能不支持所有语言环境。        *要获得完整报道,请使用{@link DateFormat}中的工厂方法        *上课。        *

这相当于打电话        * {@link #SimpleDateFormat(String,Locale)        * SimpleDateFormat(pattern,Locale.getDefault(Locale.Category.FORMAT))}。        *        * @see java.util.Locale #getDefault(java.util.Locale.Category)        * @see java.util.Locale.Category #FORMAT        * @param模式描述日期和时间格式的模式        * @exception NullPointerException如果给定的模式为null        * @exception IllegalArgumentException如果给定的模式无效        * /       public SimpleDateFormat(String pattern)       {           this(pattern,Locale.getDefault(Locale.Category.FORMAT));       }

希望,这有帮助!