Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2009, 11:34 PM   PM User | #1
omega_lonestar
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
omega_lonestar is an unknown quantity at this point
Unhappy Quick question on converting 24 hour time to 12 hour time

I'm really having a hard time converting a 24 hour time to 12 hour time based on user input. Bascially, I want to have my program ask the following:

"Please enter your time in 24 hour input:"

and the user types the following: 13 30

Then, I would like to have the output converting the time by saying:

"The time is 1:30 PM"

Any thoughts? Thanks.

omega_lonestar is offline   Reply With Quote
Old 01-21-2009, 11:39 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
If you're first token is > 12 and <= 23, subtract 12. Simple as that. The second token won't need changing but that assumes you're ensuring it to be between 0 and 59 inclusive (and hour has been validated as 00-23)
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Old 01-22-2009, 12:14 AM   PM User | #3
omega_lonestar
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
omega_lonestar is an unknown quantity at this point
Thank you for the info. Just one question though...

Say if the person puts the correct hour and yet the minutes are beyond the 0-59 range, how can I make sure that it does not print out "The time is 1:73 PM" (as an example)?
omega_lonestar is offline   Reply With Quote
Old 01-22-2009, 03:46 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Code:
if (token < 0 || token > 59)
{
    throw new IllegalArgumentException("Minutes must be between 0 and 59!");
}
Thats when token is of type int. All you do is try/catch it and reloop. You can force it to be an int without casting by using scanner and asking for the next int. If its not an int, it will toss an exception.
If you want it as a string (00 - 59), you'd need to check the values and compare them between each character. Cast to an int (with a try), and use appropriate error checking for 0-5 and 0-9.
__________________
PHP Code:
header('HTTP/1.1 420 Enhance Your Calm'); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
omega_lonestar (01-23-2009)
Old 01-23-2009, 04:06 PM   PM User | #5
omega_lonestar
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
omega_lonestar is an unknown quantity at this point
Thank you for your help This is what I have so far...I would like to have the input separated into two integers (one for hour and one for minutes) but I have no clue what to do here...


This is the main part
Code:
import java.util.*;

public class Main{
  public static void main(String[] args){
	//initialize variables
    int time = 0;
    int hour = 0;
    int minute = 0;
    //initialize the scanner
    Scanner key = new Scanner(System.in);
    //ask the person to enter the data
    System.out.print("Enter time in 24-hour format: ");
    //enter the data
    try{
      time = key.nextInt();
      
      //check to see if time is out of range, throw exception
      if(hour<0 || hour>23 && minute<0 || minute>59)
        throw new WrongTimeException();
      
      //check to see if hour is less than 12
      if (hour<12)
      {
    	  System.out.println("That is the same as: " + hour + ":" + minute + "A.M.");
      }
      //check to see if hour is equal to 12
      if (hour==12)
      {
    	  System.out.println("That is the same as: " + hour + ":" + minute + "P.M.");
      }
      
      //check to see if it is more than 12
      //subtract the number from 12 in this case
      if (hour>12)
      {
    	  hour=hour-12;
    	  System.out.println("That is the same as: " + hour + ":" + minute + "P.M.");
      }
      
     }catch(WrongTimeException e){
      System.out.println(e.getMessage());
    }
     
  }
}

And this is for the exception class

Code:
public class WrongTimeException extends Exception{
  public WrongTimeException(){
	  super("Wrong Time" + hour + ":" + minute);
  }
}
omega_lonestar is offline   Reply With Quote
Old 01-24-2009, 01:43 AM   PM User | #6
omega_lonestar
New Coder

 
Join Date: Oct 2008
Posts: 10
Thanks: 2
Thanked 0 Times in 0 Posts
omega_lonestar is an unknown quantity at this point
Whoops never mind! I was able to get it to work yay! Thanks for the help!!
omega_lonestar is offline   Reply With Quote
Old 02-01-2010, 08:58 PM   PM User | #7
nimbular
New to the CF scene

 
Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nimbular is an unknown quantity at this point
what's up, Lonestar --
may i ask how you resolved this?
thanks.
nimbular is offline   Reply With Quote
Old 09-28-2010, 03:07 AM   PM User | #8
ZetaX9
New to the CF scene

 
Join Date: Sep 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
ZetaX9 is an unknown quantity at this point
Hey, what was wrong with it? I still don't get it.
ZetaX9 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:00 PM.


Advertisement
Log in to turn off these ads.