Java:SimpleDataFormat未简化为指定模式?
尝试使用SimpleDataFormat.parse(“yyyy mm dd”)将yyyy mm dd字符串(例如2013-12-30)转换为日期对象 2013年12月30日的预期产量,2013年东部时间周一至十二月30日00:00:00收到的产量 尝试找出SimpleDataFormat返回不同格式的原因,但在尝试查看java api时不知所措。要求澄清正在发生的事情以及什么是更好的方法 注意:无法使用java.utl.DateJava:SimpleDataFormat未简化为指定模式?,java,date,simpledateformat,Java,Date,Simpledateformat,尝试使用SimpleDataFormat.parse(“yyyy mm dd”)将yyyy mm dd字符串(例如2013-12-30)转换为日期对象 2013年12月30日的预期产量,2013年东部时间周一至十二月30日00:00:00收到的产量 尝试找出SimpleDataFormat返回不同格式的原因,但在尝试查看java api时不知所措。要求澄清正在发生的事情以及什么是更好的方法 注意:无法使用java.utl.Date ... SimpleDateFormat sim
...
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateArray = new Date[rowCount];
try {
for(int index = 0; index < rowCount; index++){
dateArray[index] = simpleDateFormat.parse(fileArray[index][0]);
System.out.println(dateArray[index].toString());
}
} catch(ParseException err){
System.out.println("ERR: Data parse exception. Format is not correct.");
err.printStackTrace();
}
。。。
SimpleDataFormat SimpleDataFormat=新SimpleDataFormat(“yyyy-MM-dd”);
dateArray=新日期[行计数];
试一试{
for(int index=0;index
模式,mm
代表分钟,而不是月。对于月份,您需要使用MM
演示:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String strDate = "2013-12-30";
SimpleDateFormat sdfISO8601 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdfISO8601.parse(strDate);
System.out.println(date);
String strDateISO8601 = sdfISO8601.format(date);
System.out.println(strDateISO8601);
// Some other format
String strSomeOtherFormat = new SimpleDateFormat("EEEE MMM dd yyyy").format(date);
System.out.println(strSomeOtherFormat);
}
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String strDate = "2013-12-30";
LocalDate date = LocalDate.parse(strDate);
System.out.println(date);
String strDate8601 = date.toString();
System.out.println(strDate8601);
// Custom format
String customFormat = DateTimeFormatter.ofPattern("EEEE MMM dd uuuu").format(date);
System.out.println(customFormat);
}
}
我也建议你检查一下
请注意,java.util
的日期时间API及其格式化APISimpleDataFormat
已经过时,并且容易出错。我建议你完全停止使用它们,转而使用
使用现代日期时间API:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
public static void main(String[] args) throws ParseException {
String strDate = "2013-12-30";
SimpleDateFormat sdfISO8601 = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdfISO8601.parse(strDate);
System.out.println(date);
String strDateISO8601 = sdfISO8601.format(date);
System.out.println(strDateISO8601);
// Some other format
String strSomeOtherFormat = new SimpleDateFormat("EEEE MMM dd yyyy").format(date);
System.out.println(strSomeOtherFormat);
}
}
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String strDate = "2013-12-30";
LocalDate date = LocalDate.parse(strDate);
System.out.println(date);
String strDate8601 = date.toString();
System.out.println(strDate8601);
// Custom format
String customFormat = DateTimeFormatter.ofPattern("EEEE MMM dd uuuu").format(date);
System.out.println(customFormat);
}
}
您的日期字符串已经是日期的格式,因此在使用现代日期时间API时不需要使用任何格式化程序来解析它
有关现代日期时间API的更多信息,请访问
如果您正在为Android项目工作,并且您的Android API级别仍然不符合Java-8,请检查并。如果我可能会问,您为什么要使用旧的
SimpleDataFormat
和Date
类?与其直接打印Date
,不如使用SimpleDataFormat
(format()
方法)获取相同格式的字符串。该格式与Date
对象分开。使用simpleDataFormat.format(日期数组[索引])
@mcepender:参加一门过时的java大学课程。课程会更新,但作业不会更新。只有少数分配具有一个或两个使用不推荐的包的规范。在这种情况下,它将是Date类。@Kayaman:谢谢你的清楚解释。我对SimpleDataFormat的工作原理有了更好的理解,也对我所阅读的内容有了更好的理解。在这种情况下,我真的应该建立一个打印方法。谢谢你。@OleV.V.-我希望你做得很好。第一句尝试使用SimpleDataFormat.parse(“yyyy-mm-dd”)将yyyy-mm-dd的字符串(例如2013-12-30)转换为date对象。问题的才是真正的罪魁祸首