java時區轉換
Ⅰ 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 時區轉換
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時間 yyyy-MM-dd'T'HH:mm:ss.SSSZ 中的T是什麼意思
T的含義是表示時間的開始;
Java看起來設計得很像C++,但是為了使語言小和容易熟悉,設計者們把C++語言中許多可用的特徵去掉了,這些特徵是一般程序員很少使用的。例如,Java不支持go to語句,代之以提供break和continue語句以及異常處理。
Java還剔除了C++的操作符過載(overload)和多繼承特徵,免去了預處理程序。因為Java沒有結構,數組和串都是對象,所以不需要指針。Java能夠自動處理對象的引用和間接引用,實現自動的無用單元收集,使用戶不必為存儲管理問題煩惱,能更多的時間和精力花在研發上。
面向對象
Java是一個面向對象的語言。對程序員來說,這意味著要注意應中的數據和操縱數據的方法(method),而不是嚴格地用過程來思考。在一個面向對象的系統中,類(class)是數據和操作數據的方法的集合。
數據和方法一起描述對象(object)的狀態和行為。每一對象是其狀態和行為的封裝。類是按一定體系和層次安排的,使得子類可以從超類繼承行為。在這個類層次體系中有一個根類,它是具有一般行為的類。Java程序是用類來組織的。
Java還包括一個類的擴展集合,分別組成各種程序包(Package),用戶可以在自己的程序中使用。例如,Java提供產生圖形用戶介面部件的類(java.awt包),這里awt是抽象窗口工具集(abstract windowing toolkit)的縮寫,處理輸入輸出的類(java.io包)和支持網路功能的類(java.net包)。
Ⅳ 關於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 utc轉本地時間
java utc轉本地時間的方法:
1、創建一個格式化時間對象simpleDateFormat,並初始化格式yyyy-MM-dd HH:mm:ss:
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
2、創建時區對象utcZone,獲取utc所在的時區
TimeZone utcZone = TimeZone.getTimeZone("UTC");
3、設置utc時區,為轉換做准備
simpleDateFormat.setTimeZone(utcZone);
4、獲取本地時間,並轉換
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));