Java time zone conversion

Source: Internet
Author: User
Tags dateformat

Import java. util .*;
Import java. text. SimpleDateFormat;
Import java. text. DateFormat;
Import static java. lang. System. out;
Public class CalendarTest
{
Public static void main (String [] args)
{
/*
* Get the current calendar time
*/
Calendar now = Calendar. getInstance ();
/**
* Obtain the number of milliseconds between the current time zone and the year of the calendar month.
*/
Long srcTime = now. getTimeInMillis ();
/**
* TimeZone. getDefault (). getRawOffset () is the current default time zone (GMT + 8) and GMT
* Original time zone deviation (milliseconds)
* The following formula is the Greenwich mean time corresponding to the current time zone (converted to the time corresponding to GMT)
*/
Long targetTime = srcTime-TimeZone.getDefault (). getRawOffset ();
/**
* Create Date
*/
Date newDate = new Date (targetTime );
/**
* Format the date
*/
DateFormat df = new SimpleDateFormat ("MM/dd/yyyy HH: mm: ss ");
// Df. setTimeZone (TimeZone. getTimeZone ("GMT "));
String str = df. format (newDate );
Out. println ("newDate:" + str );
}
}
This article is from the "Zuo Luo CTO" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.