【发布时间】:2015-09-27 16:50:52
【问题描述】:
下面的代码是迄今为止我想出的最好的。 .setTime() 方法正在引发异常。有没有更好的方法来做到这一点或纠正这个问题?
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()初始化您的日历。 -
@rdm 很高兴看到您想参与 StackOverflow。发帖前请先搜索。本网站旨在成为权威问答库,而不是开放式讨论组。
-
您在标题中说您需要“将日期字符串解析为 Joda DateTime”,但在两个答案的 cmets 中您都说您“需要一个日历对象”。当您甚至不知道自己想要什么时,我们无能为力。
-
@Andreas 你是对的。我会改正的。我有点新手所以有点学习曲线。