我正在尝试使用IntervalJoda Time类,但我无法使用它的构造函数.它不采用这种格式.
我试图DateTime从格式的mysql DB中提取两个,yyyy-MM-dd HH:mm:ss并以String格式检索它.我试图将其转换为日期格式,但间隔类无法采用日期格式.请帮助我应该使用什么???
String text = "2011-10-02 18:48:05";
String text2 = "2011-10-02 18:50:05";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date olddate=sdf.parse(text);
Date newdate=sdf.parse(text2);
System.out.println(olddate);
System.out.println(newdate);
// Interval interval = new Interval(olddate, newdate);
} catch (ParseException ex) {
JOptionPane.showMessageDialog(null, "Error at Timestamp subtract Format function Dategenerator" + ex.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
不要使用java.util.Date,但是Joda Time的org.joda.time.DateTime.
对于解析使用Joda Time的org.joda.time.format.DateTimeFormat.
这应该工作:
String text = "2011-10-02 18:48:05";
String text2 = "2011-10-02 18:50:05";
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime oldDate = formatter.parseDateTime(text);
DateTime newDate = formatter.parseDateTime(text2);
System.out.println(oldDate);
System.out.println(newDate);
Interval interval = new Interval(oldDate, newDate);
System.out.println(interval);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3733 次 |
| 最近记录: |