如何将 Joda 时间格式化为 android 中的 dd-mm-yyyy?

How to format Joda Time to dd-mm-yyyy in android?

 String DOB = new DateTime(Long.parseLong(dob) * 1000, DateTimeZone.UTC ).toString();
 // Current
 // YYYY-MM-DD
 // DOB = "1994-05-10T00:00.000Z"

 // Required 
 // DD-MM-YYYY
 // DOB = "10-05-1994"

我想删除 hh:mm:ss 并使用 Joda-Time DateTimeFormatter 格式化日期。

试试这个:

DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS zzz");
// pass your DOB String
DateTime jodatime = dtf.parseDateTime(DOB);
// Format for output
DateTimeFormatter dtfOut = DateTimeFormat.forPattern("dd-MM-yyyy");
// Print the date
System.out.println(dtfOut.print(jodatime));

试试这个:

String textDate ="1994-05-10T00:00.000Z"; //Date to convert

DateTimeFormatter DATE_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ"); //Default format

SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()); //Needed format

DateTime dateTime = new DateTime(DATE_FORMAT.parseDateTime(textDate),        DateTimeZone.forID(current.getID()));

Calendar cal=dateTime.toCalendar(Locale.getDefault());

String formatted = SIMPLE_DATE_FORMAT.format(cal.getTime()); //Final Required date

tl;博士

使用 java.time classes.

Instant.ofEpochSecond( 1_485_748_890L )
       .atZone( ZoneId.of( "America/Montreal" ) )
       .toLocalDate()
       .format(    
           DateTimeFormatter.ofPattern ( "dd-MM-uuuu" )
                            .withLocale ( Locale.UK )
       )

29-01-2017

Joda-Time

如果您想要 date-only 值而不需要 time-of-day,您应该使用 org.joda.time.LocalDate class.

仅供参考,Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes。 java.time 的大部分是 back-ported 到 Java 6、Java 7 和 Android(见下文)。

java.time

LocalDate class 代表 date-only 值,没有 time-of-day 和时区。

LocalDate birthdate = LocalDate.of( 1994 , 5 , 10 );

如果您的输入是自 1970 年第一个时刻以来的整秒计数(UTC 1970-01-01T00:00:00Z),请转换为 InstantInstant class represents a moment on the timeline in UTC with a resolution of nanoseconds(最多九 (9) 位小数)。

Instant instant = Instant.ofEpochSecond( 1_485_748_890L );

通过特定地区 wall-clock time, assign a time zone (ZoneId) to get a ZonedDateTime 的镜头来观察这一刻。

时区对于确定日期至关重要。对于任何给定时刻,日期在全球范围内因地区而异。例如,Paris France is a new day while still “yesterday” in Montréal Québec.

午夜后几分钟

指定 proper time zone name in the format of continent/region, such as America/Montreal, Africa/CasablancaPacific/Auckland。切勿使用 ESTIST 等 3-4 字母缩写,因为它们 不是 真正的时区,未标准化,甚至不是唯一的(!)。

ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( z );

LocalDate class 代表 date-only 值,没有 time-of-day 和时区。

LocalDate ld = zdt.toLocalDate();

要生成表示对象值的字符串,请为标准 ISO 8601 格式的字符串调用 toString,YYYY-MM-DD。

ld.toString(): 2017-01-29

对于其他格式,请使用 DateTimeFormatter class。您可以指定格式模式,或让 class 自动本地化。

DateTimeFormatter f = DateTimeFormatter.ofPattern ( "dd-MM-uuuu" ).withLocale ( Locale.UK );
String output = ld.format ( f );

instant.toString(): 2017-01-30T04:01:30Z

zdt.toString(): 2017-01-29T23:01:30-05:00[America/Montreal]

ld.toString(): 2017-01-29

output: 29-01-2017

关于java.time

java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

要了解更多信息,请参阅 Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310

从哪里获得java.time classes?

  • Java SE 8 and SE 9 及更高版本
    • Built-in。
    • 标准 Java API 的一部分,带有捆绑实施。
    • Java 9 添加了一些小功能和修复。
  • Java SE 6 and SE 7
    • java.time 的大部分功能是 back-ported 到 Java ThreeTen-Backport 中的 6 和 7。
  • Android
    • ThreeTenABP项目专门为Android改编ThreeTen-Backport(上面提到的)。
    • 参见