带有dateStyle和timeStyle的Joda-Time Formatter

Jea*_*ond 3 java format datetime date jodatime

使用java.text.DateFormat,可以构建具有日期和时间样式的日期格式化程序:

DateFormat df = getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG);
system.out.println(df.format(new Date()));
Run Code Online (Sandbox Code Playgroud)

是否可以使用与Joda-Time类似的东西?

Jos*_*ter 5

标准格式内置于JodaTime

JodaTime中内置了许多标准的日期时间格式.

这是我使用的一个例子:

DateTime dateTime = DateTime.now(DateTimeZone.UTC);

String formattedDateTime = dateTime.toString(DateTimeFormat.fullDateTime());
Run Code Online (Sandbox Code Playgroud)

请注意DateTimeFormat.fullDateTime().

其他一些选择包括:

  • fullTime()
  • fullDate()
  • shortDateTime()
  • forStyle("MS") 表示日期和短时间.
  • forStyle("S-") 表示日期和时间.
  • ......等......

完整列表可在以下位置找到:

http://www.joda.org/joda-time/apidocs/org/joda/time/format/DateTimeFormat.html#forStyle(java.lang.String)

干杯,

J.P