• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to differentiate between the date formats "mm/dd/yyyy" OR "dd/mm/yyyy"  RSS feed

 
Pravesh Verma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

Is there is a way where I can code to differentiate between the date formats "mm/dd/yyyy" OR "dd/mm/yyyy". The major problem lies in identifying the date format for dates like "07/07/2014" or "12/12/2014".

Kindly suggest and I would be grateful in case someone can post the code snippet.

PS: I have tried all combinations of the code to figure out the date formats, however the problem is only with the case where month <=12. Need solution only for this.

Thanks and Regards
Pravesh
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

What do you mean by "figure out the date format", exactly? If you get "7/12/2014" you obviously can't know if whatever person or process created it meant July 12 or December 7th. If you get multiple dates, however, chances are that some of them will have a number of 13 or larger in them, which would make it clear (as it stands to reason that all dates would have the format).

What is the source of these dates?
 
Pravesh Verma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

First of thanks for the quick response on this thread!!

The source of these dates are stored value in the DB. However while saving the dates the format of the date is not saved. Now the problem is while retrieving these dates we have to show the data on the screen while the logged in LOCALE language, where we are unable to identify the previous correct date format.

Thanks and Regards
Pravesh
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So do you have reason to believe that not all dates are in the same format? If not, then looking at them all should in all likelihood make it clear which format it is.
 
Pravesh Verma
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf,

No we are not sure that all dates are saved in same format. Then how we should proceed further?

Regards
Pravesh
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pravesh Verma wrote:
No we are not sure that all dates are saved in same format. Then how we should proceed further?


I would look at the other fields to see if there is anything else that can be used. For example, perhaps the country code can give you a hint, as certain countries prefer one format over the other. Or another example, is there a code for the person who enter the data? If so, perhaps you can check other entries for the same data entry person (one with a number greater than 12).... But if there isn't... Sorry, sometimes, stuff can't be done. The computer can do a lot of great things, but it can't perform magic.

Henry
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pravesh Verma wrote:Hi Ulf,

No we are not sure that all dates are saved in same format. Then how we should proceed further?

Regards
Pravesh


Are you saying the dates are stored in a text column like VARCHAR instead of DATE types? Generally the database should store the dates in a DATE related type column. That way you can then retrieve it as Date type in Java and format it according to who wants to see it. If the dates are stored in text columns like VARCHAR then you have a problem, especially if the writes can happen from multiple points which could be using different formats.
 
Craig Worsell
Greenhorn
Posts: 14
Java MySQL Database Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with E Armitage, surely your db will know the date and usually in a yyyy-MM-dd format that you can change to a java date and format how you want to. But if your dates are stored in the db differently then firstly why? Secondly can you get it changed...?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pravesh Verma wrote:No we are not sure that all dates are saved in same format. Then how we should proceed further?

I think E Armitage has basically said it, but I will add this:

IF your "dates" are, in fact, stored as strings, then you need to inform your management immediately and try to stop any further propagation of this until you get it fixed; because it is WRONG, and it's a problem that's only likely to get worse.
A further possibility is to find the culprits responsible for it and string them up by their private parts; but don't embark on any "revenge missions" until you've solved the problem (division of responsibility).

Dates are NOT Strings, or vice-versa, and if you have no idea where that date came from, then there really is no way of converting a string back to a date.

Personally, I blame the Americans, who are the only civilised country on the planet to use that ridiculous mm/dd/yy[yy] format. I know, because I lived and programmed in Canada (who use dd/mm/yy, like everybody else) for twenty years, and 80% of their foreign trade is with the US.

In "objective" terms, you could create "Trusted" and "Untrusted" date classes in your Java programs, and treat them accordingly; but exactly what that entails I have no idea.

However, all the above is the nightmare scenario. If your dates are in fact stored as SQL DATEs, you have no problem: simply import them using the standard methods and you will have a java.sql.Date, and THAT is convertible to any format you like.

Winston
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: . . . don't embark on any "revenge missions" until you've solved the problem (division of responsibility). . . .
Surely you mean, “let a little anticipation heighten the suffering”? Mwaahaahaahaahaahaahaahaahaa
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!