Java Equivalent to .NET's DateTime.Parse?

2019-06-16 15:48发布

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"

3条回答
甜甜的少女心
2楼-- · 2019-06-16 15:57

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.

查看更多
何必那么认真
3楼-- · 2019-06-16 16:13

See parse method in DateFormat Class. Here is a sample

DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");
Date date = df.parse("2001-01-01");
查看更多
狗以群分
4楼-- · 2019-06-16 16:14

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.

查看更多
登录 后发表回答