• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String Input, Problems getting date format on output

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic