answersLogoWhite

0


Best Answer

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.

01public class TimeConvert

02{

03

04//int value of hour

05private String hour;

06

07//int value of minuets

08private String minuets;

09

10public TimeConvert()

11{

12hour="00";

13minuets="00";

14}

15

16//static method to convert a 24 hour time to 12 hour time

17public String doConversion(String aTime) throws TimeFormatException

18{

19//formatted String

20String timeFormated = String.format("That is the same as %s:%s",hour,minuets);

21

22//find the position of the colon

23int colon=aTime.indexOf(':');

24

25//if there is no colon

26if(colon!=2)

27{

28TimeFormatException tfe= new TimeFormatException("You entered "+aTime+"\nFormat must be hh:mm\n");

29throw tfe;

30}

31

32else

33{

34//parse the numbers before the colon

35hour = aTime.substring(0,colon);

36int intHour=0;

37

38//parse the numbers after the colon

39minuets = aTime.substring(3);

40int intMinuets=0;

41

42try

43{

44intHour= Integer.parseInt(hour);

45intMinuets= Integer.parseInt(minutes);

46

47if(intHour <0 intHour>23)

48{

49throw new TimeFormatException("\nYou entered "+hour+"\nHour must be less than 23\n");

50}

51else if(intMinuets<0 intMinuets>59)

52{

53throw new TimeFormatException("\nYou entered "+minuets+"\nMinuets must be less than 59\n");

54}

55

56else

57{

58if(intHour==12)

59{

60minuets+="PM";

61}

62

63else if(intHour<12)

64{

65minuets+="AM";

66}

67

68else if(intHour>12)

69{

70intHour=intHour-12;

71hour=Integer.toString(intHour);

72minuets+="PM";

73}

74}

75}

76

77catch(NumberFormatException nfe)

78{

79TimeFormatException tfe = new TimeFormatException("\nYou entered "+aTime+"\nFormat to hh:mm\n");

80throw tfe;

81}

82

83}

84return timeFormated;

85}

86}

Here is the custom exception class

01class TimeFormatException extends Exception

02{

03//one String parameter constructor to pass to super class Exception

04TimeFormatException(String msg)

05{

06super(msg);

07}

08

09//concatenate 2 String messages and pass to super class Exception

10TimeFormatException(String msg,String msg1)

11{

12super(msg1+msg);

13}

14}

And here is the test class to run the program.

01import java.util.*;

02public class TimeConvertTest

03{

04public static void main(String args[])

05{

06//instantiate Scanner

07Scanner scan=new Scanner(System.in);

08

09//instantiate TimeConvert object

10TimeConvert convert = new TimeConvert();

11

12while(true)

13{

14System.out.print("\nEnter Time in 24-hour notation<hh:mm>: ");

15

16String converted="unknown";

17

18//try to convert the time

19try

20{

21//ask user for a time

22String aTime=scan.next();

23converted=convert.doConversion(aTime);

24}

25

26catch(TimeFormatException tfe)

27{

28System.out.println(tfe.getMessage());

29}

30

31System.out.printf("%s\nAgain(y/n)",converted);

32String desc = scan.next();

33char opt = desc.charAt(0);

34switch(opt)

35{

36case'n':

37System.exit(0);

38break;

39

40case'y':

41break;

42}

43}

44

45}46}

User Avatar

Wiki User

7y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a Java program that converts a time from a 24-hour notation to 12-hour notation?
Write your answer...
Submit
Still have questions?
magnify glass
imp