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"));