解析另一个TimeZone中的格式化日期字符串到日期对象不起作用 [英] Parsing formatted Date String in another TimeZone to date object not working

查看:64
本文介绍了解析另一个TimeZone中的格式化日期字符串到日期对象不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将格式化的日期字符串转换为Date对象。日期字符串格式化为某个其他时区。

I am trying to convert a formatted date String to Date object. Date String is formatted to some other timezone.

当我执行 sdf.parse(String)时,它会返回我的系统日期对象。

When I do sdf.parse(String) it returns me my System date object.

代码如下,

static Date convertGMTTime(String timeZone, long longDate){
    Date convertedTime = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try{
        Date date = new Date(longDate);
        System.out.println("timezone: "+timeZone +", timestamp: "+date);
        Locale locale = Locale.ENGLISH;
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
/*          DateFormat formatter = DateFormat.getDateTimeInstance(
                    DateFormat.DEFAULT,
                    DateFormat.DEFAULT,
                    locale);
        formatter.setTimeZone(destTimeZone);*/
        sdf.setTimeZone(destTimeZone);
        String convertedDateStr = sdf.format(date);
        System.out.println("convertedDateStr: "+convertedDateStr);
        convertedTime = sdf.parse(convertedDateStr);
        System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
    }catch(Exception e){
        e.printStackTrace();
    }
    return convertedTime;
}

如果有人能提供帮助并指出我哪里出错,我将不胜感激。
提前致谢。

I would appreciate if anyone could help and point out where I am going wrong. Thanks in advance.

输出:


时区:大西洋/ Cape_Verde,时间戳:Tue Jun 26 17:38:11 IST 2012

源时区:sun.util.calendar.ZoneInfo [id =Atlantic / Cape_Verde,偏移= -3600000,dstSavings = 0, useDaylight = false,transitions = 6,lastRule = null]

timezone: Atlantic/Cape_Verde, timestamp: Tue Jun 26 17:38:11 IST 2012
Source timezone: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

convertedDateStr:2012-06-26 11:08:11

convertedDateStr: 2012-06-26 11:08:11

convertedTime:Tue Jun 26 17:38:11 IST 2012

sdf:sun.util.calendar.ZoneInfo [id =Atlantic / Cape_Verde,offset = -3600000,dstSavings = 0, useDaylight = false,transitions = 6,lastRule = null]

convertedTime: Tue Jun 26 17:38:11 IST 2012
sdf:sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]






更多细节分享,当我使用另一个sdf对象(没有为它设置时区)时,它会返回正确的时间和日期,但仍然从系统时钟中选择时区


Some more details to share, When I use another sdf object(without setting timezone for it), It do return me correct time and date but still timezone is picked from System clock

代码

static Date convertGMTTime(String timeZone, long longDate){
    Date convertedTime = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdfParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try{
        Date date = new Date(longDate);
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
        sdf.setTimeZone(destTimeZone);
        String convertedDateStr = sdf.format(date);
        System.out.println("convertedDateStr: "+convertedDateStr );
        convertedTime = sdfParse.parse(convertedDateStr,new ParsePosition(0));
        System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
    }catch(Exception e){
        e.printStackTrace();
    }
    return convertedTime;
}

输出

Source timezone: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

convertedDateStr: 2012-06-26 12:24:56

convertedTime: Tue Jun 26 12:24:56 IST 2012 

sdf: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

据我所知,当我没有为sdf分配时区时,它需要系统时区,但为什么不在系统时区显示时间?我在时区中显示它,因为它在字符串中,但时区是不同的。

I understand that when I do not assign timezone to sdf it takes System time zone, but why doesn't it show time in System time zone? I shows it in timezone as it was in String but Timezone is different.

当我设置时区时,它根据我的系统时间返回日期对象,而不管sdf是否设置了其他时区。

Ans when I set timezone it returns date object as per my system time irrespective of the fact that sdf has some other time zone set.

任何人都可以解释sdf.parse和sdf.format的功能行为。

Can anyone please explain the functional behavior for sdf.parse and sdf.format.

对于我来说,sdf.setTimeZone()在我们使用格式时会产生影响,当我们使用sdf.parse()时它会无效。我觉得很奇怪。

For me sdf.setTimeZone() does have its impact when we use format and it is nullified when we use sdf.parse(). I find it quite strange.

在这方面感谢帮助。

推荐答案

您已经有一个日期(或日期的毫秒数),因此无需转换。日期没有任何时区。这是一个普遍的时刻。仅当您显示此日期时,时区才相关,因为日期65647678000在某个时区可能是12:38,而在某个其他时区可能是10:38。当你解析日期的字符串表示时,它也是相关的,因为10:38在某个时区是65647678000,而在其他时区是65657678000。

You already have a Date (or the number of milliseconds of the Date), so there is nothing to convert. A Date doesn't have any time zone. It's a universal instant in time. The time zone is relevant only when you display this date, because the date 65647678000 could be 12:38 in some time zone, but 10:38 in some other time zone. It's also relevant when you parse the String representation of a Date, because 10:38 is 65647678000 in some time zone, but is 65657678000 in some other.

虽然您没有显示Date对象,或者将字符串解析为日期,但您无需关心时区。要选择显示/解析时使用的时区,请设置 DateFormat 的时区,然后使用 DateFormat.format() / DateFormat.parse()格式化/解析日期。

While you don't display a Date object, or parse a String to a Date, you don't need to care about time zones. And to choose the time zone used when displaying/parsing it, set the time zone of the DateFormat, and then use DateFormat.format()/DateFormat.parse() to format/parse the date.

当你使用 Date.toString()要显示日期,它将始终使用您当前的时区。

When you use Date.toString() to display a date, it will always use your current time zone.

我觉得它更容易通过不考虑日期作为一天,一个月,一年,一小时等来理解我的意思,但是作为一个时刻:当肯尼迪被枪杀时。 当肯尼迪被枪杀时对每个人来说都是同一时刻。但如果你代表在达拉斯时区肯尼迪被枪杀的那一刻,这与你在巴黎时区代表同一时刻所获得的结果不一样。

I find it easier to understand what I mean by not thinking of a Date as a day, a month, a year, an hour, etc., but as a moment: "when Kennedy was shot". "When Kennedy was shot" is the same moment for everyone. But if you represent the moment "when Kennedy was shot" in Dallas time zone, it's not the same result as the result you get when you represent this same moment in Paris time zone.

这篇关于解析另一个TimeZone中的格式化日期字符串到日期对象不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