我假設(shè)了你的已知時(shí)間類型為Calendar,如果不是你也可以自己改成Date類型,代碼如下:
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站建設(shè)、成都做網(wǎng)站與策劃設(shè)計(jì),豐寧網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:豐寧等地區(qū)。豐寧做網(wǎng)站價(jià)格咨詢:028-86922220
import?java.text.DateFormat;
import?java.text.SimpleDateFormat;
import?java.util.Calendar;
import?java.util.TimeZone;
public?class?Test?{
public?static?void?main(String[]?args)?{
//假如這個(gè)是你已知的時(shí)間類型
Calendar?cal?=?Calendar.getInstance();
cal.getTimeInMillis();
//北京時(shí)區(qū)GMT+8
Calendar?beijingcal?=?Calendar.getInstance();
beijingcal.clear();
beijingcal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
beijingcal.setTimeInMillis(cal.getTimeInMillis());
DateFormat?fmt?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
String?beijingFormatStr?=?fmt.format(beijingcal.getTime());
System.out.println(beijingFormatStr);
}
}
public Date getCST(String strGMT) throws ParseException {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
return df.parse(strGMT);
}
public String getGMT(Date dateCST) {
DateFormat df = new SimpleDateFormat("EEE, d-MMM-yyyy HH:mm:ss z", Locale.ENGLISH);
df.setTimeZone(TimeZone.getTimeZone("GMT")); // modify Time Zone.
return(df.format(dateCST));
}
2種方法僅供參考,希望 采納。
//我實(shí)現(xiàn)一個(gè)時(shí)鐘窗口程序給你了,好讓你更容易理解,希望對你有幫助。 import java.awt.*; import java.awt.event.*; import java.util.*; //世界時(shí)鐘 public class TimerTest { public static void main(String[] args) { new TimerTestFrame("世界時(shí)鐘"); } } class TimerTestFrame extends Frame { /** * */ private static final long serialVersionUID = 1L; public TimerTestFrame(String s) { super(s); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); setLayout(new GridLayout(2, 6)); ClockCanvas clk1 = new ClockCanvas("北京", "GMT+8"); // 創(chuàng)建時(shí)鐘 ClockCanvas clk2 = new ClockCanvas("巴黎", "GMT+2"); ClockCanvas clk3 = new ClockCanvas("華盛頓", "GMT-4"); ClockCanvas clk4 = new ClockCanvas("洛衫磯", "GMT-7"); ClockCanvas clk5 = new ClockCanvas("倫敦", "GMT+1"); ClockCanvas clk6 = new ClockCanvas("芝加哥", "GMT-5"); add(clk1); add(clk2); add(clk3); add(clk4); add(clk5); add(clk6); setSize(500, 350); // 設(shè)置框架寬高 setVisible(true); } } class ClockCanvas extends Canvas implements Runnable { /** * */ private static final long serialVersionUID = 1L; private int seconds = 0; private String city; private GregorianCalendar calendar; Thread t; public ClockCanvas(String c, String tz) { city = c; //也可以通過TimeZone.setTimeZone(String n)函數(shù)改變時(shí)區(qū),n為時(shí)區(qū)參數(shù)名。 calendar = new GregorianCalendar(TimeZone.getTimeZone(tz)); t = new Thread(this); t.start(); setSize(125, 125); // 設(shè)置畫布大小 setBackground(Color.black); } // 重寫父類的方法繪制時(shí)鐘圖形 public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; BasicStroke bstroke = new BasicStroke(2.0f); g2d.setStroke(bstroke); g2d.setColor(Color.green); g2d.drawOval(0, 0, 100, 100); bstroke = new BasicStroke(5.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 0, 50, 5); g2d.drawLine(0, 50, 5, 50); g2d.drawLine(50, 95, 50, 98); g2d.drawLine(95, 50, 98, 50); double hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60) / (12 * 60 * 60); double minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60); double secondAngle = 2 * Math.PI * (seconds - 15) / (60); bstroke = new BasicStroke(5.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (30 * Math.cos(hourAngle)), 50 + (int) (30 * Math.sin(hourAngle))); bstroke = new BasicStroke(3.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (40 * Math.cos(minuteAngle)), 50 + (int) (40 * Math.sin(minuteAngle))); bstroke = new BasicStroke(1.0f); g2d.setStroke(bstroke); g2d.drawLine(50, 50, 50 + (int) (45 * Math.cos(secondAngle)), 50 + (int) (45 * Math.sin(secondAngle))); g2d.setColor(Color.red); g2d.drawString(city, 35, 115); } public void timeElapsed() { //new Date()獲得當(dāng)前時(shí)間 System.out.println(new Date()); calendar.setTime(new Date()); seconds = calendar.get(Calendar.HOUR) * 60 * 60 + calendar.get(Calendar.MINUTE) * 60 + calendar.get(Calendar.SECOND); } public void run() { try { while (true) { Thread.sleep(300); timeElapsed(); repaint(); } } catch (InterruptedException e) { } } }