java代码转时区 java时间格式转换代码

关于JAVA时间格式转换问题,涉及时区//java代码转时区我实现一个时钟窗口程序给java代码转时区你java代码转时区了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);}// 重写父类java代码转时区的方法绘制时钟图形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 * 60calendar.get(Calendar.MINUTE) * 60calendar.get(Calendar.SECOND);}public void run() {try {while (true) {Thread.sleep(300);timeElapsed();repaint();}} catch (InterruptedException e) {}} }
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时间转换,带时区的java代码转时区我假设了你的已知时间类型为Calendarjava代码转时区,如果不是你也可以自己改成Date类型java代码转时区,代码如下java代码转时区:
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) {
//假如这个是你已知的时间类型
Calendar cal = Calendar.getInstance();
cal.getTimeInMillis();
//北京时区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);
}
}
Java如何根据TimeZone转换时间,可以给出相关的例子吗?谢谢通过TimeZonejava代码转时区的getTimeZone方法来实现java代码转时区,具体可参考下面代码例子java代码转时区:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
publicclass CalendarTime {
publicstaticfinal String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
privatestatic TimeZone USER_TIMEZONE = TimeZone.getTimeZone("PRC");
privatestatic TimeZone GMT_TIMEZONE = TimeZone.getTimeZone("GMT");
publicstaticvoid main(String[] args) throws ParseException {
System.out.println("start test---------------------");
// construct a string to indicate a local time
String testTime = new String("2008-02-02 08:30:33");
System.out.println("convert a local string to a local gmt time");
System.out.println(testTime);
System.out.println("convert to GMT time");
// convert the string to a gmt calendar
Calendar gmtCal = CalendarTime
.convertLocalDateStringToGMTCalendar(testTime);
System.out.println(getDateFormat(GMT_TIMEZONE).format(gmtCal.getTime())
" "gmtCal.getTimeZone().getID());
Calendar localCal = convertGMTToCNTime(gmtCal);
System.out.println(localCal.getTime());
System.out.println("convert a gmt calendar to a local string");
Calendar localTime = Calendar.getInstance();
localTime.set(2008, 01, 02, 0, 30, 33);
System.out.println(localTime.getTime());
Calendar gmtTime = convertCNTimeToGMT(localTime);
System.out.println(getDateFormat(GMT_TIMEZONE)
.format(gmtTime.getTime())
" "gmtTime.getTimeZone().getID());
String localStr = convertToLocalTimeString(gmtTime);
System.out.println(localStr);
}
/**
*getadateformatforanytimezone
*
*@paramtimeZone
*@return
*/
publicstatic SimpleDateFormat getDateFormat(TimeZone timeZone) {
SimpleDateFormat formater = new SimpleDateFormat(DATE_PATTERN);
formater.setTimeZone(timeZone);
return formater;
}
/**
*convertacalendartoanylocaltimestring
*
*@paramcalendar
*@return
*/
publicstatic String convertToLocalTimeString(Calendar calendar) {
if (null == calendar) {
returnnull;
}
return getDateFormat(USER_TIMEZONE).format(calendar.getTime());
}
/**
*convertanydatestringtoagmtcalendar
*
*@paramtime
*@return
*@throwsParseException
*/
publicstatic Calendar convertLocalDateStringToGMTCalendar(String time)
throws ParseException {
Date date = getDateFormat(USER_TIMEZONE).parse(time.trim());
Calendar calendar = new GregorianCalendar(GMT_TIMEZONE);
calendar.setTime(date);
return calendar;
}
/**
*convertagmtcalendartoalocalcalendarjustmodifyitstimezonebut
*willnotchangeitstime
*
*@paramtime
*@return
*/
publicstatic Calendar convertGMTToCNTime(Calendar time) {
time.setTimeZone(GMT_TIMEZONE);
Calendar cnTime = Calendar.getInstance();
int begin_year = time.get(Calendar.YEAR);
int begin_month = time.get(Calendar.MONTH);
int begin_day = time.get(Calendar.DAY_OF_MONTH);
int begin_hour = time.get(Calendar.HOUR_OF_DAY);
int begin_minute = time.get(Calendar.MINUTE);
int begin_second = time.get(Calendar.SECOND);
cnTime.set(Calendar.YEAR, begin_year);
cnTime.set(Calendar.MONTH, begin_month);
cnTime.set(Calendar.DAY_OF_MONTH, begin_day);
cnTime.set(Calendar.HOUR_OF_DAY, begin_hour);
cnTime.set(Calendar.MINUTE, begin_minute);
cnTime.set(Calendar.SECOND, begin_second);
return cnTime;
}
/**
*convertalocalcalendartoagmtcalendaronlychangeitstimezonebut
*willnotchangeitstime
*
*@paramcnTime
*@return
*/
publicstatic Calendar convertCNTimeToGMT(Calendar cnTime) {
Calendar result = new GregorianCalendar(GMT_TIMEZONE);
int year = cnTime.get(Calendar.YEAR);
int month = cnTime.get(Calendar.MONTH);
int day = cnTime.get(Calendar.DAY_OF_MONTH);
int hour = cnTime.get(Calendar.HOUR_OF_DAY);
int minute = cnTime.get(Calendar.MINUTE);
int second = cnTime.get(Calendar.SECOND);
result.set(Calendar.YEAR, year);
result.set(Calendar.MONTH, month);
result.set(Calendar.DAY_OF_MONTH, day);
result.set(Calendar.HOUR_OF_DAY, hour);
【java代码转时区 java时间格式转换代码】result.set(Calendar.MINUTE, minute);
result.set(Calendar.SECOND, second);
return result;
}
}

start test---------------------
convert a local string to a local gmt time
2008-02-02 08:30:33
convert to GMT time
2008-02-02 00:30:33 GMT
Sat Feb 02 00:30:33 GMT 08:00 2008
convert a gmt calendar to a local string
Sat Feb 02 00:30:33 GMT 08:00 2008
2008-02-02 00:30:33 GMT
2008-02-02 08:30:33
关于java代码转时区和java时间格式转换代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读