Java时间格式转换获得时区

  1. import java.awt.*;   
  2. import java.awt.event.*;   
  3. import java.util.*;   
  4.   
  5. //世界时钟   
  6. public class TimerTest {   
  7.  public static void main(String[] args) {   
  8.   new TimerTestFrame("世界时钟");   
  9.  }   
  10. }   
  11.   
  12. class TimerTestFrame extends Frame {   
  13.  /**  
  14.   *   
  15.   */  
  16.  private static final long serialVersionUID = 1L;   
  17.   
  18.  public TimerTestFrame(String s) {   
  19.   super(s);   
  20.   addWindowListener(new WindowAdapter() {   
  21.    public void windowClosing(WindowEvent e) {   
  22.     dispose();   
  23.     System.exit(0);   
  24.    }   
  25.   });   
  26.   setLayout(new GridLayout(26));   
  27.   
  28.   ClockCanvas clk1 = new ClockCanvas("北京""GMT+8"); // 创建时钟   
  29.   ClockCanvas clk2 = new ClockCanvas("巴黎""GMT+2");   
  30.   ClockCanvas clk3 = new ClockCanvas("华盛顿""GMT-4");   
  31.   ClockCanvas clk4 = new ClockCanvas("洛衫矶""GMT-7");   
  32.   ClockCanvas clk5 = new ClockCanvas("伦敦""GMT+1");   
  33.   ClockCanvas clk6 = new ClockCanvas("芝加哥""GMT-5");   
  34.   add(clk1);   
  35.   add(clk2);   
  36.   add(clk3);   
  37.   add(clk4);   
  38.   add(clk5);   
  39.   add(clk6);   
  40.   setSize(500350); // 设置框架宽高   
  41.   setVisible(true);   
  42.  }   
  43. }   
  44.   
  45. class ClockCanvas extends Canvas implements Runnable {   
  46.  /**  
  47.   *   
  48.   */  
  49.  private static final long serialVersionUID = 1L;   
  50.   
  51.  private int seconds = 0;   
  52.   
  53.  private String city;   
  54.   
  55.  private GregorianCalendar calendar;   
  56.   
  57.  Thread t;   
  58.   
  59.  public ClockCanvas(String c, String tz) {   
  60.   city = c;   
  61.                   //也可以通过TimeZone.setTimeZone(String n)函数改变时区,n为时区参数名。   
  62.   calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));   
  63.   t = new Thread(this);   
  64.   t.start();   
  65.   setSize(125125); // 设置画布大小   
  66.   setBackground(Color.black);   
  67.  }   
  68.   
  69.  // 重写父类的方法绘制时钟图形   
  70.  public void paint(Graphics g) {   
  71.   Graphics2D g2d = (Graphics2D) g;   
  72.   BasicStroke bstroke = new BasicStroke(2.0f);   
  73.   g2d.setStroke(bstroke);   
  74.   g2d.setColor(Color.green);   
  75.   g2d.drawOval(00100100);   
  76.   bstroke = new BasicStroke(5.0f);   
  77.   g2d.setStroke(bstroke);   
  78.   g2d.drawLine(500505);   
  79.   g2d.drawLine(050550);   
  80.   g2d.drawLine(50955098);   
  81.   g2d.drawLine(95509850);   
  82.   double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60)   
  83.     / (12 * 60 * 60);   
  84.   double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60);   
  85.   double secondAngle = 2 * Math.PI * (seconds - 15) / (60);   
  86.   bstroke = new BasicStroke(5.0f);   
  87.   g2d.setStroke(bstroke);   
  88.   g2d.drawLine(505050 + (int) (30 * Math.cos(hourAngle)),   
  89.     50 + (int) (30 * Math.sin(hourAngle)));   
  90.   bstroke = new BasicStroke(3.0f);   
  91.   g2d.setStroke(bstroke);   
  92.   g2d.drawLine(505050 + (int) (40 * Math.cos(minuteAngle)),   
  93.     50 + (int) (40 * Math.sin(minuteAngle)));   
  94.   bstroke = new BasicStroke(1.0f);   
  95.   g2d.setStroke(bstroke);   
  96.   g2d.drawLine(505050 + (int) (45 * Math.cos(secondAngle)),   
  97.     50 + (int) (45 * Math.sin(secondAngle)));   
  98.   g2d.setColor(Color.red);   
  99.   g2d.drawString(city, 35115);   
  100.  }   
  101.   
  102.  public void timeElapsed() {   
  103.                   //new Date()获得当前时间   
  104.   System.out.println(new Date());   
  105.   calendar.setTime(new Date());   
  106.   seconds = calendar.get(Calendar.HOUR) * 60 * 60  
  107.     + calendar.get(Calendar.MINUTE) * 60  
  108.     + calendar.get(Calendar.SECOND);   
  109.  }   
  110.   
  111.  public void run() {   
  112.   try {   
  113.    while (true) {   
  114.     Thread.sleep(300);   
  115.     timeElapsed();   
  116.     repaint();   
  117.    }   
  118.   } catch (InterruptedException e) {   
  119.   }   
  120.  }   
  121. }  
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