joda-time时间框架的使用

今天总结一下joda-time框架的使用,joda-time框架在日期比较方面确实非学方便;先贴出代码;

比如要比较两个日期的天数;

startTime为Date类型,endTime为结束日期类型,像下面这样就很轻松的完成;

public String getPeriod() {
    if(this.startTime!=null&&this.endTime!=null) {
        DateTime dt1 = new DateTime(this.startTime);
        DateTime dt2 = new DateTime(this.endTime);
        return Days.daysBetween(dt1, dt2).getDays() + "";
    }
        return period;
}
2.根据某个日,推出距离当前日期多少天前,或多少天后,代码如下:

DateTime datetime=new DateTime(date);
String strDate = datetime.plusDays(-1).toString("yyyy-MM-dd");
-1就代表向前推,正数代表向后;
将String按格式转换为时间;
 
 
DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
DateTime temp=DateTime.parse(str,format);
3.日期比较,计算现在的日期与某个日期的先后
DateTime dt=new DateTime();
DateTime otherDt=new DateTime();
if(dt.isAfter(dt.getMillis())){   
}
 
 
now.plusDays(1).toString("yyyy-MM-dd")//现在的日期向后推一天
DateTime  crt=new DateTime();
DateTime dt=new DateTime(crt.toLocalDate().plusMonths(1).dayOfMonth().withMinimumValue().toString())
//当天日期的下一个月的第一天;

 

猜你喜欢

转载自blog.csdn.net/hackland2012/article/details/71635365