如何在Java中更改日期格式?

ras*_*asi 51 java date

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

 dd/MM/yyyy  to yyyy/MM/dd
Run Code Online (Sandbox Code Playgroud)

Chr*_*ker 127

如何使用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);
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,麻烦的旧日期时间类,例如 [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html)、[` java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html) 和 `java.text.SimpleTextFormat` 现在是[旧版](https ://en.wikipedia.org/wiki/Legacy_system),由 [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html ) 类。请参阅 [Oracle 教程](https://docs.oracle.com/javase/tutorial/datetime/TOC.html)。 (2认同)

Kri*_*ols 24

SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
sdf.format(new Date());
Run Code Online (Sandbox Code Playgroud)

这应该可以解决问题


Bas*_*que 12

TL;博士

LocalDate.parse( 
    "23/01/2017" ,  
    DateTimeFormatter.ofPattern( "dd/MM/uuuu" , Locale.UK ) 
).format(
    DateTimeFormatter.ofPattern( "uuuu/MM/dd" , Locale.UK )
)
Run Code Online (Sandbox Code Playgroud)

2017年1月23日

避免遗留日期时间类

克里斯托弗帕克答案是正确但过时的.麻烦的旧日期,时间类,如java.util.Date,java.util.Calendarjava.text.SimpleTextFormat现在的遗产,由取代java.time类.

使用java.time

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

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 );
Run Code Online (Sandbox Code Playgroud)

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

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

2017年1月23日

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


关于java.time

java.time框架是建立在Java 8和更高版本.这些类取代麻烦的老传统日期时间类,如java.util.Date,Calendar,和SimpleDateFormat.

现在处于维护模式Joda-Time项目建议迁移到java.time类.

要了解更多信息,请参阅Oracle教程.并搜索Stack Overflow以获取许多示例和解释.规范是JSR 310.

从哪里获取java.time类?

ThreeTen-额外项目与其他类扩展java.time.该项目是未来可能添加到java.time的试验场.您可以在此比如找到一些有用的类Interval,YearWeek,YearQuarter,和更多.


乔达时间

UPDATE:乔达时间的项目现在处于维护模式,与团队的建议迁移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 );
Run Code Online (Sandbox Code Playgroud)

转储到控制台......

System.out.println( "localDate: " + localDate );
System.out.println( "newDateString: " + newDateString );
Run Code Online (Sandbox Code Playgroud)

跑的时候......

localDate: 2010-08-12
newDateString: 2010/08/12
Run Code Online (Sandbox Code Playgroud)


YoK*_*YoK 9

使用SimpleDateFormat

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

完整示例:

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));
    }
}
Run Code Online (Sandbox Code Playgroud)