代码之家  ›  专栏  ›  技术社区  ›  Sudip Sadhukhan

如何在android中转换不同时区的时间字符串格式

  •  0
  • Sudip Sadhukhan  · 技术社区  · 7 年前

    我有一次性字符串格式 yyyy-MM-dd'T'HH:mm:ss.SSSSSS 时区是“GMT+05:30”。我需要把这个格式转换成 yyyy-MM-dd'T'HH:mm:ss.SSS'Z' 不同时区格式-“GMT0:00”。我写了下面的函数来转换不同时区的时间字符串。它工作得很好,但不能改变时间。

    public static String getUTCDate(String dateString) {
        String oldDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
        String newDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    
        String result = "";
        SimpleDateFormat simpleDateFormatOld;
        SimpleDateFormat simpleDateFormatNew;
    
        try {
            simpleDateFormatOld = new SimpleDateFormat(oldDateFormat,Locale.US);
            simpleDateFormatNew = new SimpleDateFormat(newDateFormat,Locale.US);
            simpleDateFormatNew.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
            result = simpleDateFormatNew.format(simpleDateFormatOld.parse(dateString));
        }
        catch(Exception e) {
            ExceptionHandler.handleException(e);
        }
        return result;
    }
    

    例子 :我通过了 2019-07-11T21:28:02.8469576 日期时间字符串。但作为回报我得到了 2019-07-11T21:28:02.846Z 不更改时间的日期时间字符串。

    0 回复  |  直到 7 年前
        1
  •  1
  •   Anonymous    7 年前

    试试这个:

      public static String getUTCDate(String dateString) {
        String oldDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS";
        String newDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    
        String result = "";
        SimpleDateFormat simpleDateFormatOld;
        SimpleDateFormat simpleDateFormatNew;
    
        try {
            simpleDateFormatOld = new SimpleDateFormat();
            simpleDateFormatOld.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
            simpleDateFormatOld.applyPattern(oldDateFormat);
            simpleDateFormatNew = new SimpleDateFormat(newDateFormat, Locale.US);
            result = simpleDateFormatNew.format(simpleDateFormatOld.parse(dateString));
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    

    simpleDataFormatOld 对象,然后对其应用日期模式。

        2
  •  1
  •   Anonymous    7 年前

    Instant 类是正确使用的。当程序接受字符串输入时,解析并将其转换为 即时 第一件事就是这样。只有在需要提供字符串输出时,才将时间格式化回字符串并传递出去。

    解析和转换输入

        ZoneId originalZone = ZoneId.of("Asia/Kolkata");
    
        Instant time = LocalDateTime.parse("2019-07-11T21:28:02.8469576")
                .atZone(originalZone)
                .toInstant();
    
        System.out.println(time);
    

    在大多数情况下,不要将时区作为格林尼治标准时间的裸偏移量。命名时区可以更好地向读者解释为什么选择这个时区,并且在偏移量发生变化(这种情况发生的频率比您想象的要频繁)时更能证明这一点。我利用了你的字符串是ISO8601格式这一事实。在这种情况下,我们不需要提供显式格式化程序。顺便说一句,你的示例字符串在秒上有7个小数,你的 oldDateFormat 好像要6块。从那以后这里就不重要了 LocalDateTime.parse 接受0到9位小数。

    您要求的输出是ISO8601的不同变体。上面的输出很像,因为它也是ISO8601,只是小数太多了。所以这次我们应用一个显式格式:

        ZoneOffset newOffset = ZoneOffset.UTC;
        DateTimeFormatter newFormatter = new DateTimeFormatterBuilder()
                .append(DateTimeFormatter.ISO_LOCAL_DATE)
                .appendLiteral('T')
                .appendPattern("HH:mm:ss.SSSX")
                .toFormatter();
    
        String formattedUtcDateTime = time.atOffset(newOffset).format(newFormatter);
        System.out.println(formattedUtcDateTime);
    

    2019-07-11T15:58:02.846Z

    我建议不要使用SimpleDateFormat和时区

    你试图使用的日期时间类, SimpleDateFormat TimeZone 那个 日期格式

    问题:我能用吗java.time.时间在Android上?

    对,java.time.时间在旧的和新的Android设备上都能很好地工作。只是至少需要 爪哇6

    • 在Java6和7中,获取ThreeTen Backport,即现代类的Backport(ThreeTen用于jsr310;请参阅底部的链接)。
    • 在(旧版)Android上,使用Android版的ThreeTen Backport。它叫ThreeTenABP。并确保从中导入日期和时间类 org.threeten.bp 有分装。

    链接

        3
  •  0
  •   Rajat Mehra    7 年前

    编辑:尝试此方法:

        public static String getUTCDate(String dateString) {
        Log.d(TAG,"input : "+dateString);
        String oldDateFormat = "yyyy-MM-dd'T'HH:mm:ss";
        String newDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    
        String result = "";
        SimpleDateFormat simpleDateFormatOld;
        SimpleDateFormat simpleDateFormatNew;
    
        try {
            simpleDateFormatOld = new SimpleDateFormat(oldDateFormat, Locale.US);
            simpleDateFormatNew = new SimpleDateFormat(newDateFormat,Locale.US);
            simpleDateFormatOld.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
            simpleDateFormatNew.setTimeZone(TimeZone.getTimeZone("GMT0:00"));
            result = simpleDateFormatNew.format(simpleDateFormatOld.parse(dateString));
        }
        catch(Exception e) {
            ExceptionHandler.handleException(e);
        }
        return result;
    }
    

    有两个变化: 1同时为新格式设置时区。

    simpleDateFormatNew.setTimeZone(TimeZone.getTimeZone("GMT0:00"));
    
    1. 从oldDateFormat中删除毫秒模式。

      String oldDateFormat=“yyyy-MM-dd'T'HH:MM:ss”;

        4
  •  0
  •   Sudip Sadhukhan    7 年前

    我通过添加时区来解决这个问题 oldDateFormat 也。

    simpleDateFormatOld.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
    simpleDateFormatNew.setTimeZone(TimeZone.getTimeZone("GMT+0:00"));
    
    推荐文章