在android中更改日期字符串格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10426492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change date string format in android
提问by Krishna Suthar
I am getting date string from SAX parsing like this: Wed, 18 Apr 2012 07:55:29 +0000
我从 SAX 解析中获取日期字符串,如下所示:Wed, 18 Apr 2012 07:55:29 +0000
Now, I want this string as : Apr 18, 2012 01:25 PM
现在,我希望此字符串为:2012 年 4 月 18 日下午 01:25
How can I do this?
我怎样才能做到这一点?
回答by V.J.
SimpleDateFormat format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(Date.parse("Your date string"));
UPDATE :-
更新 :-
As on, Date.parse("Your date string");
is deprecated.
如上,Date.parse("Your date string");
已弃用。
String strCurrentDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
Date newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
String date = format.format(newDate);
回答by elirigobeli
This will do it:
这将做到:
public static String formateDateFromstring(String inputFormat, String outputFormat, String inputDate){
Date parsed = null;
String outputDate = "";
SimpleDateFormat df_input = new SimpleDateFormat(inputFormat, java.util.Locale.getDefault());
SimpleDateFormat df_output = new SimpleDateFormat(outputFormat, java.util.Locale.getDefault());
try {
parsed = df_input.parse(inputDate);
outputDate = df_output.format(parsed);
} catch (ParseException e) {
LOGE(TAG, "ParseException - dateFormat");
}
return outputDate;
}
Example:
例子:
String date_before = "1970-01-01";
String date_after = formateDateFromstring("yyyy-MM-dd", "dd, MMM yyyy", date_before);
Output:
输出:
date_after = "01, Jan 1970";
回答by Hpsaturn
From oficial documentation, in the new API (18+) you should be implement this:
根据官方文档,在新的 API (18+) 中,您应该实现:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
String time=sdf.format(new Date());
Documentation: http://developer.android.com/reference/java/text/SimpleDateFormat.html
文档:http: //developer.android.com/reference/java/text/SimpleDateFormat.html
回答by nrallakis4
The easiest solution is to use DateFormat's getter methods: The getDateTimeInstance DateFormat object formats date like that: Dec 31, 1969 4:00:00 PM, however there are those extra zeroes after 4:00
最简单的解决方案是使用DateFormat的 getter 方法: getDateTimeInstance DateFormat 对象格式化日期如下:Dec 31, 1969 4:00:00 PM,但是在 4:00 之后有那些额外的零
Date date = new Date();
String fDate = DateFormat.getDateTimeInstance().format(date);
System.out.println(fDate);
回答by areg_noid
This is works for me, using SimpleDateFormatand Calendar
这对我有用,使用SimpleDateFormat和Calendar
String originDate = "Wed, 18 Apr 2012 07:55:29 +0000";
SimpleDateFormat formatIn = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z");
SimpleDateFormat formatOut = new SimpleDateFormat("MMM dd,yyyy hh:mm a");
Calendar calendar = Calendar.getInstance();
calendar.setTime(formatIn.parse(originDate));
String newDate = formatOut.format(calendar.getTime());
回答by Ole V.V.
java.time and ThreeTenABP
java.time 和 ThreeTenABP
The modern answer to this question is overdue. When this question was asked in 2012, using SimpleDateFormat
and/or DateFormat
as the other answers do, was right even though those classes had always been troublesome. They were replaced just a couple of years later by java.time, the modern Java date and time API, which I frankly find much nicer to work with. So so I am doing.
对这个问题的现代答案是迟到的。当 2012 年提出这个问题时,使用SimpleDateFormat
和/或DateFormat
其他答案是正确的,即使这些课程一直很麻烦。几年后,它们被 java.time(现代 Java 日期和时间 API)取代,坦率地说,我发现使用它更好。所以我正在这样做。
I suggest you separate your conversion into two operations. In you program don't keep date and time as a string. Keep them as a proper date-time object. So when parsing from SAX also parse the date-time string into an OffsetDateTime
immediately. When at a later point you need to show the date and time to the user (or transmit it to another system), convert it into the user's time zone and format it into an appropriate string for that purpose.
我建议您将转换分为两个操作。在您的程序中,不要将日期和时间保留为字符串。将它们保留为适当的日期时间对象。因此,当从 SAX 解析时,也要OffsetDateTime
立即将日期时间字符串解析为一个。稍后您需要向用户显示日期和时间(或将其传输到另一个系统)时,将其转换为用户的时区并将其格式化为适当的字符串。
Parse into an OffsetDateTime
解析为 OffsetDateTime
The string you got conforms with RFC 1123 format. java.time has a built-in formatter for that, which is good because it saves us from constructing our own formatter.
您得到的字符串符合 RFC 1123 格式。java.time 有一个内置的格式化程序,这很好,因为它使我们无需构建自己的格式化程序。
String stringFromSaxParsing = "Wed, 18 Apr 2012 07:55:29 +0000";
OffsetDateTime dateTime = OffsetDateTime.parse(
stringFromSaxParsing, DateTimeFormatter.RFC_1123_DATE_TIME);
System.out.println(dateTime);
Output from this snippet is:
这个片段的输出是:
2012-04-18T07:55:29Z
2012-04-18T07:55:29Z
Convert to user's time zone and format
转换为用户的时区和格式
DateTimeFormatter wantedFormatter = DateTimeFormatter.ofPattern("MMM dd, uuuu hh:mm a", Locale.ENGLISH);
String formatted = dateTime.atZoneSameInstant(ZoneId.systemDefault())
.format(wantedFormatter);
System.out.println(formatted);
I ran this in Asia/Colombo time zone and got the desired:
我在亚洲/科伦坡时区运行了这个并得到了想要的:
Apr 18, 2012 01:25 PM
2012 年 4 月 18 日下午 01:25
Question: Doesn't java.time require Android API level 26?
问题:java.time 不需要 Android API 级别 26 吗?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少Java 6。
- In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
- In non-Android 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.
- 在 Java 8 及更高版本和更新的 Android 设备(从 API 级别 26)中,现代 API 是内置的。
- 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代类的 backport(ThreeTen for JSR 310;请参阅底部的链接)。
- 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从
org.threeten.bp
子包中导入日期和时间类。
Links
链接
- Oracle tutorial: Date Timeexplaining 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.
- Oracle 教程:解释如何使用 java.time 的日期时间。
- Java 规范请求 (JSR) 310,
java.time
首先描述的地方。 - ThreeTen Backport 项目,
java.time
向 Java 6 和 7 (ThreeTen for JSR-310)的向后移植。 - ThreeTenABP, ThreeTen Backport安卓版
- 问题:如何在Android项目中使用ThreeTenABP,讲解很透彻。