业务中有关【时间】的使用

计算两个日期之间的年间隔

int startYear = Integer.parseInt(hmcinfoDTO.getLdhtqssj().substring(0,4));
int startMonth = Integer.parseInt(hmcinfoDTO.getLdhtqssj().substring(4,6));
int startDay = Integer.parseInt(hmcinfoDTO.getLdhtqssj().substring(6,8));
LocalDate startDate = LocalDate.of(startYear, startMonth, startDay);
int endYear = Integer.parseInt(hmcinfoDTO.getLdhtjssj().substring(0,4));
int endMonth = Integer.parseInt(hmcinfoDTO.getLdhtjssj().substring(4,6));
int endDay = Integer.parseInt(hmcinfoDTO.getLdhtjssj().substring(6,8));
LocalDate endDate = LocalDate.of(endYear,endMonth,endDay);

// 计算日期间隔
Period period = Period.between(startDate, endDate);

// 获取年数差异
int years = period.getYears();
System.out.println("获取年数差异:"+years);
System.out.println("距离当前多少月:" + period.getMonths());    
System.out.println("距离当前多少日:" + period.getDays());
LocalDate of(int year, int month, int dayOfMonth)
// 从年、月和日获取实例 LocalDate 
// year – 代表年份,从MIN_YEAR年到MAX_YEAR年 
// month – 代表的月份,从1月1日(1月)到12日(12月) 
// dayOfMonth – 表示从 1 到 31 的月份中的某天 
Period.between(LocalDate startDate, LocalDate endDate) 方法,获取相差时间
//startDate –开始日期包括在内,并且不能为null。
//endDate –结束日期是排他的,并且不能为null。

注:使用Period.between() 方法来获取,相差天数、相差月数的时候,发现只能计算同月的天数、同年的月数,不能计算隔月的天数以及隔年的月数

解决方法:

// 直接计算,距离当天多少日
LocalDate from = LocalDate.of(2017, 9, 1);
long day = LocalDate.now().toEpochDay() - from.toEpochDay();
System.out.println("距离当前多少日:" + day);

//计算距离当天多少月
Period period = Period.between(startDate, endDate);
System.out.println("距离当前多少月:" + period.getYears()*12+period.getMonths());

当前时间减一年,减一个月,减一天

Date date = new Date();//获取当前时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);

calendar.add(Calendar.YEAR, -1);//当前时间减去一年,即一年前的时间
calendar.add(Calendar.MONTH, -1);//当前时间前去一个月,即一个月前的时间
calendar.add(Calendar.DAY_OF_MONTH,-1); //当前时间减去一天,即一天前的时间

calendar.getTime();//获取处理过的时间(获取一年前的时间,或者一个月前的时间,或者一天前的时间)
calendar.getTimeInMillis();//返回当前时间的毫秒数

将字符串类型转为日期类型

    String s1 = "20151117190936";
    String s2="20090915-17:20:12";
    try {
        Date date1 = new SimpleDateFormat("yyyyMMddHHmmss").parse(s1);
        //输出格式
        System.out.println("=======解析字符串1======");
        System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date1));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date1));
        System.out.println("=======解析字符串2======");
        Date date2 = new SimpleDateFormat("yyyyMMdd-HH:mm:ss").parse(s2);
        System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(date2));
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date2));
    } catch (ParseException e) {
        e.printStackTrace();
    }

java中比较 字符串类型 日期大小

String类中的compareTo方法
原理是将字符串转成char[],从char[0]开始进行比较,如果两值不相等,则返回相减的结果。一般将结果与0相比,进行判断,并不关心返回的具体值;
String s1="2020-07-22";
String s2="2020-06-22";
System.out.println(s1.compareTo(s2));//结果:1
System.out.println(s2.compareTo(s1));//结果:-1<br>相等时 结果是 0<br><br>

需要注意的是:
两个日期格式必须相同,如 :2020-01-01与2020-05-05。如果格式不同,比如2020/01/01,则可能会影响最终结果。

在保证日期格式相同的基础上,有没有时分秒只会影响当年月日相同时的结果:

String s1="2020/06/22";
String s2="2020/05/22 13:01:15";
System.out.println(s1.compareTo(s2));//结果:1

String s3="2020/06/22";
String s4="2020/06/22 13:01:15";
System.out.println(s3.compareTo(s4));//结果:-9
SimpleDataFormat类中的compaTo方法

SimpleDateFormat可以通过parse方法,将一个日期类型的字符串,转化成对应的Date型日期。我们就可以对Date进行操作。
Date同样有compareTo的方法,可以将String通过SimpleDateFormat转化成Date类型,再进行compareTo的判断。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String now =  "2020-01-01";
String end =  "2019-01-01";
Date nowDate = sdf.parse(now);
Date endDate = sdf.parse(end);
System.out.println(nowDate.compareTo(endDate));//结果:1


Date  dat1=new Date();
Date  dat2=new Date();
int va = dat1.compareTo(dat2);
如果 dat1>dat2  va=1;
      dat1=dat2  va=0;
      dat1<dat2  va=-1;
