Now I have two different formats of date written in string:
String date1 = "2018-10-12 18:01:01";// yyyy-MM-dd HH:mm:ss
String date2 = "2018-10-12 18:01";//yyyy-MM-dd HH:mm
I am using joda
and I want to convert the string to DateTime
,the basic way is to use two formatter to parse each of them:
DateTimeFormatter formatter1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter formatter2 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm");
DateTime dt1 = formatter1.parseDateTime(date1);
DateTime dt2 = formatter2.parseDateTime(date2);
Above code blocks works fine but it created two formatter,since the date formate is very similar(the latter one just lack of seconds),I am wonder if there is a way that I can just use one formatter to parse all of them or I have to use two formatter?
Note:
due to the production enviroment limit,I can not use java8
now,so I want to the answer based on joda
Thanks in advance!
I just tried as below,and got IllegalArgumentException: Invalid format
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dt1 = formatter.parseDateTime(date1);
DateTime dt2 = formatter.parseDateTime(date2);