Determine if a String is a valid date before parsing

后端 未结 12 1651
半阙折子戏
半阙折子戏 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条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 12:01

    you could use split to determine which format to use

    String[] parts = date.split("-");
    df = (parts.length==3 ? format1 : format2);
    

    That assumes they are all in one or the other format, you could improve the checking if need be

提交回复
热议问题