Program class to develop a time class

programclasstodevelopatimeclass.java

// programm class to deveolpe a time class

public class Time

{
 private int hour;
 private int minute;
 private int second;
 private String time1; // 12 hour clock display
 private String time2; // 24 hour display
 public String notation; // used to set as AM or PM
 public int notationhour; // used to set the hour from 24 hour clock hour to 12 hour clock
 public String tempmin, tempsec; // used for the outputting of the time
 
 

 // constructors

 public Time() // default constructor with no parameters
 {
  setTime(0,0,0);
 }
 

 public Time(int h) // overloaded constructor with one parameter
 {
  setTime(h,0,0);
 }
 

 public Time(int h, int m) // overloaded contructor with two parameters
 {
  setTime(h,m,0);
 }
 

 public Time(int h, int m, int s) // overloaded constructor with three parameters
 {
  setTime(h,m,s);
 }
 
 

 // set methods
 

 public void setTime(int h, int m, int s)
 {
  setHour(h);
  setMinute(m);
  setSecond(s);

  {

   if ((hour <=11)&&(hour>0))  // if loop to establish if it's AM or PM and adjust the hour accordingly
   {
    notation= "AM";
    notationhour = hour;
   }

   else if (hour >12)
   {
    notation = "PM";
    notationhour = hour - 12;
   }
   else if ( hour == 0) // special case when the time is midnight
   {
    notationhour = 12;
    notation = "AM";
   }

   else
   {
    notation = "PM";
    notationhour = hour;
   }
 
 

   if ((minute >= 0)&&(minute <=9))  // establishes if a zero is required in the displayed in front of the minute output
   {
    tempmin = ("0" + minute );
   }
   else
   {
    tempmin = (minute + "");
   }

   if ((second >= 0)&&(second <= 9))  // establishes if a zero is required to be displayed in front of the second output
   {
    tempsec = ("0" + second + " ");
   }
   else
   {
    tempsec = (second + " ");
   }
  }
 

  if (hour >9 ) // if loop establishes if a zero is required in the 24 hour display for the hour
  {
   time1 = (notationhour + " : " + tempmin + " : " + tempsec + notation); // sets the time sting
   time2 = (hour + tempmin + ""); // sets the 24 hour time string
  }
  else
  {
   time1 = (notationhour + " : " + tempmin + " : " + tempsec + notation); // sets the time string
   time2 = ("0" + hour + tempmin); // sets the 24 hour time string
  }

 }
 

 public void setHour(int h)
 {

  if (h>=0&&h<=23) // tests if the hour value is in range
  {
   hour = h;
  }
  else
  {
   hour = 12; // defaults the  hour to 12 when it's out of range
   // tells the user that the hour has been defaulted
   System.out.println("An incorrect hour has been entered and hour has been defaulted to 12 \n");
  }
 }
 

 public void setMinute(int m)
 {

  if (m>=0&&m<=59) // tests if the minute value is in range
  {
   minute = m;
  }

  else
  {
   minute = 0; // defaults minute to 0 when it's out of range
   // tells the user that the minute has been defaulted
   System.out.println("An incorrect minute has been entered and minute has been defaulted to 0 \n");
  }
 }
 

 public void setSecond(int s)
 {

  if (s>=0&&s<=59) // tests if the seconds are in range
  {
   second = s;
  }

  else
  {
   second = 0; // sets second to the default 0 when it's out of range
   // tells the user that the second has been defaulted
   System.out.println("An incorrect second has been entered and second has been defaulted to 0 \n");
  }
 }
 
 

 // get methods to return all the relevant values
 

 public int getHour()
 {
  return hour;
 }

 public int getMinute()
 {
  return minute;
 }

 public int getSecond()
 {
  return second;
 }

 public String getTime()
 {
  return time1;
 }

 public String get24hrTime()
 {
  return time2;
 }

}// ends the class

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.