• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java 8 New Features - creating the current date

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On page 105, the book contains the statement:



1) This doesn't work. [It should get getYear() and not getDayOfYear().]
2) What's the point of all of this code. Why not just write LocalDate.now()?
 
author
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,

You are correct. It should be getYear() and not getDayOfYear(). This will be added to the book’s errata.

For better or worse I used a more verbose approach to calculate the current date to illustrate the methods available. LocalDate.now() is shorter and can be used. Knowing about these methods allows you to easily calculate a future date where one of the “of” methods parameter is modified. Similar results can be obtained using a “plus” method.

The new data and time API provides a lot of flexibility in how temporal values can be created. At the same time, this flexibility can be confusing as to which method is best.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Reese wrote:For better or worse I used a more verbose approach to calculate the current date to illustrate the methods available.


Ah. It's still confusing, but the reason makes sense. I was puzzling if I was missing something!

And cool that you copy/pasted this text to the errata page!
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any automated way of replacing old date with new date api in the code ?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep bhatt wrote:Is there any automated way of replacing old date with new date api in the code ?


I don't think there is and I don't see a reason why one would automate it.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep bhatt wrote:Is there any automated way of replacing old date with new date api in the code ?

There is a good reason for not doing it:

if it ain't broke, don't fix it!

 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An automated conversion would break APIs. The JDBC API still uses old dates. As does plenty of other code including code your company has probably written.

I agree with the above - don't do it. Just use the new API for new code. Converting back to the old API as needed.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:The JDBC API still uses old dates.


... but has added functionality to allow the use of LocalDate.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
THat's cool. You can go write from the new Date to java.sql.Date without having to go through java.util.Date first.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice to see you again, Darryl
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Nice to see you again, Darryl



Thank you, Campbell. I've been doing some stuff at home with JDBC, Swing and Java 8 features and coded convoluted parse/format conversions between SQL Date and LocalDate before I discovered the new toLocalDate() and valueOf(LocalDate) methods. I do wish the latter had been designed to return null for a null argument instead of throwing a NPE though.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't looked at that method, but I think I might have thrown an NPE, too.
 
reply
    Bookmark Topic Watch Topic
  • New Topic