PHP's "strtotime" vs JAVA's ... nothing

PHP's "strtotime" function is one of the coolest core functions of the language.  I never really noticed how great it was until today.

At my company, we import date formats from customers and need to store them in MySQL format. ('Y-m-d h:m:s').  In PHP, you can pass strtotime just about anything and -- with the help of the date function -- convert that wiley timestamp to what you want.

Consider the following time formats:


  • 2004-02-12T15:19:21+00:00 (ISO 8601)

  • Thu, 21 Dec 2000 16:01:07 +0200 (RFC 2822)

  • Monday, January 1st

  • tomorrow

  • -1 week 2 days 4 hours 2 seconds


Let's convert these to MySQL timestamps with PHP:
echo date('Y-m-d h:m:s', strtotime('2004-02-12T15:19:21+00:00'));
echo date('Y-m-d h:m:s', strtotime('Thu, 21 Dec 2000 16:01:07 +0200'));
echo date('Y-m-d h:m:s', strtotime('Monday, January 1st'));
echo date('Y-m-d h:m:s', strtotime('tomorrow'));
echo date('Y-m-d h:m:s', strtotime('-1 week 2 days 4 hours 2 seconds'));

This automagically outputs:
2004-02-12 07:02:21
2000-12-21 06:12:07
2009-01-01 12:01:00
2009-02-12 12:02:00
2009-02-06 09:02:41

I remember the first time I can across strtotime and thought "wow - it will take in just about any format."  Clearly, there are some limits (I can't enter "orange" and get a date back) -- but needless to say, the function is simple and powerful.

Back to today at work.

A developer (we've recently moved to being an all-Java shop) tells me he has to write a custom date parser for ingesting and converting partner's date formats.  And every time we see a new date format, he has to add to his parser (then we have to build it, QA it, release it).

Poppy cock, I say!  Isn't there something in Java like strtotime?  Turns out?  No.

I did my googling, nothing.  Then I turned to Twitter.  Watch as we go from "But you *can* do that in Java!" to "oh, well, you'd have to be expecting a certain date format" quicker than you can say "ISO 8601."


  • Java Friend #1: "you can do that simply: String time = "12:31:24"; DateFormat sdf = new SimpleDateFormat("hh:mm:ss"); String result = sdf.parse(time).toString();"

  • Java Friend #2: "Won't SimpleDateFormat.parse do what you need?"

  • Me, to both: "Thanks for the suggestions. All the sample code I see is massive compared to the one-liner "strtotime." Sigh. Ah well.""

  • Java Friend #1 replies : "a one liner, but not as short as strtotime: System.out.println(new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z").parse(t));""

  • I direct message #1: "Will SimpleDateFormat accept multiple input date formats: ISO 8601, RFC 2822, MySQL Timestamp/Datestamp, etc.?"

  • His reply:  "you need to know the date format coming into SimpleDateTime. What format is it?"

  • Me: "There's the rub. Could be ISO 8601, RFC 2822, MySQL Timestamp/Datestamp, etc. Was hoping to not have to write for each possible..."

  • Him: "you'll need to do something about the type of date strings you operating on to make an error free conversion"


<slaps forehead>

And there's the problem -- I don't want to have to build in a switch for every known date format.  If partner XYZ wants to enter a non-standard format ("-1 week 2 days 4 hours 2 seconds"?), we shouldn't need to refactor the code to parse this.

Now.  A disclaimer.  I am not a JAVA programmer.  I consider myself an intermediate PHP programmer.  Programming is not my job nor is it my dream to become a full-time developer.

However, I've done my google...and I've asked 4 different JAVA experts who all agree it can't be done as cleanly as strototime.

That being said?  Booooo to you Java.  Boooooo.

Loading mentions Retweet
Filed under  //  date   dateformat   java   music   php   ramblings   simpledateformat   strtotime   tech  
Comments (0)
Posted 1 year ago