• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

getDateInstance(?)

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to create a variable of type date from user input. I want it to be only a date, not time and other things in the variable, so I thought that parsing the input with
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
would accomplish that (according to the documentation, SHORT would be only a date dd-mm-yyyy). However, when I output the variable, I get the day, time and timezone, which I am not at all intrested in.
Code that parses and saves variable
Date Pn = df.parse(in.readLine());
How should I initialize the DateFormat?
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If someone enters 12-12-2003 12:30pm, what does your program output as the date that was entered?
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope... I get this output:
Thu May 26 00:00:00 CET 0018
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try:
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, thank you. I still get day of week, time and timezone though... How do I get rid of them?
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Create another SimpleDateFormat instance with the format of how you want the output to look, and then call the ".format(...)" method and output the results.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to keep in mind that you are creating a Date object. The SimpleDateFormat used to load the data into the Date object has nothing to do with how the output of the Date object will look. As suggested, you can use the same or a different SimpleDateFormat object to format the Date for output.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much, I didn't think of that the date could add it's own information
reply
    Bookmark Topic Watch Topic
  • New Topic