• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

Java time tick add time in Java

Dear Experts,

I'm having a really hard time with my homework.  I don't want anyone to do my homework, but I can't figure out what I'm doing wrong.  I don't know how to advance the time.    The instructor said to do it the using my tick method, but I can't get it to return a value.  Eclipse recommended a lot of my code so I usually use what it  recommends.  I could get this to work in one file so I made two files.   Was this a good or bad idea to make two files?

.
Instructions
Also a tick() method of type void that advances time for the Time object by 1 second.

Submit code with the following test scenario:

Create two time objects – one without argument and the second with 23,59, 55.
Print the starting time for both objects in military and regular time.
Advance the first time object (no – arg) by 100 ticks
Advance the second object by 10 ticks.
Print the time for both the objects in military and regular time formats.

 
import java.text.DecimalFormat;

public class TimeA4P2 {

	private static int hour;   
	private static int minute; 
	private static int second;
	  
	 public TimeA4P2(){
	   setTime(0, 0, 0);
	 }	  

	public void setTime(int h, int m, int s) {
		hour = (( h >= 0 && h < 24) ? h : 0);
		minute = (( m >= 0 && m < 60) ? m : 0);
		second = (( s >= 0 && s < 60) ? s : 0);
	}
	
	public String getMilitaryTime()
	   {
		  DecimalFormat twoDigits = new DecimalFormat("00");
		  
		  return twoDigits.format(hour) + ":" +
		  		 twoDigits.format(minute) + ":" +
		  		 twoDigits.format(second);
	   } 	  
	  
	 public String getRegularTime()
	   {
		  DecimalFormat twoDigits = new DecimalFormat("00");
		  
		  return ((hour == 12 || hour == 0) ? 12 : hour % 12) +
				  ":" + twoDigits.format(minute) +
				  ":" + twoDigits.format(second) +
				  (hour < 12 ? " AM" : " PM");   
		   
	   }
	 //Adds the time 
	 
	 public int tick() {
	      
		 setTime(23, 59, 55);
 	   	  second += 1;
	   	   
	   	 if(second % 60 == 0);
	   	 	minute +=minute;
	   	 	if(minute % 60 == 0);
	   	 	   hour +=1;
	   	 	   if(hour % 24 == 0);
	   	 	   	 	hour = 0;
	   	
	   return (hour, minute, second);	 	   	 	
	
		 // I think this should return 23, 59, 56
	}
		}   	   	           


import javax.swing.JOptionPane;

public class TimeTestA4P2{

	
	public static void main(String[] args) {
		
			TimeA4P2 t1 = new TimeA4P2();
			TimeA4P2 t2 = new TimeA4P2();
			
			String output;
			
		    output = "The 1st Military time is: "
		    + t1.getMilitaryTime() +
		    "\nThe 1st Regular time is:  " + t1.getRegularTime();    
					
			
		    
		    t1.setTime (15, 30, 25);
		    output += "\n\nThe 2nd Military time is:  " + 
		    t1.getMilitaryTime() +
		    "\nThe 2nd Regular time is:  " + 
		    t1.getRegularTime();   
		    

   	        t1.setTime(23, 59, 55);
   	        output += "\n\nThe 3rd Military time is:  " + 
		    t1.getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " + 
		    t1.getRegularTime(); 	
   	        
   	        output += "\n\nThe 3rd Military time is:  " + 
		    tick.getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " + 
		    tick.getRegularTime();    	        
   	        
   	        
		    JOptionPane.showMessageDialog( null, output,
		    	 "Time for Assigment 4",
		    	  JOptionPane.INFORMATION_MESSAGE );	
		    
		    System.exit( 0 );	
   	        }	    
		    
	}

Open in new window

0
WalterAPO
Asked:
WalterAPO
  • 4
1 Solution
 
for_yanCommented:
I'm not sure I understadn your goal - this is an example
of changing time by second  - myabe it has some connection to your question.

If not perhaps you can explain onece again what you need.

