2

I need to get a Date instance from input file. I don't know the date format, but I want to get it from user profile settings.

Te following code does not working:

DateFormat form = DateFormat.getDa​teInstance(DateF​ormat.SHORT, Locale.getDefaul​t());

try { 
    Date t = form.parse("6/6/2015");
}

unparseable date error

I want to know if there is any way to get date from string without knowing the date string pattern.

I need this date to create MySQL query. Maybe there is another way to build this query without parsing date? I am using Entity Beans.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marianx
  • 31
  • 5
  • possible duplicate of [Parsing a Date given in unknown format to standard format in java?](http://stackoverflow.com/questions/11220618/parsing-a-date-given-in-unknown-format-to-standard-format-in-java) – Basil Bourque Jun 07 '15 at 06:36

2 Answers2

4

No. Consider the date "1/2/2015": is that February 1st or January 2nd. Depends on your locale.

Instead, you should be more specific: rather than getting a date formatter for your locale, use SimpleDateFormat with an explicit pattern.

anonymouse
  • 51
  • 1
  • But if i don't know pattern? I want to deal with many possibla date formats. – Marianx Jun 06 '15 at 13:00
  • 1
    @Marianx Sometimes, we can't get what we want. Without locale information or restriction it is not possible to reliably parse a numeric date. That is not a pleasant fact. I've wished it were not the case ever since, as a new young programmer in 1970, I learned about the atrociously illogical MM/DD/YYYY format. It is still a fact we have to live with. I suggest going back to your user profile information and seeing if you have, or can deduce, a locale. – Patricia Shanahan Jun 06 '15 at 15:08
2

I want to know if there is any way to get data from string without knowing the data string pattern.

Without any more information, this is very error prone. For example, consider "7/6/2015" - does that mean June 7th, or July 6th?

If you know the user's locale, you can do a lot better - for example, you could obtain DateFormat instances for long, medium, short and full date patterns for that locale, and try them one at a time. Bear in mind, however, that depending on where this code is executing, the default locale (as you're using at the moment) may not be the user's locale. You mention the user profile settings - hopefully that already contains a locale.

One alternative is to ask the user to tell you what the format is - maybe provide lots of different examples, and let them pick the one that matches.

Finally, if the file has lots of dates in and you're confident they'll all be in the same format, you could try to parse all of them in each of several different formats - that's likely to reduce the chances of error, as "7/6/2015" becomes unambigious if you've also seen "13/1/2015" for example.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • "For example, consider "7/6/2015" - does that mean June 7th, or July 6th?" I want to get format from user profile settings. I don't know whitch date is correct. – Marianx Jun 06 '15 at 12:59
  • @Marianx: Right, so you should use the user's locale, which I'm *hoping* is in the user profile settings. You haven't told us anything about what *is* in the user profile settings, or whether this is running on the user's machine, or a web site, or anything like that. Please read the *whole* answer. – Jon Skeet Jun 06 '15 at 13:01
  • The problem is that i can't ask user. In my case DateF​ormat.SHORT returns value of 3, and ther is an error. – Marianx Jun 06 '15 at 13:09
  • `DateFormat.SHORT` is a constant (you're meant to pass it into `DateFormat.getDateInstance`), and "there is an error" doesn't tell us anything about what you're trying when you receive the error, or what that error is. *Why* can't you ask the user? Hint: if you tell us more about the context, we're more likely to be able to help you. No-one enjoys playing guessing games when trying to help someone. – Jon Skeet Jun 06 '15 at 13:11
  • I Have some info in my input file, for example: name, surname, date, age, etc. I want to convert this date (string) to Date instance. After that i need to build mySQL query with this date to get some information from database. The error appears when i want to parse string to date: Date t = form.parse("6/6/2015"); I can't ask user because another script runs it. – Marianx Jun 06 '15 at 13:25
  • @Marianx: That hasn't told us *any* of the information I asked about. – Jon Skeet Jun 06 '15 at 14:08