Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-08-22
8
Medium Priority
?
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 332 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

721 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