下面的代碼是迄今爲止我所得到的最好的代碼。 .setTime()方法拋出異常。有沒有更好的方法來做到這一點或糾正這一點?如何將日期字符串解析爲java的Joda DateTime對象?
String format = "MM/dd/yyyy";
DateTime test = new DateTime();
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/dd/yyyy");
SimpleDateFormat formater = new SimpleDateFormat(format);
String startDateString = "09/10/2015";
String endDateString = "09/20/2015";
Date startDate = null;
Date endDate = null;
Calendar sampleDateStart = null;
Calendar sampleDateEnd = null;
try{
startDate = formater.parse(startDateString);
endDate = formater.parse(endDateString);
sampleDateStart.setTime(startDate);
sampleDateEnd.setTime(endDate);
}catch(Exception e){
System.out.println(e.getMessage());
}
NPE警報! :)用'Calendar.getInstance()'初始化你的日曆。 – Tunaki
@rdm很高興看到你想參與StackOverflow。發帖前請先搜索。本網站旨在成爲一個權威性問答庫,而不是一個開放式討論組。 –
你說在標題中你需要「將日期字符串解析成Joda DateTime」,但是在對兩個答案的評論中,你都說過「需要一個日曆對象」。當你*甚至不知道你想要什麼時,我們無法提供幫助。 – Andreas