Converting 24 hour time notation

Convert 24 hour time notation to 12 hour

Page 1 of 1

4 Replies - 30989 Views - Last Post: 28 November 2012 - 06:24 PM Rate Topic: -----

#1 donnyj47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-January 06

Converting 24 hour time notation

Post icon  Posted 27 January 2006 - 02:33 AM

I am having extreme problems(i think logic) trying to convert 24 hour time notation to 12 hour time notation with AM and PM. I really don't have an idea of what the problem is. The user inputs a string time in 24 hour notation, and then the program is supposed to convert it, not using any java tools.
public class TimeConvert
 {
  
	//int value of hour
	private String hour;
	
	//int value of minuets
	private String minuets;
	
  public TimeConvert()
  {
 	 hour="00";
 	 minuets="00";
  }
  
  //static method to convert a 24 hour time to 12 hour time
  public String doConversion(String aTime) throws TimeFormatException
  {
 	 //formatted String
 	 String timeFormated	= String.format("That is the same as %s:%s",hour,minuets);
 	 
 	 //find the position of the colon
 	 int colon=aTime.indexOf(':');
 	 
 	 //if there is no colon
 	 if(colon!=2)	
 	 {
    TimeFormatException tfe= new TimeFormatException("You entered "+aTime+"\nFormat must be hh:mm\n");
    throw tfe;
 	 }
 	 
 	 else
 	 {
    //parse the numbers before the colon
    hour = aTime.substring(0,colon);
    int intHour=0;
    
    //parse the numbers after the colon
    minuets = aTime.substring(3);
    int intMinuets=0;
    
    try
    {
   	 intHour= Integer.parseInt(minuets);
   	 intMinuets= Integer.parseInt(hour);
   	 
   	 if(intHour <0 || intHour>23)
   	 {
      throw new TimeFormatException("\nYou entered "+hour+"\nHour must be less than 23\n");
   	 }
   	 else if(intMinuets<0 || intMinuets>59)
   	 {
      throw new TimeFormatException("\nYou entered "+minuets+"\nMinuets must be less than 59\n");
   	 }
   	 
   	 else
    {
   	 if(intHour==12)
   	 {
      minuets+="PM";
   	 }
   	 
   	 else if(intHour<12)
   	 {
      minuets+="AM";  
   	 }
   	 
   	 else if(intHour>12)
   	 {
      intHour=intHour-12;
      hour=Integer.toString(intHour);
      minuets+="PM";
   	 }
    }
    }
    
    catch(NumberFormatException nfe)
    {
   	 TimeFormatException tfe = new TimeFormatException("\nYou entered "+aTime+"\nFormat to hh:mm\n");
   	 throw tfe;
    }
    
 	 }
 	 return timeFormated; 	 
  }
 }



Here is my custom exception class
class TimeFormatException extends Exception
{
	//one String parameter constructor to pass to super class Exception
	TimeFormatException(String msg)
	{
  super(msg);
	}
	
	//concadinate 2 String messages and pass to super class Exception
	TimeFormatException(String msg,String msg1)
	{
  super(msg1+msg);
	}
}



And here is the test class to run the program.
import java.util.*;
public class TimeConvertTest
{
	public static void main(String args[])
	{
  //instanciate Scanner
  Scanner scan=new Scanner(System.in);
  
  //instanciate TimeConvert object
  TimeConvert convert = new TimeConvert();
  
  while(true)
  {
 	 System.out.print("\nEnter Time in 24-hour notation<hh:mm>: ");
 	 
 	 String converted="unknown";
 	 
 	 //try to convert the time
 	 try
 	 {
    //ask user for a time
    String aTime=scan.next();
    converted=convert.doConversion(aTime);
 	 }
 	 
 	 catch(TimeFormatException tfe)
 	 {
    System.out.println(tfe.getMessage());
 	 }
 	 
 	 System.out.printf("%s\nAgain(y/n)",converted);
 	 String desc = scan.next();
 	 char opt = desc.charAt(0);
 	 switch(opt)
 	 {
    case'n':
    System.exit(0);
    break;
    
    case'y':
    break;
 	 }
  }
  
	}
}



Any push in the right direction will greatly help :D

Is This A Good Question/Topic? 0
  • +

Replies To: Converting 24 hour time notation

#2 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Converting 24 hour time notation

Posted 27 January 2006 - 03:47 AM

I'm at a friends place who has no Java so I'll try and see what I can do without testing your code.
From the looks of it, here's what I found.
intHour= Integer.parseInt(minuets);
intMinuets= Integer.parseInt(hour);


I hope you realised what went wrong here....Logical Error.
If you get any other errors...post it here as well.
Was This Post Helpful? 0
  • +
  • -

#3 donnyj47  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 27-January 06

Re: Converting 24 hour time notation

Posted 27 January 2006 - 12:51 PM

OK, i fixed that little problem. However when you run the test class, my conversions are still off.
Was This Post Helpful? 0
  • +
  • -

#4 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Converting 24 hour time notation

Posted 28 January 2006 - 03:04 AM

I'm home and I fixed it.
  • String timeFormated = String.format("That is the same as %s:%s",hour,minuets);
    
    

    You have put this line as the first line of the doconversion function. Put it just before the return statement.


  • The println is nowhere close to the printf function so don't use the format specifiers like %s for the function. Instead to print the formatted output using this statement followed by another one that asks whether the user wishes to continue.[You can put them in one sentence too as shown later]
    System.out.println(converted);
    
    


    or...
    System.out.println(converted+" Again(y/n)? ");
    
    
Hope you know where you went wrong...The conversion code was flawless...(other than the previous mistake I pointed out).

Cheers.

This post has been edited by born2c0de: 28 January 2006 - 03:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 timjacquez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-November 12

Re: Converting 24 hour time notation

Posted 28 November 2012 - 06:24 PM

View Postborn2c0de, on 27 January 2006 - 03:47 AM, said:

I'm at a friends place who has no Java so I'll try and see what I can do without testing your code.
From the looks of it, here's what I found.
intHour= Integer.parseInt(minuets);
intMinuets= Integer.parseInt(hour);


I hope you realised what went wrong here....Logical Error.
If you get any other errors...post it here as well.


In case anyone was as lost as I was about what "went wrong here", the minutes and hour in parentheses need to be switched so that it reads
intHour= Integer.parseInt(minuets);
intMinuets= Integer.parseInt(hour);



I realize not everyone is as dense as me; however, I definitely would have saved time had I known this haha Other than that, great fix and great code. Thanks!

View Postborn2c0de, on 27 January 2006 - 03:47 AM, said:

I'm at a friends place who has no Java so I'll try and see what I can do without testing your code.
From the looks of it, here's what I found.
intHour= Integer.parseInt(minuets);
intMinuets= Integer.parseInt(hour);


I hope you realised what went wrong here....Logical Error.
If you get any other errors...post it here as well.


In case anyone was as lost as I was about what "went wrong here", the minutes and hour in parentheses need to be switched so that it reads
intHour= Integer.parseInt(hour);
intMinuets= Integer.parseInt(minuets);



I realize not everyone is as dense as me; however, I definitely would have saved time had I known this haha Other than that, great fix and great code. Thanks!

And my apologies for the retarded post. I can't seem to delete it... fuckin noob.<----
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1