我正在尝试将格式为“ yyyyMMddHHmmss”的日期字符串转换为另一个时区“ America / Sao_Paulo”的日期字符串,同时考虑到America / Sao_Paulo的DST(从2016年10月16日开始)。
我正在使用TimeZone类作为
TimeZone代表时区偏移量,还可以计算出夏令时。
import java.util.TimeZone;
import java.text.*;
import java.util.Date;;
public class TimeZoneConversion {
public static void main(String[] args)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
sdf.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
try
{
String inputdate = "20161016052355";
Date t = sdf.parse(inputdate);
System.out.println(t);
}
catch(ParseException e)
{
}
}
}
上面的输出显示在IST中
Sun Oct 16 12:53:55 IST 2016
如何将输入为“ yyyyMMddHHmmss”的时间字符串转换为输出中带有DST的“ America / Sao_Paulo”时区中的时间字符串?
这对我有用
try
{
SimpleDateFormat f = new SimpleDateFormat("yyyyMMddhhmmss");
f.setTimeZone(TimeZone.getTimeZone("Asia/Calcutta"));
Date time = f.parse("20161015113634");
DateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
formatter.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
System.out.println(formatter.format(time));
}
catch (ParseException e){
}
输入:20161015113634(时间不在夏令时中)
输出:20161015030634(圣保罗的转换时间)
输入:20161016113634(夏令时间)
输出:20161016040634(圣保罗转换为夏令时的时间)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句