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());
}
Run Code Online (Sandbox Code Playgroud)
获取此输出 :: Thu Dec 17 15:37:43 GMT+05:30 5
在科特林中,
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)
}
Run Code Online (Sandbox Code Playgroud)
输入日期:
Sat Dec 19 12:50:57 GMT+05:30 2020
Run Code Online (Sandbox Code Playgroud)
输出日期格式:
19-Dec-2020 12:50 PM
Run Code Online (Sandbox Code Playgroud)
解释 :
如果您需要日期对象表单字符串,请使用:
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();
}
Run Code Online (Sandbox Code Playgroud)
你的输出是
Thu Dec 17 15:37:43 GMT+05:30 2015:在日期对象中
再次将其转换为字符串:
String formateDate = new SimpleDateFormat("MM-dd-yyyy").format(date);
Log.v("output date ",formateDate);
Run Code Online (Sandbox Code Playgroud)
现在你的输出是:
12-17-2015
Run Code Online (Sandbox Code Playgroud)