62,628
社区成员
发帖
与我相关
我的任务
分享
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateFormatTest {
public static void main(String[] args) throws Exception {
String str = "8/16/2017 5:00:00 PM";
SimpleDateFormat formerDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
SimpleDateFormat needDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = formerDateFormat.parse(str);
System.out.println(needDateFormat.format(d));
}
}
Exception in thread "main" java.text.ParseException: Unparseable date: "08/16/2017 05:00:00 PM"
at java.text.DateFormat.parse(Unknown Source)
at DateFormatTest.main(DateFormatTest.java:9)
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class DateFormatTest {
public static void main(String[] args) throws Exception {
String str = "08/16/2017 05:00:00 PM";
SimpleDateFormat formerDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a", Locale.US);
SimpleDateFormat needDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d = formerDateFormat.parse(str);
System.out.println(needDateFormat.format(d));
}
}
写成这样就对了