简体   繁体   中英

SimpleDateFormat parsing a non date String

I'm facing a problem with SimpleDateFormat that's parsing a non date String. 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

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. 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. :-)

Thanks in advance

EDIT:

I have another string which is causing problems even with setLenien(false) .

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

anyone an Idea?

使用setLenient验证输入日期String

oFormat.setLenient(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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