gpt4 book ai didi

android - 从 2016 年 4 月 18 日开始在 android 中解析日期格式 10 :41 AM to 04/18/2016 10:41

转载 作者:行者123 更新时间:2023-11-29 15:43:29 24 4
gpt4 key购买 nike

我正在尝试将 April 18 2016 10:41 AM 解析为 04/18/2016 10:41。这是我的代码

private String dateFormat(String strCurrentDate) throws ParseException {

SimpleDateFormat format = new SimpleDateFormat("mmmm dd yyyy hh:mm Z");
Date newDate = null;
newDate = format.parse(strCurrentDate);
format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
String date = format.format(newDate);
Log.d("DATE_FORMATE_TESTING", date);
return date;

}

但它给出了以下错误

04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: java.text.ParseException: Unparseable date: "April 18 2016 01:41 PM" (at offset 0)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at java.text.DateFormat.parse(DateFormat.java:579)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at gps.clock.com.MainActivity.dateFormat(MainActivity.java:441)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at gps.clock.com.MainActivity.access$500(MainActivity.java:70)
04-18 10:41:43.834 7526-7526/gps.clock.com W/System.err: at gps.clock.com.MainActivity$1$4.onClick(MainActivity.java:303)

谁能告诉我该如何解决?为什么显示 Unparseable date。我应该如何解析它?提前致谢。

最佳答案

试试下面的代码

    String strCurrentDate = "April 18 2016 10:41 AM";
SimpleDateFormat format = new SimpleDateFormat("MMM dd yyyy hh:mm a");
Date newDate = null;
try {
newDate = format.parse(strCurrentDate);
} catch (ParseException e) {
e.printStackTrace();
}
format = new SimpleDateFormat("MM/dd/yyyy hh:mm");
String date = format.format(newDate);
System.out.print(date);

关于android - 从 2016 年 4 月 18 日开始在 android 中解析日期格式 10 :41 AM to 04/18/2016 10:41,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com