【问题标题】:Parsing date. Can not parse the same format in java解析日期。无法在java中解析相同的格式
【发布时间】:2017-05-26 22:28:35
【问题描述】:

我正在从包含日期、收盘价、成交量等的 .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 日的日期,但是当我来到 28-Apr-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)

【问题讨论】:

  • 尝试为 SimpleDateFormat 构造函数提供美国语言环境。默认情况下,它将使用可能不是英语的默认语言环境。
  • 你最好使用java.time
  • 土耳其语言环境?其中 May 的缩写是 May(与英语一样),但 April 的缩写是 Nis(代表 Nisan)。

标签: java string date parsing


【解决方案1】:

尝试将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(字符串,区域设置) * SimpleDateFormat(模式,Locale.getDefault(Locale.Category.FORMAT))}。 * * @see java.util.Locale#getDefault(java.util.Locale.Category) * @see java.util.Locale.Category#FORMAT * @param pattern 描述日期和时间格式的模式 * @exception NullPointerException 如果给定模式为空 * @exception IllegalArgumentException 如果给定的模式无效 */ 公共 SimpleDateFormat(字符串模式) { 这(模式,Locale.getDefault(Locale.Category.FORMAT)); }

希望,这会有所帮助!

【讨论】:

  • 很高兴知道它对您有所帮助!
【解决方案2】:

您可能希望将语言环境添加到您的SimpleDateFormat,如下所示:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 2015-08-13
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    相关资源
    最近更新 更多