import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.SimpleTimeZone;
import java.util.TimeZone;

import javax.swing.text.html.HTMLDocument.Iterator;

public class Test2 {

protected static Format format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

/**
* timeZoneOffset表示时区,如中国一般使用东八区,因此timeZoneOffset就是8
* @param timeZoneOffset
* @return
*/
public String getFormatedDateString(int timeZoneOffset){
if (timeZoneOffset > 13 || timeZoneOffset < -12) {
timeZoneOffset = 0;
}
TimeZone timeZone;
String[] ids = TimeZone.getAvailableIDs(timeZoneOffset * 60 * 60 * 1000);
if (ids.length == 0) {
// if no ids were returned, something is wrong. use default TimeZone
timeZone = TimeZone.getDefault();
} else {
timeZone = new SimpleTimeZone(timeZoneOffset * 60 * 60 * 1000, ids[0]);
}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(timeZone);
return sdf.format(new Date());
}

public static String getFormatedDateString(String _timeZone) throws Exception{
SimpleDateFormat s=new SimpleDateFormat("yyyy/MM/dd HH:mm");
Date ddd=s.parse("2014/6/13 4:00");
TimeZone timeZone = null;
if("".equals(_timeZone)){
timeZone = TimeZone.getDefault();
}else{
timeZone = TimeZone.getTimeZone(_timeZone);
}

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
sdf.setTimeZone(timeZone);
//TimeZone.setDefault(timeZone);
return sdf.format(ddd);
}

public static void setCNTimeZone(){
final TimeZone zone = TimeZone.getTimeZone("GMT+8");
TimeZone.setDefault(zone);
}

public static void main(String args[]) throws Exception{
System.out.println(getFormatedDateString(""));
System.out.println(getFormatedDateString("Asia/Shanghai"));
System.out.println(getFormatedDateString("Asia/Seoul"));
System.out.println(getFormatedDateString("Europe/Madrid"));
System.out.println(getFormatedDateString("GMT+1:00"));
System.out.println(TimeZone.getDefault().getID());
}

}

Anddoi 将时间转换为指定时区的时间的更多相关文章

  1. 【转帖】linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York

    linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York 2015-02-10 10:58:22 youcha ...

  2. centos7开启ntp并同步时间到指定时区

    前提:近期公司都是使用的直接对外的云服务器,在登上服务器后用date命令查看新服务器的时间,发现并不是标准时间,于是需要做时间同步.我这里讲的是能连接外网的情况下,在服务器不多的情况下是否此方法,大型 ...

  3. 根据本地/服务器时间获取指定时区时间 new Date指定时区时间

    1.代码 function getTimeByTimeZone(timeZone){ var d=new Date(); localTime = d.getTime(), localOffset=d. ...

  4. python 通过pytz模块进行时区的转换,获取指定时区的时间

    import pytz import time import datetime print(pytz.country_timezones('cn')) # 查询中国所拥有的时区 print(pytz. ...

  5. python代码中指定时区获取时间方法

    os.environ['TZ'] = 'Asia/Shanghai' os.environ['TZ'] = 'Europe/London' hour_cur = time.strftime('%H')

  6. abp允许跨域代码,时间转换为固定格式,本地时间

     在Global的 Application_BeginRequest方法中: Thread.CurrentThread.CurrentCulture = new CultureInfo("z ...

  7. Python时间获取详解,Django获取时间详解,模板中获取时间详解(navie时间和aware时间)

    1.Python获取到的时间 import pytz from datetime import datetime now = datetime.now() # 这个时间为navie时间(自己不知道自己 ...

  8. 转自文翼的博客:将本地时间转换为 GMT 时间

    在写 RSS 订阅接口的时候,发现最终输出文章的 RSS 时间(GMT时间),在本地上显示的时间和在服务器上显示的时间不一致. 原因是时区不一致,那么在 JavaScript 中,如何将时间转换为统一 ...

  9. 利用SimpleDateFormat进行时间的跨时区转换 - Java

    * 次方法主要用来将特定时区的时间转换成指定时区的时间,比如将北京时间“2018-04-08 15:40:49.031”,转换对应的美国东部时间是“2018-04-08 03:40:49.031”   ...

随机推荐

  1. Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)

    结果: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->opti ...

  2. A Tour of Go Methods with pointer receivers

    Methods can be associated with a named type or a pointer to a named type. We just saw two Abs method ...

  3. Java SAX DefaultHandler

    The org.xml.sax.helpers.DefaultHandler class is the base class for "listeners" in SAX 2.0. ...

  4. hdoj 1402 Prepared for New Acmer【快速幂】

    Prepared for New Acmer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. IAR 1.3 for STM8 ST-Link无法调试 无法仿真 the debugging session could not be started

    IAR 1.3 for STM8 ST-Link无法调试 the debugging session could not be started CPU型号是:STM8F103F3 首先要用ST Vis ...

  6. dotnetfx35.exe

    http://download.microsoft.com/download/2/0/e/20e90413-712f-438c-988e-fdaa79a8ac3d/dotnetfx35.exe

  7. Javascript call与apply记录

    [注]:记录自己对javascript中call与apply的见解 总会有些东西会被人拿出来重复的写来写去,为何? 只是因为自己感觉不够了解,所谓好记性不如烂笔头,并且在写的同时也会或多或少的收获到一 ...

  8. Struts2 Action的访问路径

    1.     Action的访问路径 扩展名 缺省以.action结尾,请参考:default.properties文件,可以通过配置改变这一点: <constant name="st ...

  9. N个数字中随机取m个数子,不重复

    <script> function rand_nums(min,max,count){ var arr = []; for(var i=min;i<max;i++){ arr.pus ...

  10. java_TreeSet 定制排序实例

    package ming; import java.util.Comparator; import java.util.TreeSet; class M { int age; public M(int ...