• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java 8 New Features - creating the current date

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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()?
 
Richard Reese
author
Ranch Hand
Posts: 84
5
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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!
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any automated way of replacing old date with new date api in the code ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49828
69
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34863
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49828
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see you again, Darryl
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 49828
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked at that method, but I think I might have thrown an NPE, too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic