我想訪問run()方法中的字符串變量,並在jsp屏幕上顯示值。請注意,run()方法中保存的字符串變量的值根據以下代碼每1秒更改一次。因此,我的要求是在屏幕上顯示值並動態更改值。在JSP屏幕上顯示動態UTC時鐘
(OR)
簡而言之,我試圖展示的是在JSP屏幕上每隔一秒更改一次的UTC時鐘時間。
public class Clock implements Runnable {
JLabel jb;
//Constructor takes the clock JLabel
public Clock(JLabel jb) {
this.jb = jb;
}
public void run() {
while (true) {
try {
//Thread sleeps & updates ever 1 second, so the clock changes every 1 second.
jb.setText(timeNow());
String dynaStr= timeNow();
Thread.sleep(1000);
} catch (InterruptedException ex) {
System.out.println(ex);
}
}
}
public String timeNow() {
Calendar c = Calendar.getInstance();
TimeZone z = c.getTimeZone();
int offset = z.getRawOffset();
if(z.inDaylightTime(new Date())){
offset = offset + z.getDSTSavings();
}
int offsetHrs = offset/1000/60/60;
int offsetMins = offset/1000/60 % 60;
System.out.println("offset: " + offsetHrs);
System.out.println("offset: " + offsetMins);
c.add(Calendar.HOUR_OF_DAY, (-offsetHrs));
c.add(Calendar.MINUTE, (-offsetMins));
System.out.println("GMT Time: "+c.getTime());
return c.getTime().toString();
}
}
使用JavaScript而不是java。 – brso05 2014-10-28 14:06:46