第一步:添加依赖
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();// 日 int hour = dt.getHourOfDay();// 小时 int minute = dt.getMinuteOfHour();// 分钟 int second = dt.getSecondOfMinute();// 秒 int millis = dt.getMillisOfSecond();// 毫秒
//格式化时间 DateTime dateTime = new DateTime(); String s1 = dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa"); String s2 = dateTime.toString("yyyy-MM-dd HH:mm:ss"); String s3 = dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa"); String s4 = dateTime.toString("yyyy/MM/dd HH:mm ZZZZ"); String s5 = dateTime.toString("yyyy/MM/dd HH:mm Z");
查看更详细使用请看Joda-TimeAPI:https://www.joda.org/joda-time/apidocs/index.html