第一步:添加依赖
implementation 'net.danlew:android.joda:2.9.5'
第二步:更具自己需求操作,下面我粘出我项目中用到的和一些简单操作
DateTime currTime = new DateTime(currTimeFormat); DateTime violationTime = new DateTime(violationIntime); int days = Days.daysBetween(violationTime, currTime).getDays();//两个时间的天数 DateTime dateTime = new DateTime(currTime.getYear(), currTime.getMonthOfYear(), 14, 12, 0, 0, 000); int maximumValue = dateTime.dayOfMonth().getMaximumValue();//获取当前月的有多少天 if (days > maximumValue) { violationRecord.delete(); }
一些常用操作:
DateTime dt = new DateTime();//当前系统所在时区的当前时间,精确到毫秒 DateTime dt = new DateTime(new Date());//它表示这个时间戳距1970-01-01T00:00:00Z的毫秒数。使用默认的时区 DateTime dt = new DateTime(2019, 12, 18, 17, 23, 51, 501);//构造一个指定的时间,这里精确到秒,类似地其它构造方法也可以传入毫秒
int year = dt.getYear();// 年 int month = dt.getMonthOfYear();// 月 int day = dt.getDayOfMonth();