Forums Register Login

Date input formatting issue

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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:
+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
Thank you, I forgot to add the "format" in the command.  I was trying instead of .

Thank you for the help!
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
Thank you for the suggestion, changed code from Date to LocalDate.  Yes, it is MUCH nicer to work with!
+Pie Number of slices to send: Send
 

Paul Peterson wrote:Thank you for the suggestion . . .

That's a pleasure
Quick! Before anybody notices! Cover it up with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 341 times.
Similar Threads
Handling a strange date format
String conversion
Problems in converting to a Date Object.
Invalid Date
Conversion of string to date
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 11:41:04.