public class Tick {
 public static void main(String[] args) {


        SimpleDateFormat sdf100 = new SimpleDateFormat("HH:mm:ss");
        java.util.Date dd100 = sdf100.parse("23:55:59",new ParsePosition(0));

        for(int j =0; j<10; j++) {
            
          java.util.Date ddcur = new java.util.Date(dd100.getTime() + j*1000L);
            System.out.println("date: " + sdf100.format(ddcur));
        }

}

Open in new window



date: 23:55:59
date: 23:56:00
date: 23:56:01
date: 23:56:02
date: 23:56:03
date: 23:56:04
date: 23:56:05
date: 23:56:06
date: 23:56:07
date: 23:56:08

Open in new window

0
 
for_yanCommented:
I'm not sure I understood all requirements - but compile this and look at it;
I think we can then go from here:

import javax.swing.*;
import java.text.DecimalFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class TimeA4P2 {

       SimpleDateFormat sdf100 = new SimpleDateFormat("HH:mm:ss");
       java.util.Date dd200 = sdf100.parse("23:55:59",new ParsePosition(0));

	private static int hour;
	private static int minute;
	private static int second;

	 public TimeA4P2(){
	   setTime(0, 0, 0);
	 }

     public TimeA4P2(int h, int m, int s){
	   dd200=setTime(h, m, s);
	 }


	public java.util.Date  setTime(int h, int m, int s) {
        String hr = "" + h;
        String mi = "" + m;
        String se = "" + s;

        while(hr.length()<2)hr = "0"+hr;
           while(mi.length()<2)mi = "0"+mi;
           while(se.length()<2)se = "0"+se;


             java.util.Date dd100 = sdf100.parse(hr+":" +mi + ":" + se,new ParsePosition(0));

        return dd100;

		//hour = (( h >= 0 && h < 24) ? h : 0);
	//	minute = (( m >= 0 && m < 60) ? m : 0);
//		second = (( s >= 0 && s < 60) ? s : 0);

	}

	public String getMilitaryTime()
	   {

           return sdf100.format(dd200);
		 // DecimalFormat twoDigits = new DecimalFormat("00");
            /*
		  return twoDigits.format(hour) + ":" +
		  		 twoDigits.format(minute) + ":" +
		  		 twoDigits.format(second);
           */
	   }

	 public String getRegularTime()
	   {

            SimpleDateFormat sdf101 = new SimpleDateFormat("hh:mm:ss a");
           return sdf101.format(dd200);
           /*
		  DecimalFormat twoDigits = new DecimalFormat("00");

		  return ((hour == 12 || hour == 0) ? 12 : hour % 12) +
				  ":" + twoDigits.format(minute) +
				  ":" + twoDigits.format(second) +
				  (hour < 12 ? " AM" : " PM");
           */

	   }
	 //Adds the time

	 public void tick() {


           dd200 = new java.util.Date(dd200.getTime() + 1000L);

       //  return ddcur;
		 /*
         setTime(23, 59, 55);
 	   	  second += 1;

	   	 if(second % 60 == 0);
	   	 	minute +=minute;
	   	 	if(minute % 60 == 0);
	   	 	   hour +=1;
	   	 	   if(hour % 24 == 0);
	   	 	   	 	hour = 0;

	   return (hour, minute, second);	 	   	 	

         */
		 // I think this should return 23, 59, 56
	}
		}




class TimeTestA4P2{


	public static void main(String[] args) {

			TimeA4P2 t1 = new TimeA4P2(15,30,25);
			TimeA4P2 t2 = new TimeA4P2(23,59,55);

			String output = "";





                  //  t1.setTime (15, 30, 25);


		    output = "The 1st Military time is: "
		    + t1.getMilitaryTime() +
		    "\nThe 1st Regular time is:  " + t1.getRegularTime();

                   t1.tick();


		    output += "\n\nThe 2nd Military time is:  " +
		    t1.getMilitaryTime() +
		    "\nThe 2nd Regular time is:  " +
		    t1.getRegularTime();
                         t1.tick();


   	        output += "\n\nThe 3rd Military time is:  " +
		    t1.getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " +
		    t1.getRegularTime();


         t2.setTime(23, 59, 55);

        output += "\n\nThe 1st Military time is: "
                  + t2.getMilitaryTime() +
                  "\nThe 1st Regular time is:  " + t2.getRegularTime();


                              /*
   	        output += "\n\nThe 3rd Military time is:  " +
		    t1.tick().getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " +
		    tick.getRegularTime();
                                 */


		    JOptionPane.showMessageDialog( null, output,
		    	 "Time for Assigment 4",
		    	  JOptionPane.INFORMATION_MESSAGE );

		    System.exit( 0 );


	}
}

Open in new window

0
 
for_yanCommented:
Hope this is waht you want:

import javax.swing.*;
import java.text.DecimalFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;

public class TimeA4P2 {

