Solved

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

Posted on 2010-08-22
8
313 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
Comment Utility
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
Comment Utility
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
Comment Utility
SimpleDateFormat is the way to go, but you should be knowing the format in advance!
0
 

Author Comment

by:ddzz2010
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Assisted Solution

by:eicheled
eicheled earned 83 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks all.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array11 challenge 16 50
word0 challenge 3 56
unix in java example 9 37
regex code to filter this thip of combo email 42 42
by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

12 Experts available now in Live!

Get 1:1 Help Now