Home > Net >  Time code gives a negative value when in the time is in am
Time code gives a negative value when in the time is in am

Time:02-10

import java.util.Calendar; 
//
public class Time12 extends Time {
//
public Time12 () {`
super();
}
private String ampm;
private int hour;
//
public boolean isAm () {
/**/
if ( ampm.equals("am") ) return true;
else                     return false;
/**/
}
@Override
public String toString () {
    //
    Calendar c;
    if ( hour == 0 ) hour = 12;
    c = Calendar.getInstance();
    if ( c.get(Calendar.AM_PM) == 0) ampm = "am";
    else                             ampm = "pm";
    boolean isAm;
    int h12;
    //
    isAm = isAm();
    h12 = getHour();
    h12 = h12 - 12;
    //
    return Time.i2s(h12)   ":"   Time.i2s(getMinute())   ":"   Time.i2s(getSecond())   ampm;
    } 
 }

The program here prints the time on a 12 hour clock. When I run it with two others programs that are not shown here the am time is printed to the console as a negative number.

CodePudding user response:

  •  Tags:  
  • Related