0

I'm using this code for converting String to java Date Object.Which maven dependency do I need to use for DateTimeFormatter and ZonedDateTime.

Date date = Date.from(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss‌​.SSSZ").parse("2015-‌​01-12T05:00:00.000+0‌​000", ZonedDateTime::from).toInstant())

Can anyone please help me out ...

dev777
  • 999
  • 6
  • 17
  • 34

4 Answers4

4

You must use Java8 to use those classes, they were added in this version. If you don't use Java8 yet, you can resort to JodaTime (http://www.joda.org/joda-time/) but the API is a bit different

Edit : as said by Basil Bourque, it is not advised to use JodaTime, but instead use the backport described in his post.

lepak
  • 369
  • 2
  • 9
  • No, the Joda-Time project team advises migration to java.time. Joda-Time is in maintenance mode now. See [my Answer](http://stackoverflow.com/a/39043704/642706) for back-ports to Java 6 & 7. – Basil Bourque Aug 19 '16 at 16:40
  • I didn't know that, so it wouldn't be advised to use an "old" version of JodaTime built with java7? – lepak Aug 22 '16 at 08:01
  • Joda-Time is still actively maintained. Be dure to update to its latest version of Joda-Time for bug fixes and updates to its ‘tz’ database of time zones (zone rules change frequently). Start learning java.time and start using it when convenient. You can use both Joda-Time and java.time in different parts of your project; just be careful with `import` statements as some classes share the same names. – Basil Bourque Aug 22 '16 at 15:35
2

Not Joda-Time

Some other Answers are incorrect in suggesting the Joda-Time library. This source code is clearly using the java.time classes built into Java 8 and later.

java.time

The java.time framework is the successor to Joda-Time, defined by JSR 310, and led by the same man as who created Joda-Time, Stephen Colbourne.

Back-ports

To answer the Question, if you are using Java 6 or 7, use the ThreeTen-Backport project. Much of the java.time functionality was back-ported there.

Currently the Maven dependency is:

<dependency>
    <groupId>org.threeten</groupId>
    <artifactId>threetenbp</artifactId>
    <version>1.3.2</version>
</dependency>

That back-port was further adapted for Android in the ThreeTenABP project. See How to use ThreeTenABP in an Android project

OffsetDateTime

That example code can be simplified. The input data in standard ISO 8601 format can be directly parsed by the OffsetDateTime class. So need to define a formatting pattern.

OffsetDateTime odt = OffsetDateTime.parse( "2015-‌​01-12T05:00:00.000+0‌​000" ) ;
java.util.Date utilDate = java.util.Date.from( odt.toInstant() ) ;

Or in one line if you insist.

java.util.Date utilDate = java.util.Date.from( OffsetDateTime.parse( "2015-‌​01-12T05:00:00.000+0‌​000" ).toInstant() );

Of course, catch DateTimeParseException for invalid inputs.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1
Edit:As @BasilBourque mentioned this new [Java8 Date api][1]. 
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
1

Classes with such names are part of JDK 8+ in java.time and java.time.format packages.