3

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.

ЯegDwight
  • 24,821
  • 10
  • 45
  • 52
Christopher
  • 451
  • 1
  • 6
  • 10

6 Answers6

9

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    Don't. That will parse it into a Date, which is meant to represent a specific time in history. 5 months is a period of time, which is very different. – johv Oct 08 '12 at 19:08
  • The question states that it's about "parse times/dates". But I'm not sure this wasn't a bad phrasing from OP. If the real question is, in fact, about durations, my answer doesn't help. But then... if it's about duration the question doesn't make much sense as months have a variable duration... – Denys Séguret Oct 08 '12 at 19:10
  • Interesting point about months having variable duration! It could still be a time period though, just not convertible to milliseconds without "anchoring" it first. E.g. 2012-10-08 + 5m1w5d3h10m15s makes perfect sense. Or 5m3w + 10m - 1m3w = 15m2w. – johv Oct 08 '12 at 19:42
  • Yes. My conclusion is that the problem wasn't sufficiently thought about by OP. This isn't really a question that could be answered as it is. – Denys Séguret Oct 08 '12 at 19:45
2

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.

Community
  • 1
  • 1
johv
  • 4,424
  • 3
  • 26
  • 42
1

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."

Mr_Spock
  • 3,815
  • 6
  • 25
  • 33
0

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...

Community
  • 1
  • 1
Austin Mueller
  • 169
  • 1
  • 10
0

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

Community
  • 1
  • 1
user1500049
  • 993
  • 7
  • 15
0

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.

dan
  • 13,132
  • 3
  • 38
  • 49