就在我这个程序上面添加个什么方法 或者条件!! 就可以实现同时显示不同时区的时间!!!
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class MyClock implements Runnable{
Frame f;
Label time1;
Label time2;
Label time3;
Label time4;
Label time5;
Label time6; public MyClock(){
f=new Frame("时刻表");
time1=new Label("Asia/shanghai");
time2=new Label("Europe/Moscow");
time3=new Label("America/Los_Angeles");
time4=new Label();
time5=new Label();
time6=new Label();
f.setLayout(new GridLayout(3,2));
f.add(time1);
f.add(time4);
f.add(time2);
f.add(time5);
f.add(time3);
f.add(time6);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
time4.setFont(new Font("Arial", Font.PLAIN, 20));
time5.setFont(new Font("Arial", Font.PLAIN, 20));
time6.setFont(new Font("Arial", Font.PLAIN, 20));
Thread a=new Thread(this); a.start();
}
public void run(){
//System.out.println("111111111111111");
while(true){
try{
//System.out.println("aaaaaaaaaaaaaaaaaa");
Thread.sleep(1000);
time4.setText(getTime());
time5.setText(getTime());
time6.setText(getTime()); }
catch(Exception e){
e.printStackTrace();
}
}
}
private String getTime(){
GregorianCalendar th=new GregorianCalendar();
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
return th.get(GregorianCalendar.HOUR)+":"+
th.get(GregorianCalendar.MINUTE)+":"+
th.get(GregorianCalendar.SECOND);
}
public static TimeZone getDefault(){
return TimeZone.getDefault();
}
public static void main(String []args){
MyClock h=new MyClock();
}
}
import java.awt.*;
import java.awt.event.*;
import java.util.*;public class MyClock implements Runnable{
Frame f;
Label time1;
Label time2;
Label time3;
Label time4;
Label time5;
Label time6; public MyClock(){
f=new Frame("时刻表");
time1=new Label("Asia/shanghai");
time2=new Label("Europe/Moscow");
time3=new Label("America/Los_Angeles");
time4=new Label();
time5=new Label();
time6=new Label();
f.setLayout(new GridLayout(3,2));
f.add(time1);
f.add(time4);
f.add(time2);
f.add(time5);
f.add(time3);
f.add(time6);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
time4.setFont(new Font("Arial", Font.PLAIN, 20));
time5.setFont(new Font("Arial", Font.PLAIN, 20));
time6.setFont(new Font("Arial", Font.PLAIN, 20));
Thread a=new Thread(this); a.start();
}
public void run(){
//System.out.println("111111111111111");
while(true){
try{
//System.out.println("aaaaaaaaaaaaaaaaaa");
Thread.sleep(1000);
time4.setText(getTime());
time5.setText(getTime());
time6.setText(getTime()); }
catch(Exception e){
e.printStackTrace();
}
}
}
private String getTime(){
GregorianCalendar th=new GregorianCalendar();
Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("Europe/Moscow"));
return th.get(GregorianCalendar.HOUR)+":"+
th.get(GregorianCalendar.MINUTE)+":"+
th.get(GregorianCalendar.SECOND);
}
public static TimeZone getDefault(){
return TimeZone.getDefault();
}
public static void main(String []args){
MyClock h=new MyClock();
}
}
private String getTime(String timeZone) {
GregorianCalendar th = new GregorianCalendar(TimeZone
.getTimeZone(timeZone));
return th.get(GregorianCalendar.HOUR) + ":"
+ th.get(GregorianCalendar.MINUTE) + ":"
+ th.get(GregorianCalendar.SECOND);
}
之前是建好的cal实例没用啊。
public void run() {
while (true) {
try {
Thread.sleep(1000);
time4.setText(getTime("Europe/Moscow"));
time5.setText(getTime("China/Shanghai"));
time6.setText(getTime("America/Los_Angeles"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
即String.length() == 12:第一个数大于8,第二个数必小于0,即第一位是"-";
且first.length - (second.length-1) == 0
且str.substring(0,first.length - 2)相同
如first = 1234589 (len = 7)
second =-1234581 (len = 7)
12345相同
first = 1234580
second =-1234572
12345 相同