# Converting Military Time

• November 14th, 2012, 12:44 PM
greystreet34
Converting Military Time
Hi, I'm working on an assignment where I have to convert military time to standard time. I think I mostly have it solved except for when the minutes are between 0 and 10. For example,

909 should read 9:09 am, but it prints 9:9 am.

I'm not sure how to add a 0 before the minutes in that case. Any advice would be much appreciated, thanks!

Code :

```import java.util.Scanner;   public class TimeConvert { public static String militaryToOrdinaryTime(int milTime) { int hour = milTime / 100; int min = milTime%100; String period;   if (hour < 0 || hour > 24 || min < 0 || min > 59) { return ""; } else if (hour > 12) { hour = hour - 12; period = "pm"; } else { period = "am"; } if (hour == 0) { hour = 12; } else if (min == 0) { String ordTime = hour + " " + period; return ordTime; } else if (min < 10 && min > 0) { // needs fixing } String ordTime = hour + ":" + min + " " + period; return ordTime; }   public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter military Time (hhmm) : "); int time = in.nextInt();   System.out.println("Ordinary Time: " +militaryToOrdinaryTime(time)); } }```
• November 14th, 2012, 12:45 PM
KevinWorkman
Re: Converting Military Time
Might want to take a look at the DecimalFormat class.

Or the DateFormat class for that matter!
• November 14th, 2012, 01:34 PM
greystreet34
Re: Converting Military Time
I did, but we never went over any of those classes and I'm trying to figure it out with the tools we learned so far. (this is a first semester Java course).

--- Update ---

Ok, I got it to work using this:
Code :

``` String min1 = String.valueOf(min); min1 = "0" + min1; String ordTime = hour + ":" + min1 + " " + period; return ordTime;```
Although we never learned the valueOf class I assume it's ok to use. Not sure what else the teacher would have us do.
• November 14th, 2012, 01:39 PM
copeg
Re: Converting Military Time
This thread has been cross posted here:

Java Programming Forums Cross Posting Rules

The Problems With Cross Posting

• November 14th, 2012, 05:34 PM
jps
Re: Converting Military Time
Quote:

Originally Posted by greystreet34
Ok, I got it to work using this:
Code :

``` String min1 = String.valueOf(min); min1 = "0" + min1; String ordTime = hour + ":" + min1 + " " + period; return ordTime;```
Although we never learned the valueOf class I assume it's ok to use.

valueOf is not a class, it is a method of the String class. You should invest the time it takes to read every word in the documentation on the string class ... )dramatic pause( ... ten times!

Quote:

Originally Posted by greystreet34
Not sure what else the teacher would have us do.

Me neither. But some good suggestions were made. Did you search for information on DecimalFormat or DateFormat?
• November 16th, 2012, 03:48 PM
greystreet34
Re: Converting Military Time
Thanks for the help everyone, and for the information about cross posting. I wasn't sure about the rules for that and if I do continue to post on different forums I'll be sure to link the other discussions in.

I did look up information about DecimalFormat and DateFormat, but a lot of it seemed over my head for the time being!
• November 18th, 2012, 04:22 AM
jps
Re: Converting Military Time
Quote:

Hi, I'm working on an assignment
Quote:

I did look up information about DecimalFormat and DateFormat, but a lot of it seemed over my head for the time being!
It looks like this may have been the point of the assignment. Just a thought...