java設置時區
❶ java DateFormat類的.setTimeZone(TimeZone zone)怎樣使用
setTimeZone
publicvoidsetTimeZone(TimeZonezone)
為此DateFormat對象的日歷設置時區。
參數:
zone-給定的新時區。
TimeZone
getDefault
()
獲取此主機的默認TimeZone。默認TimeZone的來源可能隨實現的變化而變化。
返回:
默認的TimeZone。
另請參見:
setDefault(java.util.TimeZone)
getTimeZone
(StringID)
獲取給定ID的TimeZone。
參數:
ID-TimeZone的ID,要麼是縮寫(如"PST"),要麼是全名(如"America/Los_Angeles"),要麼是自定義ID(如"GMT-8:00")。注意,對縮寫的支持只是出於JDK1.1.x兼容性的考慮,因此應該使用全名。
返回:
指定的TimeZone,如果給定的ID無法理解,則返回GMT區域。
DateFormatdateFormat=newSimpleDateFormat();
//dateFormat.setTimeZone(TimeZone.getDefault());
dateFormat.setTimeZone(TimeZone.getTimeZone("EET"));
System.out.println(dateFormat.format(newDate()));
❷ java 時區轉換
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種方法僅供參考,希望 採納。
❸ 關於JAVA時間格式轉換問題,涉及時區
//我實現一個時鍾窗口程序給你了,好讓你更容易理解,希望對你有幫助。 import java.awt.*; import java.awt.event.*; import java.util.*; //世界時鍾 public class TimerTest { public static void main(String[] args) { new TimerTestFrame("世界時鍾"); } } 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"); // 創建時鍾 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); // 設置框架寬高 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)函數改變時區,n為時區參數名。 calendar = new GregorianCalendar(TimeZone.getTimeZone(tz)); t = new Thread(this); t.start(); setSize(125, 125); // 設置畫布大小 setBackground(Color.black); } // 重寫父類的方法繪制時鍾圖形 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()獲得當前時間 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) { } } }
❹ java時間轉換,帶時區的
我假設了你的已知時間類型為Calendar,如果不是你也可以自己改成Date類型,代碼如下:
importjava.text.DateFormat;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
importjava.util.TimeZone;
publicclassTest{
publicstaticvoidmain(String[]args){
//假如這個是你已知的時間類型
Calendarcal=Calendar.getInstance();
cal.getTimeInMillis();
//北京時區GMT+8
Calendarbeijingcal=Calendar.getInstance();
beijingcal.clear();
beijingcal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
beijingcal.setTimeInMillis(cal.getTimeInMillis());
DateFormatfmt=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
StringbeijingFormatStr=fmt.format(beijingcal.getTime());
System.out.println(beijingFormatStr);
}
}
❺ java中如何獲取時區
//我實現一個時鍾窗口程序給你了,好讓你更容易理解,希望對你有幫助。x0dx0aimport java.awt.*;x0dx0aimport java.awt.event.*;x0dx0aimport java.util.*;x0dx0ax0dx0a//世數粗攔界時鍾凳擾x0dx0apublic class TimerTest {x0dx0apublic static void main(String[] args) {x0dx0anew TimerTestFrame("世薯胡界時鍾");x0dx0a}x0dx0a}x0dx0ax0dx0aclass TimerTestFrame extends Frame {x0dx0a/**x0dx0a* x0dx0a*/x0dx0aprivate static final long serialVersionUID = 1L;x0dx0ax0dx0apublic TimerTestFrame(String s) {x0dx0asuper(s);x0dx0aaddWindowListener(new WindowAdapter() {x0dx0apublic void windowClosing(WindowEvent e) {x0dx0adispose();x0dx0aSystem.exit(0);x0dx0a}x0dx0a});x0dx0asetLayout(new GridLayout(2, 6));x0dx0ax0dx0aClockCanvas clk1 = new ClockCanvas("北京", "GMT+8"); // 創建時鍾x0dx0aClockCanvas clk2 = new ClockCanvas("巴黎", "GMT+2");x0dx0aClockCanvas clk3 = new ClockCanvas("華盛頓", "GMT-4");x0dx0aClockCanvas clk4 = new ClockCanvas("洛衫磯", "GMT-7");x0dx0aClockCanvas clk5 = new ClockCanvas("倫敦", "GMT+1");x0dx0aClockCanvas clk6 = new ClockCanvas("芝加哥", "GMT-5");x0dx0aadd(clk1);x0dx0aadd(clk2);x0dx0aadd(clk3);x0dx0aadd(clk4);x0dx0aadd(clk5);x0dx0aadd(clk6);x0dx0asetSize(500, 350); // 設置框架寬高x0dx0asetVisible(true);x0dx0a}x0dx0a}x0dx0ax0dx0aclass ClockCanvas extends Canvas implements Runnable {x0dx0a/**x0dx0a* x0dx0a*/x0dx0aprivate static final long serialVersionUID = 1L;x0dx0ax0dx0aprivate int seconds = 0;x0dx0ax0dx0aprivate String city;x0dx0ax0dx0aprivate GregorianCalendar calendar;x0dx0ax0dx0aThread t;x0dx0ax0dx0apublic ClockCanvas(String c, String tz) {x0dx0acity = c;x0dx0a//也可以通過TimeZone.setTimeZone(String n)函數改變時區,n為時區參數名。x0dx0acalendar = new GregorianCalendar(TimeZone.getTimeZone(tz));x0dx0at = new Thread(this);x0dx0at.start();x0dx0asetSize(125, 125); // 設置畫布大小x0dx0asetBackground(Color.black);x0dx0a}x0dx0ax0dx0a// 重寫父類的方法繪制時鍾圖形x0dx0apublic void paint(Graphics g) {x0dx0aGraphics2Dg2d = (Graphics2D) g;x0dx0aBasicStroke bstroke = new BasicStroke(2.0f);x0dx0ag2d.setStroke(bstroke);x0dx0ag2d.setColor(Color.green);x0dx0ag2d.drawOval(0, 0, 100, 100);x0dx0abstroke = new BasicStroke(5.0f);x0dx0ag2d.setStroke(bstroke);x0dx0ag2d.drawLine(50, 0, 50, 5);x0dx0ag2d.drawLine(0, 50, 5, 50);x0dx0ag2d.drawLine(50, 95, 50, 98);x0dx0ag2d.drawLine(95, 50, 98, 50);x0dx0adouble hourAngle = 2 * Math.PI * (seconds - 3 * 60 * 60)x0dx0a/ (12 * 60 * 60);x0dx0adouble minuteAngle = 2 * Math.PI * (seconds - 15 * 60) / (60 * 60);x0dx0adouble secondAngle = 2 * Math.PI * (seconds - 15) / (60);x0dx0abstroke = new BasicStroke(5.0f);x0dx0ag2d.setStroke(bstroke);x0dx0ag2d.drawLine(50, 50, 50 + (int) (30 * Math.cos(hourAngle)),x0dx0a50 + (int) (30 * Math.sin(hourAngle)));x0dx0abstroke = new BasicStroke(3.0f);x0dx0ag2d.setStroke(bstroke);x0dx0ag2d.drawLine(50, 50, 50 + (int) (40 * Math.cos(minuteAngle)),x0dx0a50 + (int) (40 * Math.sin(minuteAngle)));x0dx0abstroke = new BasicStroke(1.0f);x0dx0ag2d.setStroke(bstroke);x0dx0ag2d.drawLine(50, 50, 50 + (int) (45 * Math.cos(secondAngle)),x0dx0a50 + (int) (45 * Math.sin(secondAngle)));x0dx0ag2d.setColor(Color.red);x0dx0ag2d.drawString(city, 35, 115);x0dx0a}x0dx0ax0dx0apublic void timeElapsed() {x0dx0a//new Date()()獲得當前時間x0dx0aSystem.out.println(new Date()());x0dx0acalendar.setTime(new Date()());x0dx0aseconds = calendar.get(Calendar.HOUR) * 60 * 60x0dx0a+ calendar.get(Calendar.MINUTE) * 60x0dx0a+ calendar.get(Calendar.SECOND);x0dx0a}x0dx0ax0dx0apublic void run() {x0dx0atry {x0dx0awhile (true) {x0dx0aThread.sleep(300);x0dx0atimeElapsed();x0dx0arepaint();x0dx0a}x0dx0a} catch (InterruptedException e) {x0dx0a}x0dx0a}x0dx0a}