Solved

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

Posted on 2010-08-22
8
315 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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
 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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