【问题标题】:Java Date FormatterJava 日期格式化程序
【发布时间】:2011-04-07 12:24:47
【问题描述】:

我将日期格式设置为 "YYYY-mm-dd hh:mm" 作为格式化程序对象。

如何格式化输入格式化程序对象以仅获取"YYYY-mm-dd";?

【问题讨论】:

  • 您应该接受以下答案之一作为您问题的解决方案,或者如果您仍然没有答案,请更新您的问题。
  • 对于该问题的新读者,我建议您不要使用SimpleDateFormat,也不要使用Date。这些类设计不良且过时,尤其是前者,尤其是出了名的麻烦。而是使用来自java.time, the modern Java date and time API 的LocalDateTime 和DateTimeFormatter。

标签: java simpledateformat


【解决方案1】:

我得到的日期格式为 "YYYY-mm-dd hh:mm" 作为格式化程序 目的。如何格式化输入 仅获取格式化程序对象 "YYYY-mm-dd";

您不能将日期设置为 YYYY-mm-dd,它应该是 yyyy-MM-dd .要在 yyyy-MM-dd 中获取日期,代码如下:

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(todaysDate);

【讨论】:

  • 请注意SimpleDateFormat 不是线程安全的,并且是常见的错误来源。请改用DateTimeFormatter
【解决方案2】:
Format formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        Date date;
        try {
            date = (Date)((DateFormat) formatter).parse("2011-04-13 05:00");
            formatter = new SimpleDateFormat("yyyy-MM-dd");
            String s = formatter.format(date);
            System.out.println(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }

【讨论】:

    【解决方案3】:

    使用SimpleDateFormat

    String myDateString = "2009-04-22 15:51";
    
    SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
    
    System.out.println(outFormat.format(inFormat.parse(myDateString)));
    

    【讨论】:

      【解决方案4】:

      如果您要获取格式为"YYYY-mm-dd hh:mm" 的日期并且您希望它为"YYYY-mm-dd",我建议您只使用inputDate.substring(0, 10)。

      无论哪种方式,都要小心潜在的Y10k bugs :)

      【讨论】:

        【解决方案5】:

        在 Java 中按照 yyyy-MM-dd 的示例甲酸盐日期

        Format formatter = new SimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();
        System.out.println("Now: "+formatter.format(now.getTime()) );
        

        【讨论】:

          【解决方案6】:
          【解决方案7】:

          SimpleDateFormat 是你要找的。​​p>

          【讨论】:

            【解决方案8】:

            试试这个:

            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = formatter.format(todaysDate);
            

            【讨论】:

              【解决方案9】:

              使用此代码:

              Date date=new Date();
              
              SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
              
              String formattedDate = formatter.format(date);
              
              System.out.println("formatted time==>" + formattedDate);
              
              SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
              

              【讨论】:

                【解决方案10】:

                one by user2663609 等其他答案是正确的。

                作为替代方案,java.util.Date/Calendar 类的第三方开源替代品Joda-Time 包含满足您需求的内置格式。

                // © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
                // import org.joda.time.*;
                // import org.joda.time.format.*;
                
                String stringIn = "2011-04-07";
                
                // Returns a formatter for a full date as four digit year, two digit month of year, and two digit day of month (yyyy-MM-dd).
                DateTimeFormatter formatter = ISODateTimeFormat.date().withZone( DateTimeZone.forID( "Europe/London" ) ).withLocale( Locale.UK );
                DateTime dateTime = formatter.parseDateTime( stringIn ).withTimeAtStartOfDay();
                String stringOut = formatter.print( dateTime );
                

                转储到控制台...

                System.out.println( "dateTime: " + dateTime.toString() );
                System.out.println( "stringOut: " + stringOut );
                

                运行时……

                dateTime: 2011-04-07T00:00:00.000+01:00
                stringOut: 2011-04-07
                

                【讨论】:

                  【解决方案11】:

                  这个问题有很多好答案! ,这里又来了一个更通用的解决方案

                  public static String getDateInFormate(String oldFormate , String newFormate , String dateToParse){
                      //old "yyyy-MM-dd hh:mm"
                      //new yyyy-MM-dd
                      //dateTopars 2011-04-13 05:00  
                      String formatedDate="";
                      Format formatter = new SimpleDateFormat();
                      Date date;
                      try {
                          date = (Date)((DateFormat) formatter).parse(dateToParse);
                          formatter = new SimpleDateFormat(newFormate);
                          formatedDate = formatter.format(date);
                      } catch (ParseException e) {
                          e.printStackTrace();
                      }
                      return formatedDate;    
                  } 
                  

                  【讨论】:

                  • 这个问题有很多不好的答案。关于他们所有人都支持SimpleDateFormat,这是一个臭名昭著的麻烦制造者,我们在 2011 年提出这个问题时不得不使用它,但现在应该没有人再使用了。
                  【解决方案12】:
                   SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
                                  String strDate = entry_date;
                                  System.out.println("strDate*************"+strDate);
                                  Date date = null;
                                  try {
                                      date = sdf.parse(strDate);
                  
                                  } catch (ParseException e1) {
                                      // TODO Auto-generated catch block
                                      e1.printStackTrace();
                                  }
                  
                                  DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                  
                                  Date yesterday =subtractDay( date);
                                  String requiredDate = df.format(yesterday);
                                  System.out.println("110 days before*******************"+requiredDate);
                  
                  public static Date subtractDay(Date date) {
                  
                          Calendar cal = Calendar.getInstance();
                          cal.setTime(date);
                          cal.add(Calendar.DATE, -110);`enter code here`
                          return cal.getTime();
                      }
                  

                  【讨论】:

                    【解决方案13】:

                    java.time

                    我建议您使用现代 Java 日期和时间 API java.time 进行日期和时间工作。

                    为解析输入定义一个格式化程序:

                    private static final DateTimeFormatter FORMATTER
                            = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm", Locale.ROOT);
                    

                    解析:

                        String input = "2019-01-21 23:45";
                        LocalDateTime dateTime = LocalDateTime.parse(input, FORMATTER);
                        System.out.println(dateTime);
                    

                    目前的输出:

                    2019-01-21T23:45

                    格式化输出:

                        String output = dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE);
                        System.out.println(output);
                    

                    2019-01-21

                    教程链接

                    Trail: Date Time (The Java™ Tutorials) 解释如何使用 java.time。

                    【讨论】:

                      猜你喜欢
                      • 2015-06-17
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2019-02-19
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多