I was wondering if java comes with a built in way to parse times/dates like this one:
5m1w5d3h10m15s
It's 5 months, 1 week, 5 days, 3 hours, 10 minutes, 15 seconds.
I was wondering if java comes with a built in way to parse times/dates like this one:
5m1w5d3h10m15s
It's 5 months, 1 week, 5 days, 3 hours, 10 minutes, 15 seconds.
Yes, it's called SimpleDateFormat, you just have to define your pattern.
As you didn't (voluntarly?) precise the year, I added one :
DateFormat df = new SimpleDateFormat("M'm'W'w'F'd'H'h'm'm's's'yyyy");
System.out.println(df.parse("5m1w5d3h10m15s"+"2012"));
Of course there are some libraries available (many people redirect to Joda, which is better than the standard and confusing java libraries) but as your question is about "if java comes with a built in way to parse times/dates", the answer is a clear yes.
You probably want to parse this into a time period. rather than into a Date, unless your input looks like "2012y10m8d" etc. Expect to encounter many problems if you try to represent a period of time as a java.util.Date. Trust me, I've been down that path before.
Instead consider using Joda time for time periods. See this question for details: How do I parse a string like "-8y5d" to a Period object in joda time
HumanTime looks interesting too.
I would first reverse the string. Then, I would tokenize the string into 6 numeric, separate parts -- months, weeks, days, hours, minutes, seconds. I'd store each token in an array and treat each element separately: a[0] = seconds, a[1] = minutes, ... a[5] = months. That's the most intuitive way I can think of offhand since I can't recall any library that does this for you. And of course, you didn't specify what you mean by "parse."
I dont know about that specific format... but i suggest you take a look at this
also, it wouldn't be too difficult to build your own parser...
You can refer to this post for ideas: Java date format - including additional characters
And of course the SimpleDateFormat class for reference. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Using SimpleDateFormatter class will help you convert your string into a date. Using Joda time PeriodFormatter will allow you convert/express a period instead of a date. Eg. you will be able to express and parse something like: 15m1w5d3h10m15s
too.