SimpleDateFormat

E

Eric

I am having a heck of a time trying to parse 24 hour time format from String
to a java.util.date

I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")

but it throws a Parse Exception error.

Can anyone give me some pointers or put me in the right direction?

Thanks in advance for any help!!


-Eric
 
M

Michael Borgwardt

Eric said:
I am having a heck of a time trying to parse 24 hour time format from String
to a java.util.date

I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")

but it throws a Parse Exception error.

Can anyone give me some pointers or put me in the right direction?

Define your own SimpleDataFormat using a pattern that desribes the
format you actually mean. The static method gives you "the default
formatting style for the default locale", which in your case probably
expects something like "08:00am".
 
T

Todd Shillam

Eric,

Here's a few snippets of code to that may shed some light on how to use
Java's SimpleDateFormat.


//CREATING SIMPLE_DATE_FORMAT TO DISPLAY MONTH/DAY/YEAR
SimpleDateFormat formatter;
formatter = new SimpleDateFormat("MM/dd/yyyy", Locale.US);

//NEW DATE OBJECT--INITIALZIED WITH CURRENT SYSTEM DATE
today = new Date();

//CONVERTS DATE (TODAY) INTO STRING FORMAT
output = formatter.format(today);

//PRINTS STRING VARIABLE
System.out.println("Current Date (MM/DD/YYYY): " + output);
System.out.println();

//COLLECTING USER INPUT FOR DATE (MM/DD/YYYY)
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println();
System.out.print("Please enter new date (MM/DD/YYYY): ");
String userInput = br.readLine();

//INPUT VARIABLE PARSED INTO A DATE--INITIALIZES
today = formatter.parse(userInput);

//DATES CONVERTED BACK INTO A STRING
output = formatter.format(today);

//STRING DISPLAYED
System.out.println(output);

Good luck,
--
Todd


I am having a heck of a time trying to parse 24 hour time format from String
to a java.util.date

I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")

but it throws a Parse Exception error.

Can anyone give me some pointers or put me in the right direction?

Thanks in advance for any help!!


-Eric
 
C

Chris Smith

Eric said:
I am having a heck of a time trying to parse 24 hour time format from String
to a java.util.date

I am trying to use SimpleDateFormat.getTimeInstance().parse("0800")

The code above is actually identical to DateFormat.getTimeInstance(),
and has nothing to do with SimpleDateFormat. It's unfortunate that the
Java language allows this kind of error. In any case, you use
SimpleDateFormat by creating an instance, not calling static methods.
The format mask is specified in the API docs, but in your case it would
be:

new SimpleDateFormat("kkmm").parse("0800");

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top