aspose file tools*
The moose likes Features new in Java 8 and the fly likes Java 8 New Features - creating the current date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 8
Bookmark "Java 8 New Features - creating the current date" Watch "Java 8 New Features - creating the current date" New topic
Author

Java 8 New Features - creating the current date

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30957
    
158

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()?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Richard Reese
author
Ranch Hand

Joined: Jul 13, 2011
Posts: 84
    
    5

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.

Richard Reese
Java 8 New Features: A Practical Heads-Up Guide
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30957
    
158

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

Joined: Feb 27, 2002
Posts: 8919

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


Groovy
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10266
    
168

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.

[My Blog] [JavaRanch Journal]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39878
    
  28
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

Joined: May 26, 2003
Posts: 30957
    
158

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

Joined: May 03, 2008
Posts: 4658
    
    5

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

... but has added functionality to allow the use of LocalDate.


luck, db
There are no new questions, but there may be new answers.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30957
    
158

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

Joined: Oct 13, 2005
Posts: 39878
    
  28
Nice to see you again, Darryl
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

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

Joined: Oct 13, 2005
Posts: 39878
    
  28
I haven't looked at that method, but I think I might have thrown an NPE, too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java 8 New Features - creating the current date