Reputation: 13506
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);
Upvotes: 2
Views: 258
Reputation: 86276
Two options:
Two quotes from the Joda-Time home page:
Users are now asked to migrate to
java.time
(JSR-310).Note that Joda-Time is considered to be a largely “finished” project. No major enhancements are planned. If using Java SE 8, please migrate to
java.time
(JSR-310).
The good news is you can migrate even if using Java 6 or 7. The developers of java.time (lead by Stephen Colebourne, also the lead developer of Joda-Time) have also developed the ThreeTen Backport, the backport of java.time for Java 6 and 7. See the link at the bottom.
Anton Balaniuc is already showing the code in his good answer, so there’s no use for me to repeat that here.
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
DateTimeFormatter parser = new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm")
.appendOptional(DateTimeFormat.forPattern(":ss").getParser())
.toFormatter();
DateTime dt1 = parser.parseDateTime(date1);
DateTime dt2 = parser.parseDateTime(date2);
System.out.println("dt1: " + dt1);
System.out.println("dt2: " + dt2);
On my computer in Europe/Copenhagen time zone the output from this snippet was:
dt1: 2018-10-12T18:01:01.000+02:00
dt2: 2018-10-12T18:01:00.000+02:00
As you can see, the key to specifying optional parts in the format is the appendOptional
method of DateTimeFormatterBuilder
.
java.time
.java.time
was first described.java.time
to Java 6 and 7 (ThreeTen for JSR-310).Upvotes: 2
Reputation: 11739
You can indicate that some parts of the format are optional using []
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm[:ss]");
LocalDateTime dateTime = LocalDateTime.parse("2018-10-12 18:01:01", formatter);
LocalDateTime dateTime1 = LocalDateTime.parse("2018-10-12 18:01", formatter);
System.out.println(dateTime + " " + dateTime1);
result is
2018-10-12T18:01:01 2018-10-12T18:01
Please see Patterns for Formatting and Parsing section for more info.
Upvotes: 3
Reputation: 472
You can use DateTimeFormatterBuilder
class, something like this.
private DateTimeFormatter formatterBuilder() {
return new DateTimeFormatterBuilder()
.appendPattern("yyyy-MM-dd HH:mm")
.optionalStart().appendPattern(":ss").optionalEnd()
.toFormatter();
}
Upvotes: 0