开发者

Java Equivalent to .NET's DateTime.Parse?

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 ve开发者_如何转开发ry 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"


See parse method in DateFormat Class. Here is a sample

DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
Date date = df.parse("2001-01-01");


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.


Use DateFormat:

DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