0

I have huge set of data stored on server and it's different rows contain dates in as many as 20+ different formats. I want to convert them in to one common format. How can I achieve that? Do I need to write separate function for each format or is there some super function which can read date of any format and convert it to a specific format.

hari shankar
  • 27
  • 1
  • 7
  • 2
    You mention both Java and Perl - you might want to mention which of those (if either) you are *currently* using to read the data out of the database. Also, you might want to tell us how many records/rows "huge" is - I guarantee that *someone* thinks your table is tiny. – Bob Gilmore Sep 16 '13 at 04:21

3 Answers3

2

Generally it's not possible to parse a random date format automatically. For example "02/03/04" - without extra information, you can't tell whether this is 2 Mar 2004, 3 Feb 2004, 4 Mar 2002 or possibly something else.
But if you don't have this kind of problem, in java you can use one or more SimpleDateFormat objects, possibly with setLenient(true). From the javadoc: "With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format", but you should check whether that really helps.

1

In Perl, the Date::Parse module does a pretty spectacularly good job of extracting useable date information from arbitrary strings.

From the doco (http://metacpan.org/pod/Date::Parse) :

Below is a sample list of dates that are known to be parsable with Date::Parse

 1995:01:24T09:08:17.1823213           ISO-8601
 1995-01-24T09:08:17.1823213
 Wed, 16 Jun 94 07:29:35 CST           Comma and day name are optional 
 Thu, 13 Oct 94 10:13:13 -0700
 Wed, 9 Nov 1994 09:50:32 -0500 (EST)  Text in ()'s will be ignored.
 21 dec 17:05                          Will be parsed in the current time zone
 21-dec 17:05
 21/dec 17:05
 21/dec/93 17:05
 1999 10:02:18 "GMT"
 16 Nov 94 22:28:20 PST 
szabgab
  • 6,202
  • 11
  • 50
  • 64
bigiain
  • 809
  • 5
  • 8
  • 1
    It is generally preferred to use version agnostic urls such as [http://p3rl.org/Date::Parse](http://p3rl.org/Date::Parse), [https://metacpan.org/module/Date::Parse](https://metacpan.org/module/Date::Parse), and [http://search.cpan.org/perldoc?Date::Parse](http://search.cpan.org/perldoc?Date::Parse) – Brad Gilbert Sep 17 '13 at 20:00
0

Yes, In java the simple dateFormatter has hard-coded format needs to be defined and use. Now you have some option to accommodate all the format in one code.

  1. Create a configurable property/CSV file where each of the format could be saved like:
oldFormat1;newFormat1
oldFormat2;newFormat2
oldFormat3;newFormat3
oldFormat4;newFormat4
oldFormat5;newFormat5

(in your case, new Format will be same for all)

  1. Read the property file from java program and get these result into variable, say a Map<String><String> to hold the above pair as key-value.

  2. Use SimpleDateFormat#format to format the date

    DateFormat originalFormat = new SimpleDateFormat(old_format, Locale.ENGLISH);
    DateFormat targetFormat = new SimpleDateFormat(new_format);
    Date date = originalFormat.parse("August 21, 2012"); // parse date from database, where date format is MMMM dd, yyyy
    String formattedDate = targetFormat.format(date);  // 20120821
    

I hope these steps completes your requirement.

Gyanendra Dwivedi
  • 5,511
  • 2
  • 27
  • 53
  • I tried this code - try { DateFormat originalFormat = new SimpleDateFormat("YYYY-MM-dd"); DateFormat targetFormat = new SimpleDateFormat("dd-MM-YYYY"); Date date = originalFormat.parse("2015-05-28"); String formattedDate = targetFormat.format(date); System.out.println("Date :" + formattedDate); } catch (Exception e) { System.out.println(e.toString()); } and it's output is- Date :28-12-2015 Why is it converting month to december? – hari shankar Sep 23 '13 at 04:21
  • I guess year format should be 'yyyy' rather than 'YYYY'. Could you please check? – Gyanendra Dwivedi Sep 23 '13 at 04:55
  • It is disappointing that you have removed the accepted answer from the post. May I know what is the issue with the answer; so that I can improve it? – Gyanendra Dwivedi Sep 23 '13 at 04:59
  • Please let me know how can I use Map to parse different formats of Date. Right now I am trying something like this (as in question) - http://stackoverflow.com/questions/962953/determine-if-a-string-is-a-valid-date-before-parsing I am planning to write methods corresponding to each Date format and call these method if parsing fails in one type. – hari shankar Sep 23 '13 at 05:53
  • I am sorry that I removed the accepted answer status. The reason is I am new in Java. At first glance I felt that it would work but then I failed to implement it so I am not able to verify it by myself. – hari shankar Sep 23 '13 at 05:59