Java Android日历/日期设置此格式XXXXXXXX [英] Java Android Calendar/DateFormat this format XXXXXXXX
问题描述
我实际上是用这个
Calendar calendar = Calendar.getInstance();
final String currentDate =
DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
结果= 2019年3月27日星期三
result = wednesday 27 march 2019
我需要这个:27032019 没有,/或. 只有XXXXXXXX
I need this : 27032019 Without , / or . Only XXXXXXXX
谢谢
推荐答案
这是现代的答案和一些其他想法.
Here’s the modern answer and some additional thoughts.
在大多数情况下,您不希望将日期格式设置为27032019
.它不容易被人类阅读,也不建议进行序列化.
For most purposes you should not want your date formatted to 27032019
. It’s not easily readable by humans and not recommended for serialization.
还要考虑不使用Calendar
,DateFormat
,SimpleDateFormat
或Date
.这些课程早已过时,设计欠佳.相反,您可以使用java.time(现代Java日期和时间API).感觉好多了.
Also consider not using Calendar
, DateFormat
, SimpleDateFormat
or Date
. Those classes are long outdated and poorly designed. Instead you may use java.time, the modern Java date and time API. It’s so much nicer to work with.
如果您需要日期字符串对机器可读,例如用于JSON或用于存储在您或其他人需要从中读取的文本文件中,请使用标准的ISO 8601格式. LocalDate.toString
产生这种格式,所以我们不需要任何显式的格式化程序:
If you need your date string to be machine readable — for example if it’s for a JSON or for storing into a text file from where you or someone else needs to read it back — use the standard ISO 8601 format. LocalDate.toString
produces this format, so we don’t need any explicit formatter:
final String currentDate
= LocalDate.now(ZoneId.of("Africa/Khartoum")).toString();
System.out.println(currentDate);
今天运行时的输出是:
2019-03-27
2019-03-27
更紧凑的ISO 8601格式
如果您坚持使用没有标点符号的紧凑格式,那么ISO 8601也可以提供:
A compacter ISO 8601 format
If you insist on a compact format without any punctuation, ISO 8601 offers that too:
final String currentDate = LocalDate.now(ZoneId.of("Africa/Khartoum"))
.format(DateTimeFormatter.BASIC_ISO_DATE);
20190327
20190327
您的格式
如果您真的坚持(我不明白为什么要这么做),那么java.time当然也可以产生您要求的格式:
Your format
And if you really insist (I don’t see why you should), java.time can of course produce your requested format too:
final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("ddMMuuuu");
final String currentDate = LocalDate.now(ZoneId.of("Africa/Khartoum"))
.format(dateFormatter);
27032019
27032019
问题:我可以在Android上使用java.time吗?
是的,java.time在旧的和更新的Android设备上都能很好地工作.它只需要至少 Java 6 .
- 在Java 8和更高版本以及更新的Android设备(API级别26起)中,内置了现代API.
- 在Java 6和7中,获得了ThreeTen Backport,这是现代类的backport(JSR 310的ThreeTen;请参见底部的链接).
-
在(较旧的)Android上,使用Android版本的ThreeTen Backport.称为ThreeTenABP.并确保使用子包从
org.threeten.bp
导入日期和时间类.我对以上代码段的导入是:
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages. My imports for the above snippets are:
import org.threeten.bp.LocalDate;
import org.threeten.bp.ZoneId;
import org.threeten.bp.format.DateTimeFormatter;
- Oracle教程:日期时间解释了如何使用java.time./li>
- Java规范请求(JSR)310 ,其中
java.time
是首先描述. - ThreeTen Backport项目,即
java.time
到Java 6和7的backport(ThreeTen用于JSR-310). - ThreeTenABP ,ThreeTen Backport的Android版本
- 问题:如何在Android Project中使用ThreeTenABP ,非常详尽的解释.
- 维基百科文章:ISO 8601
- Oracle tutorial: Date Time explaining how to use java.time.
- Java Specification Request (JSR) 310, where
java.time
was first described. - ThreeTen Backport project, the backport of
java.time
to Java 6 and 7 (ThreeTen for JSR-310). - ThreeTenABP, Android edition of ThreeTen Backport
- Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
- Wikipedia article: ISO 8601
这篇关于Java Android日历/日期设置此格式XXXXXXXX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!