• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Input, Problems getting date format on output  RSS feed

 
Frank Banfield
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

I'm sure that someone has run into this before and hopefully there is a simple resolution to what I'm encountering. I'm trying to develop a very simple Java program that allows the user to input an integer, and using the dformat.format class, I'd like to convert the integer into a correctly formated date. For example, my program will display the current date, then prompt the user to input an integer. Using a very simple equation (below)

input=input-currentDate
System.out.println("The date entered is: " +input)

The program is actually performing as I'd like it to, just the date format isn't correct. For example, when the user inputs say the number 7, and today's date is June 26, 2011, the ouput is -19. This is accurate, only I would like the date output to format as June 19, 2011. So when I attempt to format the date using import java.util.Date and plug in dFormat.format, the output is December 31, 1969? The format is correct, but the output date has changed to this date? I've included my program code below. Anyone that can provide some direction is greatly appreciated!

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! Could you please UseCodeTags next time? That makes your code look better.

Frank Banfield wrote:For example, when the user inputs say the number 7, and today's date is June 26, 2011, the ouput is -19. This is accurate, only I would like the date output to format as June 19, 2011.

And what would you do if today's date is July 4th and the user inputs 12? The result (12 - 4) would be 8. How do you transform that back into June 22?

If you need to perform date arithmetic you should use Calendar, especially its add method. This can take negative numbers to subtract instead of add. The getDate and setDate methods can be used to retrieve / set the date.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!