如何在 android 中将 Thu Dec 17 15:37:43 GMT+05:30 2015 字符串转换为日期

Akh*_*han -1 android

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

Tip*_*iff 6

在科特林中,

   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)

解释 :

格式说明

  • EEE:日(周一)
  • MMM:文字月份(十二月)
  • MM : 计数日 ( 324 )
  • 毫米 : 月份 (12)
  • dd : 日期 (3)
  • HH : 小时 ( 12 )
  • 毫米:分钟 (50)
  • ss : 秒 ( 34 )
  • yyyy: 年 ( 2020 ) //yyyy 和 YYYY 相同
  • YYYY:年份 (2020)
  • zzz : 格林威治标准时间+05:30
  • aa:(上午/下午)


san*_*Xme 5

如果您需要日期对象表单字符串,请使用:

    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)