【问题标题】:Java convert Time object (GMT) to String (GMT+1)Java 将时间对象 (GMT) 转换为字符串 (GMT+1)
【发布时间】:2016-04-07 23:03:20
【问题描述】:

我尝试了以下代码,它应该允许我将时间传递作为参数转换为一个多小时 (GMT+1) 的字符串。
但我总是得到与我的对象时间相同的时间

public static String getFormattedTimeLabel(final Time time) {
    Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(time.getTime());
    SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
    sdf.setCalendar(cal);
    sdf.setTimeZone(TimeZone.getDefault());
    return sdf.format(cal.getTime());
}    

有人知道怎么解决吗?

编辑:好的,最后我在控制器中创建了以下函数。
由于 HttpServletRequest,我以简单日期格式设置的时区是客户端恢复的时区。
代码后可以看到 System.out 的打印结果。

private Time getTimeLocal(Time requestTime) {
     Calendar cal = new GregorianCalendar();
     cal.setTimeInMillis(requestTime.getTime());
     SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
     sdf.setCalendar(cal);
     System.out.println(requestTime);
     sdf.setTimeZone(tz);
     System.out.println(tz.getID());
     String dt = sdf.format(cal.getTime());
     Date date = null;
    try {
        date = sdf.parse(dt);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(dt);
     return new Time(date.getTime());
}

10:00:00 //the Time object
Europe/Paris  // The id of the client timezone (GMT+1 for me)
10 : 00 //the date. the time printed is not correct it should be set with the timezone of the client so for me GMT+1 so 11:00:00

【问题讨论】:

  • @BoristheSpider TimeZone.getDefault() 应该是系统的时区。而且我不使用我想要的那个,因为它可能会根据使用应用程序的国家/地区而有所不同
  • @Spidi'sWeb:问题不在于增加小时数,而在于设置时区和格式。
  • @BoristheSpider 我从数据库中获得的时间,我没有打印它,但通常如果我用 Timezone.getDefault() 格式化它,我应该有这个时间 + 1 小时因为我在巴黎。问题是最后没有添加小时
  • @JeromeCampeaux 我明白了——数据库让事情变得更加复杂。数据如何存储在数据库中,数据库设置为哪个时区? JDBC 连接到数据库的时区是什么?我问是因为 JDBC 有进行自动转换的讨厌习惯...我建议您在收到数据时打印数据,看看 JDBC 对您造成了什么样的影响。

标签: java time timezone simpledateformat


【解决方案1】:

代码似乎可以正常工作。输出结果时,时间显示为正确时区的时间。尝试添加时区参数如下:

public static String getFormattedTimeLabel(final Time time, String tzId) {
  Calendar cal = new GregorianCalendar();
  cal.setTimeInMillis(time.getTime());
  SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
  sdf.setCalendar(cal);
  sdf.setTimeZone(tzId == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tzId));
  return sdf.format(cal.getTime());
}

并将其调用为:

Time time = new Time(System.currentTimeMillis());
System.out.println(getFormattedTimeLabel(time, null));
System.out.println(getFormattedTimeLabel(time, "UTC"));

给出以下结果(截至目前):

12 : 08
11 : 08

由于我在 TZ GMT+1,现在已经过了中午,结果是正确的。

顺便说一句,像这样打印时间对象:

System.out.println(time.toString());

给予:

12:08:16

即它在默认时区默认格式化。

顺便说一句,该函数可以简化为不使用日历对象,如下所示:

public static String getFormattedTimeLabel(final Time time, String tzId) {
  SimpleDateFormat sdf = new SimpleDateFormat("HH : mm");
  sdf.setTimeZone(tzId == null ? TimeZone.getDefault() : TimeZone.getTimeZone(tzId));
  return sdf.format(time);
}

在 SimpleDateFormat 中设置时区就足够了。

【讨论】:

    【解决方案2】:

    好的,感谢Date TimeZone conversion in java? 这篇帖子,我终于找到了解决方案,所有这些人的帮助都参与了我的帖子。

    我的最终代码是:

    private Time getTimeLocal(Time requestTime) {
        String ret = Utils.getFormattedTimeLabel(requestTime);
        SimpleDateFormat sdfgmt = new SimpleDateFormat("HH : mm");
        sdfgmt.setTimeZone(TimeZone.getTimeZone("GMT"));
    
        SimpleDateFormat sdfmad = new SimpleDateFormat("HH : mm");
        sdfmad.setTimeZone(TimeZone.getTimeZone(tz.getID()));
    
        Date inptdate = null;
        try {
            inptdate = sdfgmt.parse(ret);
        } catch (ParseException e) {e.printStackTrace();}
    
        String localString = sdfmad.format(inptdate);
        try {
            inptdate = sdfmad.parse(localString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Time(inptdate.getTime());
    }
    

    【讨论】:

    • Time 据我所知,自格林威治标准时间以来的毫秒数 - 它不能成为“本地”。这段代码有点奇怪,因为您所做的实际上是从一个自格林威治标准时间以来应始终为毫秒的值中添加或减去您的时区偏移量......
    猜你喜欢
    • 2012-05-30
    • 2019-11-25
    • 1970-01-01
    • 2019-01-02
    • 2016-11-06
    • 2011-10-07
    • 2014-02-23
    • 2013-01-02
    • 2015-12-19
    相关资源
    最近更新 更多