wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes convert to 12 hour clock from 24 hour clock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "convert to 12 hour clock from 24 hour clock" Watch "convert to 12 hour clock from 24 hour clock" New topic
Author

convert to 12 hour clock from 24 hour clock

Rishal Dev Singh
Greenhorn

Joined: Jun 18, 2012
Posts: 5
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
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Rishal Dev Singh wrote:it show time exactly what i want but it doesnt actually work on the am pm thing

First: Please UseCodeTags (←click - and please read the page thoroughly; you may need to break up some of those long lines).

Second: Isolate the problem.
You're trying to display the time as AM/PM, so do that first and get it working before you add in all that GUI stuff.

Third: Have a look at the documentation for Calendar.HOUR_OF_DAY. Why do you think it might not be what you want?

Fourth: Have a look at the docs for the rest of the Calendar class (particularly the fields). See any that you think might help?

Fifth: Why are you using a TimeStamp? Were you told to? Calendars work best with Dates (java.util.Date).

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rishal Dev Singh
Greenhorn

Joined: Jun 18, 2012
Posts: 5
i have done it using Calendar.HOUR_OF_DAY i had created a method name sayTime which is now working fine....
i was using TimeStamp because i m reffering a book named Just java 2 second edition and i was solving one of the exercises actually they have the examples using TimeStamp and i just adapted from there ...


i have to import java.util.text.*;

should i am able to do it the other way???
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

Rishal Dev Singh wrote:should i am able to do it the other way???

Absolutely. Just one:and there are probably better.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: convert to 12 hour clock from 24 hour clock