简体   繁体   English

SimpleDateFormat解析非日期字符串

[英]SimpleDateFormat parsing a non date String

I'm facing a problem with SimpleDateFormat that's parsing a non date String. 我正面临着解析非日期字符串的SimpleDateFormat的问题。 Here's the code used reduced to minimum: 这里使用的代码减少到最小:

public class TestAddressAgainstDate {
  public static void main(String[] args) throws ParseException {
    SimpleDateFormat oFormat = new SimpleDateFormat( "yyyy-MM-dd" );
    String m_sColumnValue = "3-21-1, Nihombashi Hama-Cho";
    Date oDate = oFormat.parse( m_sColumnValue  );
    System.out.println(oDate);
  }
}

the output is the following: 输出如下:

Mon Sep 01 00:00:00 CET 4 Mon Sep 01 00:00:00 CET 4

The code this is from is testing whether the String is a date or not as the processor is also receiving date Strings from an XML. 来自的代码是测试String是否是日期,因为处理器也从XML接收日期字符串。 My problem is that I can't change the behaviour of getting the field as String and I need to deal with also getting dates from. 我的问题是我无法改变将字段作为字符串获取的行为,我还需要处理日期。

Anyone has an idea on how to solve this? 任何人都知道如何解决这个问题?

The original idea was to catch the ParseException and deal it as not a date but the japanese address ruined it. 最初的想法是捕获ParseException并将其作为日期处理,但日语地址毁了它。 :-) :-)

Thanks in advance 提前致谢

EDIT: 编辑:

I have another string which is causing problems even with setLenien(false) . 我有另一个字符串,即使使用setLenien(false)也会导致问题。

 String m_sColumnValue = "1-5-1 Ohtemachi, Chiyoda-Ku4";

anyone an Idea? 谁有想法?

使用setLenient验证输入日期String

oFormat.setLenient(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM