【问题标题】:issue with date and time zone how to get date without time zone?日期和时区问题如何获取没有时区的日期?
【发布时间】:2014-02-26 20:17:31
【问题描述】:

我尝试将 Tics 中的日期转换为 UTC 日期时间格式 - 'YYYY-MM-DDThh:mm:ss'

public static DateFormat getFormat() {
    String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$ 
    Locale locale = new Locale(systemLocale);
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
    return dateFormat;
}

public static Object getFormatedValue(Object value) {
        String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.parseLong(updated));
        return getFormat().format(new Date(calendar.getTimeInMillis()));
}

public static Object getOdataValue(Object value) throws ParseException {

    DateFormat dateFormat = getFormat();

    Date parse = dateFormat.parse(value.toString());
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(parse.getTime());

    return "\"/Date(" + calendar.getTimeInMillis() + ")/\""; //$NON-NLS-1$ //$NON-NLS-2$

}

例如,我得到 UTC 的 dtae 时间结果的问题 -

1393358368000 = 2014 年 2 月 25 日星期二 19:59:28 GMT

您的时区:2014 年 2 月 25 日晚上 9:59:28 GMT+2

此代码的结果是 2/25/2014 21:59:28 PM

如何在没有时区的情况下获得结果?在这种情况下,我希望结果是 2014 年 2 月 25 日 19:59:28 GMT 我可以在有和没有 UTC 的情况下打勾并得到不同的结果吗?

【问题讨论】:

  • “我可以打勾并在有和没有 UTC 的情况下得到不同的结果吗?”是什么意思? - 不太清楚你在问什么。
  • 查看我的示例 1393358368000 = 2014 年 2 月 25 日星期二 19:59:28 GMT 在我的代码中,结果是 2014 年 2 月 25 日星期二 21:59:28 GMT。我怎样才能得到结果将没有 GMT+2 含义,2014 年 2 月 25 日星期二 19:59:28
  • 您的代码的结果根本不是这样 - 它是 "/Date(1393358368000)/" 形式的字符串 - 您在哪里看到 GMT+2 的值?请注意,您根本不需要创建 Calendar - 只需调用 parse.getTime() 即可获得相同的毫秒值。
  • getOdataValue = /Date(1393358368000)/` 但 getFormatedValue 将是 2014 年 2 月 25 日星期二 21:59:28
  • 啊,对。不清楚你的意思。我现在将添加一个答案。

标签: java


【解决方案1】:

您的要求并不完全清楚,但如果您只想让您的 DateFormat 使用 UTC,那很容易:

public static DateFormat getFormat() {
    String systemLocale = System.getProperty("user.language"); //$NON-NLS-1$ 
    Locale locale = new Locale(systemLocale);
    DateFormat dateFormat = DateFormat.getDateTimeInstance(
        DateFormat.MEDIUM, DateFormat.MEDIUM, locale);
    dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC"));
    return dateFormat;
}

(顺便说一句,您有什么理由不使用Locale.getDefault() 作为默认语言环境?还是让DateFormat 自己选择它?)

另外请注意,您创建日历完全没有任何理由。您的getFormattedValuegetOdataValue 方法可以简化为:

public static Object getFormattedValue(Object value) {
    String updated = (strValue).replaceAll(pattern, "$1"); //$NON-NLS-1$
    long millis = Long.parseLong(updated);
    return getFormat().format(new Date(millis));
}

public static Object getOdataValue(Object value) throws ParseException {
    DateFormat dateFormat = getFormat();
    Date parsedDate = dateFormat.parse(value.toString());
    return "\"/Date(" + date.getTime() + ")/\""; //$NON-NLS-1$ //$NON-NLS-2$
}

【讨论】:

  • dateFormat.setTimeZone(TimeZone.getTimeZone("Etc/UTC") 是否会从 dateFormat 中删除时区?
  • @user1365697:不,它会将时区设置为 UTC。时区出现的事实只是您请求的“中等格式”的一部分 - 它是特定于语言环境的。 (该格式没有为我显示时区...您是否确定您正在查看getFormattedValue 的结果,而不是具有默认toString() 表示的Date 对象?)
  • 它们有什么不同?
  • 数据库保存数据没有时区,然后进行转换的问题。我用时区显示日期时间
  • @user1365697:恐怕我真的根本没有关注你。如果这不能回答您的问题,我建议您花点时间让您的问题更加更清楚。请参阅tinyurl.com/so-hints 并真正花时间把事情弄清楚。我们不知道您是如何从数据库中获取数据的,您所说的“没有时区”是什么意思(您是存储 UTC 值还是本地值?您是如何存储它的?)或者您如何显示日期,或者你想在哪个时区显示它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-21
  • 2016-12-14
  • 2015-05-24
  • 1970-01-01
  • 2019-12-21
相关资源
最近更新 更多