android java标准时间,Java Android日历/日期格式化此格式XXXXXXXX

I actually use this

Calendar calendar = Calendar.getInstance();

final String currentDate =

DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

result = wednesday 27 march 2019

I need this : 27032019

Without , / or .

Only XXXXXXXX

Thanks you

解决方案

Here’s the modern answer and some additional thoughts.

For most purposes you should not want your date formatted to 27032019. It’s not easily readable by humans and not recommended for serialization.

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.

Serialization using java.time and ThreeTenABP

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

Output when running today is:

2019-03-27

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

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

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

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;

Links

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值