Java Joda时间库getdays给出错误结果
} 天的结果是1Java Joda时间库getdays给出错误结果,java,jodatime,Java,Jodatime,} 天的结果是1 预计考虑到今天是2016年6月10日,应该是8天,这里没有什么不对,因为8天是一周零一天。如果你想得到“天”的8,你必须从周部分(即周*7+天)计算回来。这里没有错,你得到1,因为8天是一周和一天。如果你想得到“天”的8,你必须从周部分(即周*7+天)计算回来。要得到你想要的,你应该使用另一个PeriodType:PeriodType.yearMonthDay() 当前,您的代码使用标准(默认)PeriodType,它将周期分为年、月、周、日、小时、分、秒、毫秒。要获得您期望的
预计考虑到今天是2016年6月10日,应该是8天,这里没有什么不对,因为8天是一周零一天。如果你想得到“天”的8,你必须从周部分(即周*7+天)计算回来。这里没有错,你得到1,因为8天是一周和一天。如果你想得到“天”的8,你必须从周部分(即周*7+天)计算回来。要得到你想要的,你应该使用另一个
PeriodType
:PeriodType.yearMonthDay()
当前,您的代码使用标准(默认)
PeriodType
,它将周期分为年、月、周、日、小时、分、秒、毫秒。要获得您期望的内容,您应该使用另一个PeriodType
:PeriodType.yearMonthDay()
目前,您的代码使用标准(默认)
PeriodType
,它将周期分为年、月、周、日、小时、分、秒、毫秒。ohh所以我总是以年、月、周、日为单位显示吗?@RC。请把你的评论作为回答,我会接受的。哦,那么,我总是在几年、几个月、几周、几天内展示吗?@RC。请把你的意见作为回答,我会接受的。
public static void main(String args[]) throws ParseException{
String string = "May 2, 2016";
DateFormat format = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH);
Date date = format.parse(string);
System.out.println(date);
DateTime dateTime = new DateTime(date);
DateTime currentDate = new DateTime(Calendar.getInstance().getTime());
System.out.println(Calendar.getInstance().getTime());
Period p = new Period(dateTime, currentDate);
System.out.println(p.getYears());
System.out.println(p.getMonths());
System.out.println(p.getDays());
}
Period p = new Period(dateTime, currentDate, PeriodType.yearMonthDay());