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 2 of 2

Thread: Converting Military Time to Standard Time

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Converting Military Time to Standard Time

    I have two classes. time_runner is used for testing my code.

    This is what I'm using to test my code:

     
    class time_runner
    {
     
    	public static void main(String str[]) throws IOException
    	{
     
    		Time time1 = new Time(14, 56);
    		System.out.println("time1: " + time1);
    		System.out.println("convert time1 to standard time: " + time1.convert());
    		System.out.println("time1: " + time1);
    		System.out.print("increment time1 five times: ");
    		time1.increment();
    		time1.increment();
    		time1.increment();
    		time1.increment();
    		time1.increment();
    		System.out.println(time1 + "\n");
     
    		Time time2 = new Time(-7, 12);
    		System.out.println("time2: " + time2);
    		System.out.print("increment time2 67 times: ");
    		for (int i = 0; i < 67; i++)
    			time2.increment();
    		System.out.println(time2);
    		System.out.println("convert to time2 standard time: " + time2.convert());
    		System.out.println("time2: " + time2 + "\n");
     
     
    		Time time3 = new Time(5, 17);
    		System.out.println("time3: " + time3);
    		System.out.print("convert time3: ");
    		System.out.println(time3.convert());
     
    		Time time4 = new Time(12, 15);
    		System.out.println("\ntime4: " + time4);
    		System.out.println("convert time4: " + time4.convert());
     
    		Time time5 = new Time(0, 15);
    		System.out.println("\ntime5: " + time5);
    		System.out.println("convert time5: " + time5.convert());
     
    		Time time6 = new Time(24, 15);
    		System.out.println("\ntime6: " + time6);
    		System.out.println("convert time6: " + time6.convert());
     
    		Time time7 = new Time(23,59);
    		System.out.println("\ntime7: " + time7);
    		System.out.println("convert time7: " + time7.convert());
    		time7.increment();
    		System.out.println("increment time7: " + time7);
    		System.out.println("convert time7: " + time7.convert());
     
    	}
     
    }

    This is what I'm working on completing. The two constructors are "Time()", which is the default constructor that sets the time to 1200, and "Time(int h, int m)" Which says If h is between 1 and 23 inclusive, set the hour to h. Otherwise, set the hour to 0. If m is between 0 and 59 inclusive, set the minutes to m. Otherwise, set the minutes to 0. Those are my two constructors that I pretty much have down. The three methods however I'm having trouble with. The "String toString()" Returns the time as a String of length 4. The "String convert()" Returns the time as a String converted from military time to standard time. The "void increment()" Advances the time by one minute.

    public class Time
    {
        private int hour;
        private int minute;   
     
        public Time(int h, int m) {
        	if(h > 1 && h < 23)
        		hour = h;
        	else
        		hour = 0;
     
        	if(m > 0 && m < 59)
        		minute = m;
        	else
        		minute = 0;
        }
     
        public String toString(){
     
        	return null;
        }
     
        public String convert(){
     
        	return null;
        }
     
        void increment(){
     
        }
     
    }

    I've got one of the constructors pretty much down, I just have no clue what I'm doing with the three methods. Any insight would be appreciated. Tyvm!


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    You might want to take an extra look at your bounds check on hour and minutes in your constructor, what if I wanted to input the time 23:59? Or 01:59.

    As for your methods you could use the StringBuilder class to append the different parts of your output together. Then return .toString() of your StringBuilder object.

    Converting from 24h clock to am/pm you would have to use an if statement to check if your time is in am or pm, and then output accordingly.

    Rolf

Similar Threads

  1. Problem in getting time of different time zone
    By anks.coder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 13th, 2013, 05:44 PM
  2. Using Date() to get Start Time and Finish Time of a copyFiles method
    By dalythe in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 17th, 2013, 09:50 PM
  3. Hello, first time caller long time programmer....
    By P2C2N in forum Member Introductions
    Replies: 3
    Last Post: December 10th, 2012, 11:53 AM
  4. Converting Military Time
    By greystreet34 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 18th, 2012, 05:22 AM
  5. Military time converter
    By ebone in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 02:56 AM