wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes getDateInstance(?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "getDateInstance(?)" Watch "getDateInstance(?)" New topic
Author

getDateInstance(?)

Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
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

Joined: Dec 04, 2001
Posts: 80
If someone enters 12-12-2003 12:30pm, what does your program output as the date that was entered?


Hope This Helps
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
Nope... I get this output:
Thu May 26 00:00:00 CET 0018
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Try:
DateFormat df = new SimpleDateFormat("MM-dd-yyyy");


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Carl Pettersson
Ranch Hand

Joined: Sep 09, 2003
Posts: 73
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

Joined: Sep 03, 2003
Posts: 399
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Sep 09, 2003
Posts: 73
Thank you very much, I didn't think of that the date could add it's own information
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getDateInstance(?)