Reputation: 3025
How can I extract a datetime pattern from any string in Java?
The pattern is "YYYYMMddkm"
.
Are there any Joda time helper methods for this?
String 1: 2014041507393_somefile.somemore
String 2: _somefile2014041507393.somemore
Upvotes: 3
Views: 1410
Reputation: 13427
Use the parse
method of SimpleDateFormat
:
SimpleDateFormat format = SimpleDateFormat("YYYYMMddkm");
format.parse(string, pos);
Upvotes: 0
Reputation: 2219
First use regular expression and then parse string in Java for use later:
if (str.matches("\\d{4}-\\d{2}-\\d{2}")) {
Date date = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(str);
System.out.println(date); // Example: Sat May 14 00:00:00 BOT 2014
}
Upvotes: 0
Reputation: 3281
You will have to get rid of letters yourself. I don't think Joda time will have any methods to do this for you.
Upvotes: 0
Reputation: 2716
Use the following regular expression:
^\d{4}-\d{2}-\d{2}$
as in
String date = "2014041507393_somefile.somemore";
if (str.matches("\\d{4}-\\d{2}-\\d{2}")) {
...
}
Upvotes: 3