小编Aru*_*unK的帖子

java中将日期从一个时区转换为另一时区

我想使用 java 中的 SimpleDateFormat 类将日期从一个时区转换为另一个时区。但不知何故,它产生了不同的结果,这些结果应该位于同一时区。

这是一个测试用例,它生成一个结果为 IST,另一个结果为 GMT。我认为它应该只为这两种情况生成 GMT。

public class TestOneCoreJava {

    public static void main(String[] args) throws ParseException {// Asia/Calcutta
        DateFormat formatter = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a");
        System.out.println(getDateStringToShow(formatter.parse("26-Nov-10 03:31:20 PM +0530"),"Asia/Calcutta", "Europe/Dublin", false));
        System.out.println(getDateStringToShow(formatter.parse("02-Oct-10 10:00:00 AM +0530"),"Asia/Calcutta", "Europe/Dublin", false));
        //------Output--
        //26-Nov-10 GMT
        //02-Oct-10 IST
    }

    public static String getDateStringToShow(Date date,
            String sourceTimeZoneId, String targetTimeZoneId, boolean includeTime) {
        String result = null;

        // System.out.println("CHANGING TIMEZONE:1 "+UnitedLexConstants.SIMPLE_FORMAT.format(date));
        String date1 = new SimpleDateFormat("dd-MMM-yy hh:mm:ss a").format(date);

        SimpleDateFormat sourceTimeZoneFormat = new SimpleDateFormat("Z");
        sourceTimeZoneFormat.setTimeZone(TimeZone.getTimeZone(sourceTimeZoneId)); …
Run Code Online (Sandbox Code Playgroud)

java timezone simpledateformat

1
推荐指数
1
解决办法
5092
查看次数

标签 统计

java ×1

simpledateformat ×1

timezone ×1