SimpleDataFormat类中的getTime方法

该方法的返回值是自1970-01-01 00:00:00到指定日期间的毫秒数,返回值类型为long。
通过比较毫秒数大小,同样可以比较时间的大小。

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
long time1 = sdf.parse("2019-1-1").getTime();
long time2 = sdf.parse("2020-1-1").getTime();
System.out.println(time1>time2);//false

hutool的DateUtil工具类使用

引入坐标
<dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-all</artifactId>
  <version>5.3.9</version>
</dependency>
date方法获取当前Date类型的日期时间 (格式)
Date date = DateUtil.date(); // 2020-09-14 10:27:06
// Calendar类型转Date类型       
Date date2 = DateUtil.date(Calendar.getInstance()); // 2020-09-14 10:27:07
// 毫秒值转Date类型
Date date3 = DateUtil.date(System.currentTimeMillis()); // 2020-09-14 10:27:07
now方法获取当前字符串类型的日期
//当前时间字符串,格式:yyyy-MM-dd HH:mm:ss
String now = DateUtil.now(); // 2020-09-14 10:22:46
//当前日期字符串,格式:yyyy-MM-dd
String today= DateUtil.today(); // 2020-09-14
parse方法将字符串类型日期解析为Date类型日期
// 将字符串格式的日期解析成Date格式
String dateStr = "2017-03-01"; // 2017-03-01 00:00:00
Date date = DateUtil.parse(dateStr);

// 将字符串格式的日期解析成Date格式
String dateStr2 = "2017-03-01 23:20:23";
Date date2 = DateUtil.parse(dateStr2); // 2017-03-01 23:20:23

// 将字符串格式的日期解析成指定的Date格式
Date date3 = DateUtil.parse(dateStr2, "yyyy-MM-dd"); // 2017-03-01 00:00:00
format方法将字符串类型日期格式化为指定格式的字符串类型日期
// 当前Date格式时间
Date date = DateUtil.date(); 
// 格式化为指定格式的字符串
String format = DateUtil.format(date, "yyyy/MM/dd");//结果:2020/07/16
// 格式化为日期格式字符串
String formatDate = DateUtil.formatDate(date);//结果:2020-07-16
// 格式化为日期时间格式字符串
String formatDateTime = DateUtil.formatDateTime(date);//结果:2020-07-16 20:22:54
// 格式化为时间格式字符串
String formatTime = DateUtil.formatTime(date);//结果:20:22:54
year方法和month方法获取Date类型日期的年份和月份
Date date = DateUtil.date();
//获得年的部分
int year = DateUtil.year(date);
//获得月份,从0开始计数
int month = DateUtil.month(date)+1;
beginOfDay方法和endOfMonth方法获取某天/某月的开始活结束时间
Date date = DateUtil.date();
//一天的开始,结果:2017-03-01 00:00:00
Date beginOfDay = DateUtil.beginOfDay(date);
//一天的结束,结果:2017-03-01 23:59:59
Date endOfDay = DateUtil.endOfDay(date);
//一月的开始,结果:2017-03-01 00:00:00
Date beginOfMonth = DateUtil.beginOfMonth(date);
//一月的结束,结果:2017-03-31 23:59:59
Date endOfMonth = DateUtil.endOfMonth(date);
offsetHour方法和offsetDay方法获取时间偏移(向前或向后)
Date date = DateUtil.date();
//获取三个小时前的时间,结果:2017-03-01 19:33:23
Date newDate = DateUtil.offsetHour(date, -3);
//获取三个小时后的时间
Date date = DateUtil.offsetHour(date,3);
//获取三天后的时间,结果:2017-03-04 22:33:23
Date newDate2 = DateUtil.offsetDay(date, 3);
//获取三天前的时间,结果:2017-03-04 22:33:23
Date newDate3 = DateUtil.offsetDay(date, -3);

//昨天
Date yesterday = DateUtil.yesterday();
//明天
Date tomorrow = DateUtil.tomorrow();
//上周
Date Date = DateUtil.lastWeek();
//下周
Date Date1 = DateUtil.nextWeek();
//上个月
Date Date2 = DateUtil.lastMonth();
//下个月
Date Date3 = DateUtil.nextMonth();
between方法获取日期天数差和小时差
String dateStr1 = "2017-03-01 22:33:23";
Date date1 = DateUtil.parse(dateStr1);
String dateStr2 = "2017-05-01 23:33:23";
Date date2 = DateUtil.parse(dateStr2);
String dateStr3 = "2017-03-02 20:23:35";
Date date3 = DateUtil.parse(dateStr3);

//两个时间段相差的天数
long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
//两个时间段相差的小时
long betweenHour = DateUtil.between(date1, date3, DateUnit.HOUR);

//格式化时间差,精确到分
String s = DateUtil.formatBetween(betweenDay, BetweenFormater.Level.MINUTE);
其他
 //年龄
int age = DateUtil.ageOfNow("1990-01-30");

//是否闰年
boolean leapYear = DateUtil.isLeapYear(2017);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值