-4

Is there any function or library which gets a date in milliseconds, given a String?

This question shows how to convert a formatted String to a Date object, but is there any way to do this with an unformatted String?

Community
  • 1
  • 1
Antarix
  • 665
  • 1
  • 10
  • 29
  • 2
    The short answer would be, no. The long answer would to look at something like [`DateUtils#parseDate`](http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/time/DateUtils.html#parseDate%28java.lang.String,%20java.lang.String...%29) from [Apache Commons Lang API](http://commons.apache.org/proper/commons-lang/) which allows you to pass a variable number of formats – MadProgrammer Jun 21 '13 at 05:45
  • This question does not make any sense. What amount of milliseconds would you like to get for "foo" and "foobar"; 10 & 200, or 42 and 24? – oligofren Jun 21 '13 at 05:52

2 Answers2

2

Basically, the task is impossible. Here's an example:

01/04/2012

In the US, that means January 4th 2012. In Australia, that mean 1st April 2012.

Without knowing where you are and what date formats conventionally mean, it is impossible to accurately map an arbitrary date-like string to a date time value that matches what the user actually meant.

And even if you do know about the relevant local conventions, users have a remarkable propensity to be oblivious to ambiguity. Dealing with that may require deep domain knowledge (or mind reading skills!) to disambiguate the possible meanings.


When you think about it, this is why modern user interfaces typically use a date-picker widget of some kind when the user needs to enter a date / time

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

first convert the string to Date. From there you can get time in milis using Date.getTime() method

stinepike
  • 54,068
  • 14
  • 92
  • 112