java在Android中更改日期格式
我是Android和Java开发的新手
我有这一天的类型"Fri Jan 27 00:00:00 GMT+00:00 1995"
<;——(输入)
我想得到"1995/27/01"
<;-(产出)
我使用的代码是:
String inputPattern = "EEE MMM dd HH:mm:ss z yyyy";
String outputPattern = "yyyy-MM-dd";
SimpleDateFormat inputFormat = new SimpleDateFormat(inputPattern);
SimpleDateFormat outputFormat = new SimpleDateFormat(outputPattern);
Date date = inputFormat.parse("Fri Jan 27 00:00:00 GMT+00:00 1995");
String str = outputFormat.format(date);
但我得到了一个ParseException
知道为什么吗
# 1 楼答案
如果使用
java.time
(可从Java 8获得),可以这样做:这段代码的输出是
# 2 楼答案
可能会在代码中添加applyPattern函数