Solved

change military time to Standard time

Posted on 2004-04-24
7
4,294 Views
Last Modified: 2008-02-01
I have a database returning a time to me, but it's in military time.   Like this:
15:00:00
15:15:00
15:30:00
15:45:00
16:00:00
16:15:00

What can I call that will format this time to the regular standard time???  Like this:
3:00 PM
3:15 PM
3:30 PM
4:00 PM
4:15 PM

I was trying to use this:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm");
sdf.format(time);

but it only excepts a Date and no time.    Can anyone help me?   Thanks
0
Comment
Question by:calvinklein1
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 10910039
SimpleDateFormat standard= new SimpleDateFormat("hh:mm");
SimpleDateFormat military = new SimpleDateFormat("HH:mm");
String regular = standard.format(military.parse(fromdb));

0
 
LVL 7

Expert Comment

by:maheshexp
ID: 10910057
0
 

Author Comment

by:calvinklein1
ID: 10910109
Thank you object.   but i'm still get an error when trying to parse the object from the database (or in the case the vector that was created from the database).  This is the error:

unhandled exception: java.text.ParseException
and it's for this line code from below:
regular = standard.format(military.parse(y.get(1).toString());

y.get(1)  is an object from the vector that's the way it was put in.   Do I need to cast it to a string or something to make this work???   Here's the method i'm using:

    public ArrayList getTimeSlotTime(){
        ArrayList timeSlotTime = new ArrayList(this.size);
        SimpleDateFormat standard= new SimpleDateFormat("hh:mm");
        SimpleDateFormat military = new SimpleDateFormat("HH:mm");
        String regular;
               
        Vector y;
        for(int i = 0;i < size;i++){
            y = (Vector)slotIdVector.elementAt(i);
            regular = standard.format(military.parse(y.get(1).toString());
            timeSlotTime.add(i,regular);
        }
        return timeSlotTime;
    }
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10910117
you just need to handle the exception:

        for(int i = 0;i < size;i++){
            y = (Vector)slotIdVector.elementAt(i);
            try {
               regular = standard.format(military.parse(y.get(1).toString());
            } catch (ParseException ex) {
               // parse error, handle as appropriate
               ex.printStackTrace();
               regular = "??:??";
            }
            timeSlotTime.add(i,regular);
        }
0
 

Author Comment

by:calvinklein1
ID: 10910146
Thank you!!!
0
 
LVL 92

Expert Comment

by:objects
ID: 10910164
0
 
LVL 1

Expert Comment

by:alkonaut
ID: 10948853
Actually, "the regular standard time" as you say is just a queer time format used in the US, while "military time" (24h) is the norm. (Much like the imperial system of measurement).

Come on, catch up =)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ejb stateless example 2 40
Is Applet the way to go for my drag and drop system? 8 33
wild fly 8 startup error 2 37
spring maven example issues 3 38
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question