1、 日历类Calendar
Calendar类使用其静态的getInstance()方法获取一个日历实例,该实例为当前的时间;如果想改变时间,可以通过其setTime方法传入一个Date对象,即可获得Date对象所表示时间的Calendar对象
public static void calculateTimeDifferenceByCalendar(String strDate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
Date date = formatter.parse(strDate);
Calendar c1 = Calendar.getInstance(); //当前日期
Calendar c2 = Calendar.getInstance();
c2.setTime(date); //设置为另一个时间
int year = c1.get(Calendar.YEAR);
int oldYear = c2.get(Calendar.YEAR);
//这里只是简单的对两个年份数字进行相减,而没有考虑月份的情况
System.out.println("传入的日期与今年的年份差为:" + (year - oldYear));
}
2、周期类Period
通过调用Period类的静态方法between,传入两个待比较的LocalDate对象today与oldDate,得到的Period的对象p中就包含了today与oldDate两个日期相差的年、月、日信息,可以通过p.getYears()等方法取出
public static void calculateTimeDifferenceByPeriod(int year, Month month, int dayOfMonth) {
LocalDate today = LocalDate.now();
System.out.println("Today:" + today);
LocalDate oldDate = LocalDate.of(year, month, dayOfMonth);
System.out.println("OldDate:" + oldDate);
Period p = Period.between(oldDate, today);
System.out.printf("目标日期距离今天的时间差:%d 年 %d 个月 %d 天\n", p.getYears(), p.getMonths(), p.getDays());
}
3、Duration类
Duration与Period相对应,Period用于处理日期,而Duration计算时间差还可以处理具体的时间,也是通过调用其静态的between方法,该方法的签名是between(Temporal startInclusive, Temporal endExclusive),因此可以传入两个Instant的实例(Instant实现了Temporal接口),并可以以毫秒(toMillis)、秒(getSeconds)等多种形式表示得到的时间差
public static void calculateTimeDifferenceByDuration() {
Instant inst1 = Instant.now(); //当前的时间
System.out.println("Inst1:" + inst1);
Instant inst2 = inst1.plus(Duration.ofSeconds(10)); //当前时间+10秒后的时间
System.out.println("Inst2:" + inst2);
Instant inst3 = inst1.plus(Duration.ofDays(125)); //当前时间+125天后的时间
System.out.println("inst3:" + inst3);
System.out.println("以毫秒计的时间差:" + Duration.between(inst1, inst2).toMillis());
System.out.println("以秒计的时间差:" + Duration.between(inst1, inst3).getSeconds());
}
我分别使用了Calendar类和Period类来计算两个日期的相差多久时间
Calendar:
private void loadTime2(int position) {
long time = informations.get(position).getTime();
Calendar calendar1 = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
calendar1.setTime(new Date(currentTime));
calendar2.setTime(new Date(time * 1000));
if (calendar1.get(Calendar.YEAR) != calendar2.get(Calendar.YEAR)) {
textView4.setText((calendar1.get(Calendar.YEAR) - calendar2.get(Calendar.YEAR)) + "年以前");
} else if (calendar1.get(Calendar.MONTH) != calendar2.get(Calendar.MONTH)) {
textView4.setText(calendar1.get(Calendar.MONTH) - calendar2.get(Calendar.MONTH) + "月以前");
} else if (calendar1.get(Calendar.DAY_OF_MONTH) != calendar2.get(Calendar.DAY_OF_MONTH)) {
textView4.setText(calendar1.get(Calendar.DAY_OF_MONTH) - calendar2.get(Calendar.DAY_OF_MONTH) + "天以前");
} else if (calendar1.get(Calendar.HOUR_OF_DAY) != calendar2.get(Calendar.HOUR_OF_DAY)) {
textView4.setText(calendar1.get(Calendar.HOUR_OF_DAY) - calendar2.get(Calendar.HOUR_OF_DAY) + "小时以前");
}else if (calendar1.get(Calendar.SECOND)!=calendar2.get(Calendar.SECOND)){
textView4.setText(calendar1.get(Calendar.SECOND) - calendar2.get(Calendar.SECOND) + "分钟以前");
}
}
这里按年月日时分秒的顺序依次判断是否已哪个单位计算时间,当两个日期年份不相同时,算出两个日期的差,显示n年以前。后面依次类推。
Period:
private void loadTime(int position) {
long time = informations.get(position).getTime();
Calendar instance = Calendar.getInstance();
instance.setTime(new Date(time * 1000));
Log.i("TAG", "getView: " + "年份" + instance.get(Calendar.YEAR));
LocalDate date = LocalDate.of(instance.get(Calendar.YEAR), instance.get(Calendar.MONTH) + 1, instance.get(Calendar.DAY_OF_MONTH));
Period between = Period.between(date, now);
textView4.setText(between.getYears() + "年" + between.getMonths() + "月" + between.getDays() + "日");
}
这里使用LocalDate.now获取当前日期,再通过LocalDate.of()传入年月日,获取一个新日期,再使用Period类的between方法计算两个日期的差并封装到Period类中,可以通过Period类中的getYears()、getMoths()、getDays()获取年月日信息。