【问题标题】:parsing date and time in java在java中解析日期和时间
【发布时间】:2011-03-09 12:33:37
【问题描述】:

我的时间格式为 (yyyy-mm-ddTHH:mm+0:0000)..我需要使用 java 程序将此格式转换为 mm-dd-yyyy hh:mm am/pm ...任何一位帮助我要这样做..thanx提前...

【问题讨论】:

  • 所以你有一个String,你想把它转换成另一个字符串?
  • 你能解释更多吗?字符串从何而来? XML?它要去哪里?摆动图形用户界面?

标签: java parsing datetime


【解决方案1】:

试试这样的:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm+s:SSSS");
Date date = format.parse(dateString);
format = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
dateString = format.format(date);

【讨论】:

  • 在第二种格式中,它采用当前(本地)时区。如果您不想要默认的,请提供自己的。
【解决方案2】:

这是现代答案。 SimpleDateFormat 在 2011 年被问到这个问题时是正确的答案,但现在已经不是了。原来的旧课程很麻烦,所以他们的替代品终于在 2014 年初问世了。

我对您格式中的一些细节感到有些困惑。

  • yyyy-mm-ddTHH:mm+0:0000 看起来有点像 ISO 8601 偏移日期时间,但 +0:0000 应该是与 UTC 的偏移量。我推测它可能是作为h:mmss 格式的偏移量,但它是非标准的,我在其他地方从未见过。
  • 您似乎要求使用小写的 am/pm。这也是非标准的,并且没有内置在标准类中。

我建议:

    DateTimeFormatter sourceFormat
            = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm'+0:0000'");
    DateTimeFormatter targetFormat
            = DateTimeFormatter.ofPattern("MM-dd-uuuu hh:mm a", Locale.ENGLISH);

    String sourceTime = "2017-07-05T20:31+0:0000";
    String convertedTime = LocalDateTime.parse(sourceTime, sourceFormat)
            .format(targetFormat)
            .toLowerCase(Locale.ENGLISH);

    System.out.println(convertedTime);

打印出来

07-05-2017 08:31 pm

我发现在输入中要求文字 +0:0000 是最安全的,而不是试图解释它。为了以小写形式生成pm,我在格式化后将整个字符串转换为小写。 in this answer有一个不同的技巧。

尝试更灵活地处理偏移量中的小时、分钟和秒并理解它们可能是:

    sourceTime = sourceTime.replaceFirst("(\\d:\\d{2})(\\d{2})$", "0$1:$2");
    String convertedTime = OffsetDateTime.parse(sourceTime)
            .format(targetFormat)
            .toLowerCase(Locale.ENGLISH);

replaceFirst0:0000 转换为 00:00:00,如果前面有 +-,则符合 ISO 8601 偏移量。这也适用于其他偏移量(只要小时是一位数;您可能想依次尝试一位数和两位数)。在具体代码中,我们仍然在格式化时忽略偏移量,但如果需要,它可以用于其他目的。

DateTimeFormatterLocalDateTimeOffsetDateTime 是 JSR-310 的一部分。它们是在 Java 8 及更高版本中构建的。您也可以通过ThreeTen Backport 在 Java 6 和 7 中使用它们。还有一个特定版本的 Android 反向移植,ThreeTenABP

【讨论】:

    【解决方案3】:

    查看java.text.SimpleDateFormat - 您需要两个实例,一个用于解析,一个用于格式化。

    【讨论】:

      【解决方案4】:

      使用此代码 sn-p 查看工作示例:

      import java.text.DateFormat;
      import java.text.ParseException;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      public class DateTimeAMPM {
          public static void main(String[] args) throws ParseException {
      
      DateFormat inputDateFormatter = new SimpleDateFormat(
          "yyyy-MM-dd'T'HH:mm+s:SSSS");
      Date date = inputDateFormatter.parse("2011-11-11T22:33+0:400");
      String outputDateFormatter = "MM-dd-yyyy HH:mm:ss a";
      SimpleDateFormat sdf = new SimpleDateFormat(outputDateFormatter);
      System.out.println("Date: " + sdf.format(date));
      }
      

      }

      【讨论】:

        【解决方案5】:

        您可以按照 Paulo 和 morja 所说的去做,或者只使用 String.substring(),因为您的解析要求非常简单。

        【讨论】:

          【解决方案6】:

          您应该从您的字符串创建数据对象 -> 使用适当的类(例如 SimpleDataFormat),然后使用另一种格式打印它。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-08-03
            • 1970-01-01
            • 1970-01-01
            • 2015-06-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多