Advertisement
GSculerlor

Untitled

Sep 14th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. public class Time2  
  2.  {  
  3.    private int hour;  
  4.    private int minute;  
  5.    private int second;  
  6.    public Time2()  
  7.    {  
  8.      this( 0, 0, 0 );  
  9.    }  
  10.    public Time2( int h )  
  11.    {  
  12.      this( h, 0, 0 );  
  13.    }  
  14.    public Time2 ( int h, int m )  
  15.    {  
  16.      this( h, m, 0 );  
  17.    }  
  18.    public Time2( int h, int m, int s )  
  19.    {  
  20.      setTime( h, m, s );  
  21.    }  
  22.    public Time2( Time2 time )  
  23.    {  
  24.      this( time.getHour(), time.getMinute(), time.getSecond() );  
  25.    }  
  26.    public void setTime( int h, int m, int s )  
  27.    {  
  28.      setHour( h );  
  29.      setMinute( m );  
  30.      setSecond( s );  
  31.    }  
  32.    public void setHour( int h )  
  33.    {  
  34.      if ( h >= 0 && h < 24 )  
  35.        hour = h;  
  36.      else  
  37.        throw new IllegalArgumentException( "hour must be 0-23" );  
  38.    }  
  39.    public void setMinute( int m )  
  40.    {  
  41.      if ( m >= 0 && m < 60 )  
  42.        minute = m;  
  43.      else  
  44.        throw new IllegalArgumentException( "minute must be 0-59" );  
  45.    }  
  46.    public void setSecond( int s )  
  47.    {  
  48.      if ( s >= 0 && s < 60 )  
  49.        second = ( ( s >= 0 && s < 60 ) ? s : 0 );  
  50.      else  
  51.        throw new IllegalArgumentException( "second must be 0-59" );  
  52.    }  
  53.    public int getHour()  
  54.    {  
  55.      return hour;  
  56.    }  
  57.    public int getMinute()  
  58.    {  
  59.      return minute;  
  60.    }  
  61.    public int getSecond()  
  62.    {  
  63.      return second;  
  64.    }  
  65.    public String toUniversalString()  
  66.    {  
  67.      return String.format(  
  68.        "%02d:%02d:%02d", getHour(), getMinute(), getSecond() );  
  69.    }  
  70.    public String toString()  
  71.    {  
  72.      return String.format( "%d:%02d:%02d %s",  
  73.        ( (getHour() == 0 || getHour() == 12 ) ? 12 : getHour() % 12 ),  
  74.        getMinute(), getSecond(), ( getHour() < 12 ? "AM" : "PM" ) );  
  75.    }  
  76.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement