如何转换12月17日星期四15:37:43 GMT+;2015年05月30日android中的字符串到目前为止
获取此输出::Thu Dec 17 15:37:43 GMT+05:30 5请检查我以前使用过的解决方案:如何转换12月17日星期四15:37:43 GMT+;2015年05月30日android中的字符串到目前为止,android,Android,获取此输出::Thu Dec 17 15:37:43 GMT+05:30 5请检查我以前使用过的解决方案: SimpleDateFormat format =new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH); format.setTimeZone(TimeZone.getTimeZone("GMT+05:30")); try { long diff2=format.parse(accidentRepo
SimpleDateFormat format =new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
format.setTimeZone(TimeZone.getTimeZone("GMT+05:30"));
try {
long diff2=format.parse(accidentReports.getAccidentDate());
}
如果需要日期对象表单字符串,请使用:
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
try {
String temp = "Thu Dec 17 15:37:43 GMT+05:30 2015";
Date expiry = formatter.parse(temp);
} catch (Exception e) {
e.printStackTrace();
}
您的输出是周四12月17日15:37:43格林尼治标准时间+2015年5:30
:
输入日期对象
再次将其转换为字符串:
Date date=null;
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy");
String temp = "Thu Dec 17 15:37:43 GMT+05:30 2015";
try {
date = formatter.parse(temp);
Log.e("formated date ", date + "");
} catch (ParseException e) {
e.printStackTrace();
}
现在您的输出是: String formateDate = new SimpleDateFormat("MM-dd-yyyy").format(date);
Log.v("output date ",formateDate);
很抱歉,回答这个问题可能太晚了,但它可能在不久的将来对任何人都有帮助。 只需在传递日期时调用此简单方法(Thu Dec 17 15:37:43 GMT+05:30 2015),您将得到如下结果:2015-12-17
12-17-2015
请记住,这是一个日期格式的值,而不是字符串值
如何致电:
private String getFormattedDate(Date timeZone){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = df.format(timeZone);
return formattedDate;
}
在科特林
getFormattedDate(date);
输入日期:
private fun convertToCustomFormat(dateStr: String?): String {
val utc = TimeZone.getTimeZone("UTC")
val sourceFormat = SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")
val destFormat = SimpleDateFormat("dd-MMM-YYYY HH:mm aa")
sourceFormat.timeZone = utc
val convertedDate = sourceFormat.parse(dateStr)
return destFormat.format(convertedDate)
}
输出日期格式:
Sat Dec 19 12:50:57 GMT+05:30 2020
说明:
格式说明
- EEE:日(周一)
- MMM:月份大写(12月)
- MM:计算中的天数(324)
- 月(12)
- dd:日期(3)
- 小时:小时(12)
- 毫米:分钟(50)
- 学生:秒(34)
- yyyy:Year(2020)//yyyy和yyyy都是相同的
- YYYY:年份(2020年)
- zzz:GMT+05:30
- 机管局:(上午/下午)
java.util
date-time API及其格式化APISimpleDataFormat
已经过时,并且容易出错。建议完全停止使用,并切换到*
使用现代日期时间API:
19-Dec-2020 12:50 PM
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Thu Dec 17 15:37:43 GMT+05:30 2015";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);
// Default string i.e. OffsetDateTime#toString
System.out.println(odt);
// A custom string
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssO", Locale.ENGLISH);
String formatted = odt.format(dtfOutput);
System.out.println(formatted);
}
}
输出:
19-Dec-2020 12:50 PM
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String strDateTime = "Thu Dec 17 15:37:43 GMT+05:30 2015";
DateTimeFormatter dtfInput = DateTimeFormatter.ofPattern("E MMM d H:m:s O u", Locale.ENGLISH);
OffsetDateTime odt = OffsetDateTime.parse(strDateTime, dtfInput);
// Default string i.e. OffsetDateTime#toString
System.out.println(odt);
// A custom string
DateTimeFormatter dtfOutput = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssO", Locale.ENGLISH);
String formatted = odt.format(dtfOutput);
System.out.println(formatted);
}
}
从了解有关现代日期时间API的更多信息
*无论出于何种原因,如果您必须坚持使用Java6或Java7,您可以使用哪个backport将大部分Java.time功能移植到Java6&7。如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并确认。
您的代码不打印任何内容-您从哪里获得输出?您希望日期的格式是什么?设置是什么
acdentReports.getacdentDate()
?您的格式字符串看起来对我来说是正确的,您确定输入不是将日期设置为5年吗?作为一个旁白,考虑扔掉长期过时和臭名昭著的麻烦代码< SimpleDateFormat <代码>和朋友,并添加到Android项目中,以便使用<代码> java.时间>代码>,java java日期和时间API。与之合作是如此美好。可能的重复请不要教年轻人使用早已过时且臭名昭著的麻烦SimpleDateFormat
类。至少不是第一个选择。而且不是毫无保留的。今天,我们在及其DateTimeFormatter
中有了更好的功能。是的,你可以在Android上使用它。对于较旧的Android,请查看或查看。另外,您的destFormat
格式模式不正确,在新年前后会给您带来惊喜。您需要注意格式模式字母的情况//yyyy和yyyy都是相同的。这不是真的。