Determine if a String is a valid date before parsing

后端 未结 12 1707
半阙折子戏
半阙折子戏 2020-12-03 11:36

I have this situation where I am reading about 130K records containing dates stored as String fields. Some records contain blanks (nulls), some contain strings like this: \'

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 12:03

    If you formats are exact (June 7th 1999 would be either 07-Jun-99 or 07/06/1999: you are sure that you have leading zeros), then you could just check for the length of the string before trying to parse.

    Be careful with the short month name in the first version, because Jun may not be June in another language.

    But if your data is coming from one database, then I would just convert all dates to the common format (it is one-off, but then you control the data and its format).

提交回复
热议问题