• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date input formatting issue  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a method that prompts the user for a date input and returns a date.  It works, mostly.  I have to press enter twice after the date and when it displays the date it includes the day and time which I do not need.  I'm most likely missing something minor and would like some direction.

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:I have a method that prompts the user for a date input and returns a date.

You got this right.
I have to press enter twice after the date and when it displays the date it includes the day and time which I do not need.

If by "it" you mean this method, that's not correct. There is nothing in this method that would display a successfully parsed date value assigned to the variable d. There is also nothing in this code that would cause you to have to hit Enter a second time after typing in a date.

Look for the source of those problems elsewhere, in the code that executes after this method is called.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Partial cranial flatulence.  I found the error that made me press enter a second time, but the date still displays like this
Sat Nov 26 00:00:00 EST 1966 


This is the code used to display the date:



And the prompt() method is as follows:
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming it's the d.getDate() term that's producing the actual date string value. Use another SimpleDateFormat object to format the d.getDate() value
 
Paul Peterson
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, I forgot to add the "format" in the command.  I was trying instead of .

Thank you for the help!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using Date rather than one of the newer (Java8) date and time classes? After years of complaints about the user interface of Date, Calendar, etc., and how much better Yoda Time is, Oracle finally created a date and time with a nice interface. If this is new code, please consider using Local‍Date instead.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the suggestion, changed code from Date to LocalDate.  Yes, it is MUCH nicer to work with!
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:Thank you for the suggestion . . .
That's a pleasure
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!