File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getDateInstance(?)

 
Carl Pettersson
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope... I get this output:
Thu May 26 00:00:00 CET 0018
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");
 
Carl Pettersson
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • 0
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much, I didn't think of that the date could add it's own information
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic