【问题标题】:How can I change the date format in Java? [duplicate]如何更改 Java 中的日期格式? [复制]
【发布时间】:2010-08-12 15:47:44
【问题描述】:

我需要使用 Java 更改日期格式

 dd/MM/yyyy  to yyyy/MM/dd

【问题讨论】:

    标签: java date


    【解决方案1】:

    如何使用SimpleDateFormat从一种日期格式转换为另一种日期格式:

    final String OLD_FORMAT = "dd/MM/yyyy";
    final String NEW_FORMAT = "yyyy/MM/dd";
    
    // August 12, 2010
    String oldDateString = "12/08/2010";
    String newDateString;
    
    SimpleDateFormat sdf = new SimpleDateFormat(OLD_FORMAT);
    Date d = sdf.parse(oldDateString);
    sdf.applyPattern(NEW_FORMAT);
    newDateString = sdf.format(d);
    

    【讨论】:

    【解决方案2】:
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    sdf.format(new Date());
    

    这应该可以解决问题

    【讨论】:

    • 我有一个dd/MM/yyyy 格式的字符串,我该如何转换
    【解决方案3】:

    tl;博士

    LocalDate.parse( 
        "23/01/2017" ,  
        DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ) 
    ).format(
        DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK )
    )
    

    2017/01/23

    避免使用旧的日期时间类

    answer by Christopher Parker 正确但已过时。麻烦的旧日期时间类,例如 java.util.Datejava.util.Calendarjava.text.SimpleTextFormat,现在是 legacy,被 java.time 类所取代。

    使用 java.time

    将输入字符串解析为日期时间对象,然后生成所需格式的新字符串对象。

    LocalDate 类表示没有时间和时区的仅日期值。

    DateTimeFormatter fIn = DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK );  // As a habit, specify the desired/expected locale, though in this case the locale is irrelevant.
    LocalDate ld = LocalDate.parse( "23/01/2017" , fIn );
    

    为输出定义另一个格式化程序。

    DateTimeFormatter fOut = DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK );
    String output = ld.format( fOut );
    

    2017/01/23

    顺便说一句,请考虑对表示日期时间值的字符串使用标准的ISO 8601 格式。


    关于java.time

    java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧 legacy 日期时间类,例如 java.util.DateCalendarSimpleDateFormat

    Joda-Time 项目现在位于maintenance mode,建议迁移到java.time 类。

    要了解更多信息,请参阅Oracle Tutorial。并在 Stack Overflow 上搜索许多示例和解释。规格为JSR 310

    从哪里获得 java.time 类?

    ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是未来可能添加到 java.time 的试验场。您可以在这里找到一些有用的类,例如IntervalYearWeekYearQuartermore


    乔达时间

    更新:Joda-Time 项目现在位于maintenance mode,团队建议迁移到java.time 类。为了历史的缘故,这里留下这部分。

    为了好玩,这是他为使用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.*;
    
    final String OLD_FORMAT = "dd/MM/yyyy";
    final String NEW_FORMAT = "yyyy/MM/dd";
    
    // August 12, 2010
    String oldDateString = "12/08/2010";
    String newDateString;
    
    DateTimeFormatter formatterOld = DateTimeFormat.forPattern(OLD_FORMAT);
    DateTimeFormatter formatterNew = DateTimeFormat.forPattern(NEW_FORMAT);
    LocalDate localDate = formatterOld.parseLocalDate( oldDateString );
    newDateString = formatterNew.print( localDate );
    

    转储到控制台...

    System.out.println( "localDate: " + localDate );
    System.out.println( "newDateString: " + newDateString );
    

    运行时……

    localDate: 2010-08-12
    newDateString: 2010/08/12
    

    【讨论】:

      【解决方案4】:

      使用SimpleDateFormat

          String DATE_FORMAT = "yyyy/MM/dd";
          SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
          System.out.println("Formated Date " + sdf.format(date));
      

      完整示例:

      import java.text.SimpleDateFormat;
      import java.util.Date;
      
      public class JavaSimpleDateFormatExample {
          public static void main(String args[]) {
              // Create Date object.
              Date date = new Date();
              // Specify the desired date format
              String DATE_FORMAT = "yyyy/MM/dd";
              // Create object of SimpleDateFormat and pass the desired date format.
              SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
              /*
               * Use format method of SimpleDateFormat class to format the date.
               */
              System.out.println("Today is " + sdf.format(date));
          }
      }
      

      【讨论】:

      • 我可以传递一个字符串到日期吗??
      【解决方案5】:
      SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
      Date myDate = sdf.parse("28/12/2013");
      sdf.applyPattern("yyyy/MM/dd")
      String myDateString = sdf.format(myDate);
      

      现在 myDateString = 2013/12/28

      【讨论】:

        【解决方案6】:

        这只是 Christopher Parker 的 answer 适应于使用 Java 8 中新的1 类:

        final DateTimeFormatter OLD_FORMATTER = DateTimeFormatter.ofPattern("dd/MM/yyyy");
        final DateTimeFormatter NEW_FORMATTER = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        
        String oldString = "26/07/2017";
        LocalDate date = LocalDate.parse(oldString, OLD_FORMATTER);
        String newString = date.format(NEW_FORMATTER);
        

        1 好吧,不再那么新了,Java 9 应该很快就会发布。

        【讨论】:

          【解决方案7】:

          或者你可以走正则表达式路线:

          String date = "10/07/2010";
          String newDate = date.replaceAll("(\\d+)/(\\d+)/(\\d+)", "$3/$2/$1");
          System.out.println(newDate);
          

          它也适用于两种方式。当然,这实际上不会验证您的日期,并且也适用于“21432/32423/52352”之类的字符串。您可以使用"(\\d{2})/(\\d{2})/(\\d{4}" 来更准确地确定每个组中的位数,但它只能从 dd/MM/yyyy 到 yyyy/MM/dd 而不再适用(并且仍然接受无效数字那里像45)。如果你给它一些无效的东西,比如“blabla”,它只会返回同样的东西。

          【讨论】:

          • 现在你有两个问题。 ;-)
          • 感谢您如此清楚您的方法的局限性。
          【解决方案8】:

          更改日期格式的多种方法

          private final String dateTimeFormatPattern = "yyyy/MM/dd";
          private final Date now = new Date();  
          
          final DateFormat format = new SimpleDateFormat(dateTimeFormatPattern);  
          final String nowString = format.format(now);   
          
           final Instant instant = now.toInstant();  
           final DateTimeFormatter formatter =  
                DateTimeFormatter.ofPattern(  
                   dateTimeFormatPattern).withZone(ZoneId.systemDefault());  
           final String formattedInstance = formatter.format(instant);  
          
            /* Java 8 needed*/
            LocalDate date = LocalDate.now();
            String text = date.format(formatter);
            LocalDate parsedDate = LocalDate.parse(text, formatter);
          

          【讨论】:

            【解决方案9】:

            要更改日期格式,您需要两种格式,如下所示。

             String stringdate1 = "28/04/2010";  
            
             try {
            
                 SimpleDateFormat format1 = new SimpleDateFormat("dd/MM/yyyy");
                 Date date1 = format1.parse()
                 SimpleDateFormat format2 = new SimpleDateFormat("yyyy/MM/dd");
            
                 String stringdate2 = format2.format(date1);
                } catch (ParseException e) {
               e.printStackTrace();
            }
            

            这里stringdate2 的日期格式为yyyy/MM/dd。它包含2010/04/28

            【讨论】:

              【解决方案10】:
              SimpleDateFormat format1 = new SimpleDateFormat("yyyy/MM/dd");
              System.out.println(format1.format(date));
              

              【讨论】:

                猜你喜欢
                • 2021-01-11
                • 1970-01-01
                • 2015-07-25
                • 2023-03-27
                • 1970-01-01
                • 2014-04-27
                • 1970-01-01
                • 2016-05-10
                • 2020-01-27
                相关资源
                最近更新 更多