Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-22
8
Medium Priority
?
320 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 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
Independent Software Vendors: 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

782 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