下面的代码是迄今为止我所得到的最好的代码。 .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