Solved

How to dynamically convert all date formats into number format (05/15/2010)?

Posted on 2010-08-22
8
314 Views
Last Modified: 2013-11-23
Hi,

I would like to have Java code that uses regex  to convert the following date formats into number format like (05/15/2010) on the fly. Is it possible?

Thanks

Original date format:
1. May 20, 2010 => 05/20/2010;
2. September 18, 2009 => 09/18/2009
3. Sept 12, 2005 => 09/12/2005
4. 03-28-2002 => 03/28/2002
5. 4-4-2002 => 04/04/2002
6. 12 August 2008 => 08/12/2008
0
Comment
Question by:ddzz2010
8 Comments
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 83 total points
ID: 33498417
You may use SimpleDateFormat:

http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

SimpleDateFormat sdf1 = new SimpleDateFormat("<input date format>");
SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd/yyyy");

try {
Date d = sdf1.parse("<your string>");
String ostr = sdf2.format(d);
} catch (Exception e) {
}
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 33498570
String strDate = null;
  try {
   DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
   strDate = df.format(sqlDate);
  } catch (Exception e) {
   _log.error("Exception caught in conversion : " + e.getMessage());
  }
  return strDate;
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33498733
SimpleDateFormat is the way to go, but you should be knowing the format in advance!
0
 

Author Comment

by:ddzz2010
ID: 33503883
ksivananth, can you please be more clear?

I will crawl the date types from a number of websites and therefore I wouldn't know what date format in advance. does your comment mean i can't use the SimpleDateFormat?

thanks.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Assisted Solution

by:eicheled
eicheled earned 83 total points
ID: 33504383
Any conversion requires that you need to know what you are converting FROM as well as TO.

In your case, you may need to use some logic to determine what kind of original date format you are dealing with so you can convert it properly. If you know what potential input formats you are going to encounter, you can write a comparison (regex would work) to determine the original format and then use SimpleDateFormat to do the conversions.

0
 

Author Comment

by:ddzz2010
ID: 33506756
Could anyone provide the regex for the following date formats?

May 20, 2010
September 18, 2009
Sept 12, 2005
03-28-2002
4-4-2002
12 August 2008
0
 

Accepted Solution

by:
spavithracse earned 84 total points
ID: 33509889
refer the following code and also the regex for date format in pearl is
sub isvaliddate {
  my $input = shift;
  if ($input =~ m!^((?:19|20)\d\d)[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$!) {
    # At this point, $1 holds the year, $2 the month and $3 the day of the date entered
    if ($3 == 31 and ($2 == 4 or $2 == 6 or $2 == 9 or $2 == 11)) {
      return 0; # 31st of a month with 30 days
    } elsif ($3 >= 30 and $2 == 2) {
      return 0; # February 30th or 31st
    } elsif ($2 == 2 and $3 == 29 and not ($1 % 4 == 0 and ($1 % 100 != 0 or $1 % 400 == 0))) {
      return 0; # February 29th outside a leap year
    } else {
      return 1; # Valid date
    }
  } else {
    return 0; # Not a date
  }
}
#
import java.util.Date;
#
import java.text.ParseException;
#
import java.text.SimpleDateFormat;
#
 
#
public class ConvertDateFormats {
#
 
#
public static void main(String[] args) {
#
 
#
//string containing date in one format
#
String strDate = "12/12/07";
#
 
#
try
#
{
#
//create SimpleDateFormat object with source string date format
#
SimpleDateFormat sdfSource = new SimpleDateFormat("dd/MM/yy");
#
 
#
//parse the string into Date object
#
Date date = sdfSource.parse(strDate);
#
 
#
//create SimpleDateFormat object with desired date format
#
SimpleDateFormat sdfDestination = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss");
#
 
#
//parse the date into another format
#
strDate = sdfDestination.format(date);
#
 
#
System.out.println("Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss");
#
System.out.println("Converted date is : " + strDate);
#
 
#
}
#
catch(ParseException pe)
#
{
#
System.out.println("Parse Exception : " + pe);
#
}
#
}
#
}
#
 
#
/*
#
Typical output would be
#
Date is converted from dd/MM/yy format to MM-dd-yyyy hh:mm:ss
#
Converted date is : 12-12-2007 12:00:00
#
*/

Open in new window

0
 

Author Closing Comment

by:ddzz2010
ID: 33538936
Thanks all.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
topping2 challenge 13 80
what is a "java.lang.System Property"   ? 20 56
more than one jdk and one jre 1 30
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now