0

I need to replace SimpleDataFormat with Java 8 DateTimeFormatter. Below is the code with SimpleDateFormat.

DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(source);

Now I need to change it to DateTimeFormatter. I tried as below

LocalDateTime ld = LocalDateTime.now();
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Date startdate = dtf.parse(dtf);

Now this is generating exception.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
user1111880
  • 443
  • 5
  • 8
  • 26

4 Answers4

4
   DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
   LocalDate localDate = LocalDate.parse("2017-02-11", dtf);
   System.out.println(localDate.toString());

if you want Date object from LocalDate,the following works

Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());

As @JonSkeet advised, If you're using Java 8 you should probably avoid java.util.Date altogether

Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47
3

If looking for equivalent of your sdf in DateTimeFormatter try DateTimeFormatter.ISO_LOCAL_DATE
Explore the DateTimeFormatter class for more formats.

 LocalDateTime time = LocalDateTime.now();
    time.format(DateTimeFormatter.ISO_LOCAL_DATE);

Use LocalDate instead of LocalDateTime if intrested in Date only.

Sam Tatty
  • 91
  • 8
2

tl;dr

java.util.Date.from(

LocalDate.parse( "2017-01-23" )
         .atStartOfDay( ZoneId.of( "America/Montreal" ) )
         .toInstant()

)

No need of formatting pattern

No formatting pattern needed. Your input string happens to be in standard ISO 8601 format. These standard formats are used by default in the java.time classes for parsing and generating strings.

LocalDate

Use LocalDate for a date-only value, without time-of-day and without time zone.

LocalDate ld = LocalDate.parse( "2017-01-23" );

ZonedDateTime

If you want a date-time, let java.time determine the first moment of the day. Do not assume that first moment is 00:00:00.

Determining first moment of the day requires a time zone. The date varies around the globe by zone.

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ld.atStartOfDay( z );

If you want to perceive that moment through the lens of UTC, extract an Instant object.

Instant instant = zdt.toInstant();

The Instant is equivalent to the old legacy class java.util.Date. Both represent a moment on the timeline in UTC. The modern class has a finer resolution, nanoseconds rather than milliseconds.

Avoid java.util.Date

As others mentioned, you should stick with the modern java.time classes. But if you must, you can convert. Look to new methods added to the old classes.

java.util.Date d = java.util.Date.from( instant ) ; 
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

One way of doing it would be -

    LocalDateTime ld = LocalDateTime.now();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String date = ld.format(dtf);
Shubham Chaurasia
  • 2,472
  • 2
  • 15
  • 22