       SimpleDateFormat sdf100 = new SimpleDateFormat("HH:mm:ss");
      java.util.Date dd200; // = sdf100.parse("23:55:59",new ParsePosition(0));

	private static int hour;
	private static int minute;
	private static int second;

	 public TimeA4P2(){
	    dd200=setTime(0, 0, 0);
	 }

     public TimeA4P2(int h, int m, int s){
	   dd200=setTime(h, m, s);
	 }


	public java.util.Date  setTime(int h, int m, int s) {
        String hr = "" + h;
        String mi = "" + m;
        String se = "" + s;

        while(hr.length()<2)hr = "0"+hr;
           while(mi.length()<2)mi = "0"+mi;
           while(se.length()<2)se = "0"+se;


             java.util.Date dd100 = sdf100.parse(hr+":" +mi + ":" + se,new ParsePosition(0));

        return dd100;

		//hour = (( h >= 0 && h < 24) ? h : 0);
	//	minute = (( m >= 0 && m < 60) ? m : 0);
//		second = (( s >= 0 && s < 60) ? s : 0);

	}

	public String getMilitaryTime()
	   {

           return sdf100.format(dd200);
		 // DecimalFormat twoDigits = new DecimalFormat("00");
            /*
		  return twoDigits.format(hour) + ":" +
		  		 twoDigits.format(minute) + ":" +
		  		 twoDigits.format(second);
           */
	   }

	 public String getRegularTime()
	   {

            SimpleDateFormat sdf101 = new SimpleDateFormat("hh:mm:ss a");
           return sdf101.format(dd200);
           /*
		  DecimalFormat twoDigits = new DecimalFormat("00");

		  return ((hour == 12 || hour == 0) ? 12 : hour % 12) +
				  ":" + twoDigits.format(minute) +
				  ":" + twoDigits.format(second) +
				  (hour < 12 ? " AM" : " PM");
           */

	   }
	 //Adds the time

	 public void tick() {


           dd200 = new java.util.Date(dd200.getTime() + 1000L);

       //  return ddcur;
		 /*
         setTime(23, 59, 55);
 	   	  second += 1;

	   	 if(second % 60 == 0);
	   	 	minute +=minute;
	   	 	if(minute % 60 == 0);
	   	 	   hour +=1;
	   	 	   if(hour % 24 == 0);
	   	 	   	 	hour = 0;

	   return (hour, minute, second);	 	   	 	

         */
		 // I think this should return 23, 59, 56
	}
		}




class TimeTestA4P2{


	public static void main(String[] args) {

			TimeA4P2 t1 = new TimeA4P2();
			TimeA4P2 t2 = new TimeA4P2(23,59,55);

			String output = "";





                  //  t1.setTime (15, 30, 25);


		    output = "The 1st Military time is: "
		    + t1.getMilitaryTime() +
		    "\nThe 1st Regular time is:  " + t1.getRegularTime();

                   t1.tick();


		    output += "\n\nThe 2nd Military time is:  " +
		    t1.getMilitaryTime() +
		    "\nThe 2nd Regular time is:  " +
		    t1.getRegularTime();
                         t1.tick();


   	        output += "\n\nThe 3rd Military time is:  " +
		    t1.getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " +
		    t1.getRegularTime();


         t2.setTime(23, 59, 55);

        output += "\n\nThe 1st Military time is: "
                  + t2.getMilitaryTime() +
                  "\nThe 1st Regular time is:  " + t2.getRegularTime();

        	    output += "\n\nThe 2nd Military time is:  " +
		    t2.getMilitaryTime() +
		    "\nThe 2nd Regular time is:  " +
		    t1.getRegularTime();
                         t1.tick();


   	        output += "\n\nThe 3rd Military time is:  " +
		    t2.getMilitaryTime() +
		    "\nThe 3rd Regular time is:  " +
		    t1.getRegularTime();


         


		    JOptionPane.showMessageDialog( null, output,
		    	 "Time for Assigment 4",
		    	  JOptionPane.INFORMATION_MESSAGE );

		    System.exit( 0 );


	}
}

Open in new window


0
 
for_yanCommented:
Please, ask me if something seems  not clear to you - and I'll try to  explain
times.PNG
0
 
WalterAPOAuthor Commented:
Genius Yan,

Thank you for the help.  I completely understood you solution and now my program works.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now