我试图将这个字符串日期:"2020-06-25T07:48:32Z“解析为"2020-06-25”这样的日期,并执行了如下方法:
String newDateFormat = "yyyy-MM-dd";
try {
Date newparseDate = new SimpleDateFormat(newDateFormat).parse(date);
System.out.println(newparseDate);
return new SimpleDateFormat(dateTimeFormatPattern).parse(date);
} catch (ParseException px) {
px.printStackTrace();
}
return null;
}但是我得到了这样的格式:清华六月25 : 00:00:00 CEST 2020
发布于 2020-06-25 08:25:02
我强烈建议您使用现代日期时间API而不是破碎的java.util日期时间API。
import java.time.LocalDate;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = ZonedDateTime.parse("2020-06-25T07:48:32Z");
System.out.println(zdt);
// Your required format can be got by simply using LocalDate which drops the
// time-zone and offset information
LocalDate ldt = zdt.toLocalDate();
System.out.println(ldt);
}
}输出:
2020-06-25T07:48:32Z
2020-06-25但是,如果您仍然希望使用过时的日期-时间API,您可以这样做:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
// Format for the given date-time string
SimpleDateFormat oldDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
// Desired format
SimpleDateFormat newDateFormat = new SimpleDateFormat("yyyy-MM-dd");
// The given date-time string
String dateStr = "2020-06-25T07:48:32Z";
// Parse to java.util.Date
Date newParseDate = oldDateFormat.parse(dateStr);
// Format to the desired format
String newDateStr = newDateFormat.format(newParseDate);
System.out.println(newDateStr);
}
}输出:
2020-06-25https://stackoverflow.com/questions/62570979
复制相似问题