Q> use the 12-hour clock and also displays AM or PM as appropriate.
these are some classes which i have made to solve the problem
class ClockViewAnalog extends javax.swing.JFrame
{
private javax.swing.JLabel tLabel = new javax.swing.JLabel();
ClockViewAnalog()
{ // constructor
this.setDefaultCloseOperation(
javax.swing.WindowConstants.EXIT_ON_CLOSE);
this.setSize(95,45);
this.getContentPane().add(tLabel);
this.refreshTimeDisplay();
}
protected
String getDigitsAsString(int i)
{
String str = Integer.toString(i);
if (i<10) str = "0"+str;
return str;
}
public void refreshTimeDisplay()
{
TimeStamp t = new TimeStamp();
t.fillTimes();
String display;
if(t.hrs>=1 && t.mins<=59 && t.secs<=60 || t.hrs<=12 && t.mins<=59 && t.secs<=60 )
{
display = getDigitsAsString(t.hrs%12) + ":"
+ getDigitsAsString(t.mins) + ":"
+ getDigitsAsString(t.secs);
tLabel.setText(" " + display+"AM" );
tLabel.repaint();
}
else if(t.hrs>=13 && t.mins<=59 && t.secs<=60 || t.hrs<=23 && t.mins<=59 && t.secs<=60 )
{
display = getDigitsAsString(t.hrs%12) + ":"
+ getDigitsAsString(t.mins) + ":"
+ getDigitsAsString(t.secs);
tLabel.setText(" " + display+"PM" );
tLabel.repaint();
}
}
}
--------------------------
public class Clock
{
public static void main(String args[])
{
ClockViewAnalog cv = new ClockViewAnalog();
cv.setVisible(true);
// loop about every 0.5 seconds
try
{
for (;;)
{
cv.refreshTimeDisplay();
Thread.sleep(500);
}
}
catch (Exception e)
{
System.out.println("Error:"+e) ;
}
}
}
-----------------------------
class TimeStamp
{
int hrs;
int mins;
int secs;
void fillTimes()
{
java.util.Calendar now;
now = java.util.Calendar.getInstance();
hrs = now.get(java.util.Calendar.HOUR_OF_DAY);
mins = now.get(java.util.Calendar.MINUTE);
secs = now.get(java.util.Calendar.SECOND);
}
}
-----------------------------------
it show time exactly what i want but it doesnt actually work on the am pm thing