【问题标题】:Date converter in JAVA [duplicate]JAVA中的日期转换器[重复]
【发布时间】:2013-03-08 16:59:46
【问题描述】:

我有Tue Mar 19 00:41:00 GMT 2013 这样的日期,如何将其转换为2013-03-19 06:13:00

final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
final Date date = bdate; 
Date ndate = formatter.parse(formatter.format(date)); 
System.out.println(ndate);

给出相同的日期。

【问题讨论】:

  • 你有没有尝试过?
  • 您首先在 StackOverflow 上搜索有关 Java 中日期转换的第 100 万篇文章。
  • @evening:那么为什么不解决这个问题,以便时机成熟呢?您在这里寻求人们的帮助 - 至少您能做的就是确保您提出一个明智的问题。
  • @JonSkeet:好的,请再检查一遍
  • @evening:好的,这样更好。现在你为什么期望格式为“yyyy-MM-dd HH:mm:ss”的格式化程序能够解析“Tue Mar 19 00:41:00 GMT 2013”​​的值?

标签: java date


【解决方案1】:

使用两个具有适当格式的 SimpleDateFormat 对象,并使用第一个将字符串解析为日期,第二个将日期再次格式化为字符串。

【讨论】:

    【解决方案2】:

    正如第一个答案所说。首先使用 SimpleDateFormat 解析您的日期,如下所示:

    Date from = new SimpleDateFormat("E M d hh:mm:ss z yyyy").parse("Tue Mar 19 00:41:00 GMT 2013");
    

    然后使用它用另一个 SimpleDateFormat 实例来格式化生成的日期对象,如下所示:

    String to = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(from);
    

    参见 SimpleDateFormat here 的 javadoc。希望对您有所帮助。

    【讨论】:

    • 只剩下一件事,如何将字符串转换为日期?谢谢
    • String to date 是第一行。只需在 SimpleDateFormat 上使用 parse。
    • 请再次检查线程,谢谢 veyr muich
    【解决方案3】:

    其他人遗漏的一件主要事情是处理时区 (TZ)。每当您使用 SimpleDateFormat 来往/从日期的字符串表示时,您确实需要了解您正在处理的 TZ。除非您在 SimpleDateFormat 上明确设置 TZ,否则它将在格式化/解析时使用 default TZ。除非您只处理默认时区中的日期字符串,否则您会遇到问题。

    您输入的日期代表 GMT 日期。假设您还希望将输出格式化为 GMT,则需要确保在 SimpleDateFormat 上设置 TZ:

    public static void main(String[] args) throws Exception
    {
        String inputDate = "Tue Mar 19 00:41:00 GMT 2013";
        // Initialize with format of input
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
        // Configure the TZ on the date formatter. Not sure why it doesn't get set
        // automatically when parsing the date since the input includes the TZ name,
        // but it doesn't. One of many reasons to use Joda instead
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
        Date date = sdf.parse(inputDate);
        // re-initialize the pattern with format of desired output. Alternatively,
        // you could use a new SimpleDateFormat instance as long as you set the TZ
        // correctly
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
    }
    

    【讨论】:

      【解决方案4】:

      这样使用SimpleDateFormat

      final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      final Date date = new Date();
      System.out.println(formatter.format(date));
      

      【讨论】:

      • 如何将 formatter.format(date) 转换为日期?
      • @evening: 用 formatter.parse(String),给它一个相同格式的字符串。
      • final DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");最终日期日期 = bdate;日期 ndate = formatter.parse(formatter.format(date)); System.out.println(ndate);
      • @evening:是的,它们是逆运算。
      • @evening: Date 类将日期存储为长值,但是当您打印它时,它会以可读格式显示,即:“Tue Mar 19 00:41:00 GMT 2013” .在您决定将其显示给用户之前,您无需对该格式执行任何操作。在那一刻需要将该日期格式化为您希望显示它的方式,并且您使用 SimpleDateFormat 执行此操作,它以您指定的格式返回一个字符串,在您的情况下为“2013-03-19 06:13:00 "(使用张贴的模式)。
      【解决方案5】:

      如果您对日期进行任何计算或解析,请使用 JodaTime,因为标准的 JAVA 日期支持确实有问题

      【讨论】:

      • 越野车?是一个糟糕的实现,但我不知道那些错误。
      • 是的,在进行日期/时间计算时,JodaTime 或任何 JSR 310 实现是必须的。
      • @stenrs:据我所知,JSR310 仍在开发中,它将取代 JAVA 8 中的数据 API。Stephen Colebourne 创建了 JodaTime 并正在开发 JSR310
      • @emd:还有,就是这个:github.com/ThreeTen/threeten
      • @stenrs - 你是对的
      猜你喜欢
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 2014-07-18
      相关资源
      最近更新 更多