【问题标题】:Convert String Date to String date different format [duplicate]将字符串日期转换为字符串日期不同格式[重复]
【发布时间】:2013-02-21 10:09:15
【问题描述】:

我是 Java 新手。 Postgres db 包含日期格式为yyyy-MM-dd。我需要转换为dd-MM-yyyy

我试过了,但是显示错误

   public static void main(String[] args) throws ParseException {

    String strDate = "2013-02-21";
      DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
      Date da = (Date)formatter.parse(strDate);
      System.out.println("==Date is ==" + da);
      String strDateTime = formatter.format(da);

      System.out.println("==String date is : " + strDateTime);
}

【问题讨论】:

  • 你得到什么输出?
  • 在日期对象上应用日期格式,而不是在字符串 strDate 上。它应该可以正常工作

标签: java simpledateformat date-conversion


【解决方案1】:
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
Date date = format1.parse("2013-02-21");
System.out.println(format2.format(date));

【讨论】:

  • 谢谢!我建议命名 prevDateFormat & newDateFormat。
【解决方案2】:

您需要使用两个DateFormat 实例。一个包含输入字符串的格式,另一个包含输出字符串的所需格式。

public static void main(String[] args) throws ParseException {

    String strDate = "2013-02-21";

    DateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd");
    Date da = (Date)inputFormatter.parse(strDate);
    System.out.println("==Date is ==" + da);

    DateFormat outputFormatter = new SimpleDateFormat("dd-MM-yyyy");
    String strDateTime = outputFormatter.format(da);
    System.out.println("==String date is : " + strDateTime);
}

【讨论】:

    【解决方案3】:

    参考这些格式Java Date Format Docs:

    试试这个代码:

    String myDate= "2013-02-21";
    DateFormat iFormatter = new SimpleDateFormat("yyyy-MM-dd");
    DateFormat oFormatter = new SimpleDateFormat("dd-MM-yyyy");
    String strDateTime = oFormatter.format(iFormatter.parse(myDate));
    

    【讨论】:

      【解决方案4】:

      您需要一种格式来解析当前状态和一种格式来生成所需的输出。

      String strDate  = "2013-02-21";
      DateFormat to   = new SimpleDateFormat("dd-MM-yyyy"); // wanted format
      DateFormat from = new SimpleDateFormat("yyyy-MM-dd"); // current format
      System.out.println(to.format(from.parse(strDate)));
      

      【讨论】:

        【解决方案5】:

        试试下面的实用函数。

        // convert String date to another string date
        public String convertStringDateToAnotherStringDate(String stringdate, String stringdateformat, String returndateformat){        
        
                try {
                    Date date = new SimpleDateFormat(stringdateformat).parse(stringdate);
                    String returndate = new SimpleDateFormat(returndateformat).format(date);
                    return returndate;
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return "";
                }
        
        }
        
        // call function with required parameter arguments
        String resultDate = convertStringDateToAnotherStringDate("2017-06-22", "yyyy-MM-dd", "dd-MM-yyyy")
        System.out.println(resultDate);
        

        结果: 22-06-2017

        【讨论】:

          【解决方案6】:

          您使用相同的格式化程序来处理不同的格式。

          您需要提供两个格式化程序new SimpleDateFormat("dd-MM-yyyy"); 用于将2013-02-21 转换为日期对象,并提供new SimpleDateFormat("dd-MM-yyyy"); 用于将日期对象格式化为字符串21-02-2013

          【讨论】:

            【解决方案7】:

            首先,您不应该从 Postgres 数据库中以 String 的形式获取您的日期。您应该将其作为表示日期的类型的对象来获取。

            第二,虽然在 2013 年使用 DateSimpleDateFormat 是司空见惯和合理的,但日子已经过去了,新的更好的日期和时间课程已经问世并被普遍使用。恕我直言,没有人应该再使用旧课程了,我认为它们早已过时。

            要从您的数据库中的ResultSet 获取LocalDate 对象:

                LocalDate da = yourResultSet.getObject(3, LocalDate.class);
            

            (我假设日期在查询的第 3 列中)。

            将其格式化为dd-MM-yyyy

                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu");
                String strDateTime = da.format(formatter);
                System.out.println("==String date is : " + strDateTime);
            

            这会给出类似的输出

            ==String date is : 21-02-2013
            

            最后,如果您有问题中的字符串并希望将其重新格式化为您想要的格式:

                String strDate = "2013-02-21";      
                LocalDate da = LocalDate.parse(strDate);
                System.out.println("==Date is ==" + da);
            

            打印出来

            ==Date is ==2013-02-21
            

            我正在利用字符串匹配现代日期和时间类的默认 ISO 8601 标准日期格式这一事实。因此,在这种情况下,我们不需要像解析其他格式那样使用显式格式化程序来解析。

            格式化成您想要的格式和以前一样。

            旧类问题的一个例子

            在我的电脑上,问题中代码的输出是

            ==Date is ==Tue Aug 06 00:00:00 CET 26
            ==String date is : 06-08-0026
            

            您显然得到了错误的结果,并且您不知道出了什么问题。 错误是您正在使用您打算用于字符串dd-MM-yyyy的格式化程序解析您的数据库日期字符串2013-02-21。所以它被解析为公元 2013 年 2 月 21 日。二月不是2013天吗?使用默认设置的 SimpleDateFormat 没问题,它只是继续计算接下来的几个月和几年的天数,并在五年半后的 8 月 6 日结束,但仍处于历史早期。

            如果我们尝试对现代类进行类似操作:

                LocalDate.parse(strDate, formatter);
            

            ——我们得到java.time.format.DateTimeParseException: Text '2013-02-21' could not be parsed at index 2。这是因为格式化程序指示的是两位数的日期,所以当解析两位数后输入字符串中有更多位时,这是一个错误,并因此报告。我认为这种行为更正确、更有帮助。

            【讨论】:

              【解决方案8】:

              您需要使用SimpleDateFormat 实例并传递您喜欢的日期模式

              当前模式:yyyy-MM-dd

              需要的模式:dd-MM-yyyy

              现在尝试以下操作。它产生所需的日期格式模式。

              public class App {
                  public static void main(String[] args) {
                      SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                      SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy");
                      Date date = null;
                      try {
                          date = format1.parse("2013-02-21");
                          System.out.println(format1.format(date)); //current format: 2013-02-21
                      } catch (ParseException e) {
                          e.printStackTrace();
                      }
                      System.out.println(format2.format(date)); //the format that is needed : 21-02-2013
                  }
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-12-28
                • 1970-01-01
                • 1970-01-01
                • 2021-07-27
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多