views:

218

answers:

3

I'm working on a java class that I will use with Pervasive Data Profiler that needs to check if a Date String will work with .NET's DateTime.Parse.

Is there an equivalent class or 3rd party library that can give me this functionality that is very close to .NET's DateTime.Parse?

I would need it to be able to handle a broad range of date formats. ex. "12/20/2008", "1/1/08", "5/10/2009 12:46:00 AM", "5/10/2009 17:46:00"

+5  A: 

See parse method in DateFormat Class. Here is a sample

DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
Date date = df.parse("2001-01-01");
Asad Butt
+2  A: 

Check the DateFormat.parse(String) methods of the DateFormat class.

Also, the class Date has two deprecated methods that parse strings into Date objects, however the use of the Date class in general is not recommended. It has been replaced by the Calendar class.

pnt
+1  A: 

Use DateFormat:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse("2010-03-15");

SimpleDateFormat (is very handy. But... if you really want to use something more powerful -and consider it's really worth- you can use Joda Time. It's a very powerful, yet easy-to-use library. Indeed there's a proposal to make a new standard library very similar to it.

helios