【问题标题】:how to convert string date in UTC format using joda date time如何使用 joda 日期时间将字符串日期转换为 UTC 格式
【发布时间】:2020-07-21 18:54:20
【问题描述】:
 public static String convertInDateTimeSecondTOJodaTime(String dateTime) {
        try {
            DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
            DateTime date = formatter.parseDateTime(dateTime).withZoneRetainFields(DateTimeZone.UTC);
            return date.toString("h:mm aa");
        } catch (Exception e) {
            return null;
        }
    }

main(){
print(convertInDateTimeSecondTOJodaTime("2020-04-09T07:31:16Z"))
}

我正在尝试使用 joda 日期时间将给定的日期时间转换为 UTC 格式,它给出了错误的时间,它是在一小时前给出的,请帮助我做错了什么。

所需的结果是伦敦时间,因此在本例中为上午 8:31。

【问题讨论】:

  • 永远不要将Z 硬编码为格式模式字符串中的文字。这是一个偏移量,因此需要被划桨。这可能是您错误时间的原因之一。
  • 没关系,但是当我尝试转换当前日期时间时,我也得到了错误的 UTC 时间我比我早一个小时,你能建议如何以 UTC 获取当前时间跨度>

标签: java android jodatime


【解决方案1】:
import java.util.Date;
import java.util.TimeZone;
import java.text.SimpleDateFormat;

public class CurrentUtcDate {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
        dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        System.out.println("UTC Time is: " + dateFormat.format(date));
    }
}

Output
UTC Time is: 22-01-2018 13:14:35

您可以在这里查看https://www.quora.com/How-do-I-get-the-current-UTC-date-using-Java

【讨论】:

  • 但我想要的是 am h:mm aa 格式,所以我弄错了
  • 在 22-01-2018 13:14:35 这样获得时间后,您可以使用 SimpleDateFormat("hh:mm aa") stackoverflow.com/questions/1154903/… 重新格式化它
  • 它给出错误我的日期时间是 2020-04-09T07:31:16Z 而在输出 22-01-2018 13:14:35 它是错误的
  • 你能帮我使用joda日期时间吗,因为有一些API级别折旧的简单日期
  • 没有人要求DateSimpleDateFormat。而且由于它们设计不佳且早已过时,因此我们不应该推荐它们。
【解决方案2】:

首先,不要在程序中将日期和时间作为字符串处理。将它们作为适当的日期时间对象处理。因此,除了最简单的一次性程序之外,您不应该希望有一种方法可以将 UTC 字符串转换为不同格式的伦敦时间字符串。

所以当你接受字符串输入时,解析成DateTime对象:

    String stringInput = "2020-04-09T07:31:16Z";
    DateTime dt = DateTime.parse(stringInput);
    System.out.println("Date-time is: " + dt);

目前的输出是:

日期时间为:2020-04-09T07:31:16.000Z

我正在利用您的字符串是 ISO 8601 格式的事实,这是 Joda-Time 的默认格式,因此我们不需要显式格式化程序来解析它。

直到您需要提供字符串输出,将您的日期和时间转换为所需的区域并格式化为所需的字符串:

    DateTimeZone zone = DateTimeZone.forID("Europe/London");
    DateTime outputDateTime = dt.withZone(zone);
    String output = outputDateTime.toString("h:mm aa");
    System.out.println("Output is: " + output);

输出是:上午 8:31

你的代码出了什么问题

  1. Z 格式模式字符串中的单引号是错误的。输入字符串中的 Z 与 UTC 的偏移量为 0,需要将其解析为偏移量,否则您得到的结果不正确。永远不要在Z 周围加上这些引号。
  2. withZoneRetainFields() 是用于在时区之间转换的错误方法。方法名称意味着日期和时间保持不变,只是时区发生了变化,这通常会导致不同的时间点。

发生的事情是您的字符串被解析为 2020-04-09T07:31:16.000+01:00,这与 06:31:16 UTC 的时间点相同,所以错误。接下来,您将时区替换为 UTC,保持时间为 07:31:16。然后将这个时间格式化并打印出来。

考虑 java.time

正如 Fabien 所说,Joda-Time 后来被现代 Java 日期和时间 API 的 java.time 取代。 Joda-Time 主页说:

请注意,Joda-Time 被认为是一个基本“完成”的项目。 没有计划进行重大改进。如果使用 Java SE 8,请迁移 到java.time (JSR-310)。

链接

【讨论】:

    【解决方案3】:

    由于您需要使用 Joda DateTime,您需要使用 Joda 的格式化程序。 您正在返回带有模式“h:mm aa”的日期,所以我假设您需要从日期中提取时间。

    下面的代码应该可以工作:

    import java.util.Locale;
    
    import org.joda.time.DateTime;
    import org.joda.time.format.DateTimeFormat;
    import org.joda.time.format.DateTimeFormatter;
    
    public class MyDateCoonverter {
    
        public static void main(String a[]) {
    
            System.out.println(convertInDateTimeSecondTOJodaTime("2020-04-09T07:31:16Z"));
        }
    
        public static String convertInDateTimeSecondTOJodaTime(String dateTime) {
                DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
                DateTime dt = formatter.parseDateTime(dateTime);
                return dt.toString("h:mm aa", Locale.ENGLISH);
        }
    }
    

    输出如下: 上午 7 点 31 分

    如果您不想使用任何第三方库并且仍想仅从日期中提取时间,则可以使用 Java 的 LocalTime。

    【讨论】:

    • 在 dt.toString("h:mm aa", Locale.ENGLISH) 中提供 Locale.ENGLISH 是可选的
    • 但它是错误的,因为如果您将在线检查与伦敦时间进行比较,它将在使用代码显示前一小时显示 8.30 我不知道我在哪里做错了
    • 要获得 UTC 时区的时间,请使用 withZone(DateTimeZone.UTC) 例如DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC);
    【解决方案4】:

    如果您使用的是 Java 8 或更新版本,则不应使用 java.util.Date(已弃用)或 Joda Time(由带有 java.time 包的 Java 8 的新 DATE API 代替):

    public static void main(String[] args) {
            String date = "2020-04-09T07:31:16Z";
            String formatedDate = ZonedDateTime.parse(date).format(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
            System.out.println(formatedDate); //print "7:31 AM"
        }
    }
    

    【讨论】:

    • 但是早上 7.31 是错误的,应该是 8.31 你能用当前日期试试看它的对错
    • 如果你想在格式化之前将UTC时间转换为当地时间,你可以使用:String formatedDate = ZonedDateTime.parse(date).withZoneSameInstant(Clock.systemDefaultZone().getZone()).format( DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2015-10-24
    • 2011-06-13
    相关资源
    最近更新 更多