【问题标题】:How to get UTC time without SimpleDateFormat? [duplicate]如何在没有 SimpleDateFormat 的情况下获取 UTC 时间? [复制]
【发布时间】:2014-06-13 08:31:27
【问题描述】:

我目前正在处理从 UTC 转换为 UTC 的时间戳。我发现的所有文章都是基于与 String 的转换。 Like this one:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date myDate = simpleDateFormat.parse(rawQuestion.getString("AskDateTime"));

但我想知道是否有一种方法可以简单地使用 Date 实例/类或日历将本地 Date 转换为 UTC,反之亦然,而无需将其转换为 String。

【问题讨论】:

标签: java datetime utc


【解决方案1】:

阅读Joda-Time。对于此类事情,这比 java 日期和日历类更好的 API

【讨论】:

【解决方案2】:

也许这可以帮助你:

Calendar.getInstance(java.util.TimeZone)

Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));

【讨论】:

  • 但是如何使用两个日历实例(UTC 和本地)在 Date 之间进行转换?
  • 看看 3er answ。伟大的。 stackoverflow.com/questions/308683/…
  • 那篇文章使用了 SimpleDateTimeFormat,它实际上是从 Date 到 String 的转换,反之亦然。我只是不明白为什么日期/时间转换是通过将日期转换为字符串然后再次返回日期来完成的。
  • 好吧,我感觉自己像一只大猴子,我想说第四篇文章。唯一没有使用 simpledateformat 的。我希望你向我道歉。
  • 当我还在使用 Java 7 时,它也在使用 Java 8。无论如何,第二个答案是一个很好的答案,因为它使用了偏移量。
【解决方案3】:

java.until.Date 没有时区,因此无需转换。当您将日期显式格式化为字符串时,您只会看到一个时区,或者使用其 toString 方法隐式格式化。隐式转换使用本地默认时区。

在内部,Date 将日期/时间存储为 long,表示自 UTC 时间 1970 年 1 月 1 日午夜以来的毫秒数。

因此,如果您将日期格式化为字符串,然后将字符串解析回日期,那么您什么都没有改变。

【讨论】:

    【解决方案4】:

    到目前为止,我还没有找到完美的解决方案,所以我不得不坚持从 Date 到 String 的转换,反之亦然。这是我写的一个小助手类。

    public class DateTimeHelper {
    
        public static final String MYSQL_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
        private static final TimeZone timeZoneUTC = TimeZone.getTimeZone("UTC");
    
        private Date date = new Date();
        private final SimpleDateFormat format;
    
        public DateTimeHelper(String dateTimeFormat) {
            format = new SimpleDateFormat(dateTimeFormat, Locale.US);
        }
    
        public DateTimeHelper(String dateTimeFormat, String utcTimeString) {
            this(dateTimeFormat);
    
            try {
                format.setTimeZone(timeZoneUTC);
                Date utc = format.parse(utcTimeString);
                format.setTimeZone(TimeZone.getDefault());
                String local = format.format(utc);
                date = format.parse(local);
            } catch (ParseException e) {
                // nothing
            }
        }
    
        public Date getDate() {
            return date;
        }
    
        public Date toUtc() {
    
            String temp = toString();
            format.setTimeZone(timeZoneUTC);
            try {
                return format.parse(temp);
            } catch (ParseException e) {
                return date;
            }
        }
    
        @Override
        public String toString() {
            format.setTimeZone(TimeZone.getDefault());
            return format.format(date);
        }
    
        public String toUtcString() {
            format.setTimeZone(timeZoneUTC);
            return format.format(date);
        }
    }
    

    还有一个更容易使用的:

    public class MySqlDateTimeHelper extends DateTimeHelper {
    
        public MySqlDateTimeHelper() {
            super(DateTimeHelper.MYSQL_DATE_TIME_FORMAT);
        }
    
        public MySqlDateTimeHelper(String utcTimeString) {
            super(DateTimeHelper.MYSQL_DATE_TIME_FORMAT, utcTimeString);
        }
    
        public static String getCurrentTimestampUtc() {
            MySqlDateTimeHelper current = new MySqlDateTimeHelper();
            return current.toUtcString();
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2020-02-25
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多