Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Converting Military Time

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

    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));
        }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,443
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: Converting Military Time

    Might want to take a look at the DecimalFormat class.

    Or the DateFormat class for that matter!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
               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.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,330
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Converting Military Time

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/65037-converting-military-time.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Converting Military Time

    Quote Originally Posted by greystreet34 View Post
    Ok, I got it to work using this:
               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 View Post
    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?

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Converting Military Time

    Hi, I'm working on an assignment
    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...

Similar Threads

  1. Converting?
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 31st, 2012, 05:18 AM
  2. Replies: 2
    Last Post: October 19th, 2012, 03:32 AM
  3. Converting Name.
    By LoganC in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 08:09 AM
  4. Military time converter
    By ebone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 02:56 AM
  5. Converting an array
    By Scottj996 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2010, 08:58 AM