gpt4 book ai didi

android - 如何从 dd-MM-yyyy HH :mm:ss to only dd-MM-yyyy HH:mm? 更改 android 中的日期格式

转载 作者:行者123 更新时间:2023-11-29 17:19:11 26 4
gpt4 key购买 nike

我试过下面的代码:

String created_Date = "25-Nov-15 14:23:34";

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");

System.out.println("Formatted date====>"+sdf.format(created_Date));

tv_date.setText(sdf.format(created_Date));

我得到了

llegalArgumentException: Bad class: class java.lang.String

最佳答案

首先是你的日期值不是 dd-MM-yyyy HH:mm:ss 格式,它应该像 dd-MMM-yy HH:mm:ss 。您的日期只有两位数的年份和三个字符的月份。

这里是更改日期格式的示例代码。

String mStringDate = "25-Nov-15 14:23:34";
String oldFormat= "dd-MMM-yy HH:mm:ss";
String newFormat= "dd-MM-yyyy HH:mm";

String formatedDate = "";
SimpleDateFormat dateFormat = new SimpleDateFormat(oldFormat);
Date myDate = null;
try {
myDate = dateFormat.parse(mStringDate);
} catch (java.text.ParseException e) {
e.printStackTrace();
}

SimpleDateFormat timeFormat = new SimpleDateFormat(newFormat);
formatedDate = timeFormat.format(myDate);

tv_date.setText(formatedDate );

关于android - 如何从 dd-MM-yyyy HH :mm:ss to only dd-MM-yyyy HH:mm? 更改 android 中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37291855/

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