【问题标题】:How to format Joda Time to dd-mm-yyyy in android?如何在android中将Joda Time格式化为dd-mm-yyyy?
【发布时间】:2017-01-29 15:58:11
【问题描述】:
 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 格式化日期。

【问题讨论】:

  • 为什么需要 Joda 来解析日期?
  • 基本上 dob 是纪元格式,我正在尝试将其转换为所需的格式。
  • 对...Java 内置的 Calendar、Date 和 SimpleDateFormat 都可以提供帮助。再说一遍,为什么是乔达?
  • 然后告诉我如何将日期转换为纪元和纪元到日期。

标签: android jodatime android-jodatime


【解决方案1】:

试试这个:

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));

【讨论】:

  • 您不需要解析 DOB 字符串。您可以将 long 值直接转换为正确的字符串。
【解决方案2】:

tl;博士

使用 java.time 类。

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

29-01-2017

乔达时间

如果您想要一个没有时间的仅日期值,您应该使用org.joda.time.LocalDate 类。

仅供参考,Joda-Time 项目现在位于maintenance mode,团队建议迁移到java.time 类。大部分 java.time 都向后移植到 Java 6、Java 7 和 Android(见下文)。

java.time

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

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

如果您的输入是自 1970 年 UTC 第一时刻 (1970-01-01T00:00:00Z) 以来的整秒数,请转换为 InstantInstant 类代表UTC 时间线上的时刻,分辨率为nanoseconds(最多九 (9) 位小数)。

Instant instant = Instant.ofEpochSecond( 1_485_748_890L );

要通过特定地区的wall-clock time 的镜头观看这一刻,请指定一个时区 (ZoneId) 以获取ZonedDateTime

时区对于确定日期至关重要。对于任何给定的时刻,日期在全球范围内因区域而异。例如,Paris France 中午夜过后几分钟是新的一天,而 Montréal Québec 中仍然是“昨天”。

continent/region 的格式指定proper time zone name,例如America/MontrealAfrica/CasablancaPacific/Auckland。切勿使用 3-4 个字母的缩写,例如 ESTIST,因为它们不是真正的时区,没有标准化,甚至不是唯一的 (!)。

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

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

LocalDate ld = zdt.toLocalDate();

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

ld.toString(): 2017-01-29

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

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[美国/蒙特利尔]

ld.toString(): 2017-01-29

输出:29-01-2017

关于java.time

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

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

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

从哪里获得 java.time 类?

【讨论】:

  • 现在已经很详细了,谢谢先生的好意
【解决方案3】:

试试这个:

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

【讨论】:

  • 给出 SimpleDateFormat 调用需要 API 24 当前是 API 16 的错误。
  • 这是因为你导入了 android.icu.text.SimpleDateFormat 而不是 java.text.SimpleDateFormat。
猜你喜欢
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-01
  • 2021-08-01
  • 2017-04-16
  • 1970-01-01
相关资源
最近更新 更多