Java中LocalDateTime类型相关操作

LocalDateTime类型相关操作

.

  1. LocalDateTime 与 String 的相互转换

    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    LocalDateTime time = LocalDateTime.now();
    	
    String localTime = df.format(time);
    System.out.println("LocalDateTime转成String类型的时间:"+localTime);
    	
    LocalDateTime ldt = LocalDateTime.parse("2018-01-12 17:07:05",df);
    System.out.println("String类型的时间转成LocalDateTime:"+ldt);
    
  2. 比较

    //获取当前时间
    LocalDateTime nowTime= LocalDateTime.now();
    
    //自定义时间
    LocalDateTime endTime = LocalDateTime.of(2017, 10, 22, 10, 10, 10);
    
    //比较  现在的时间 比 设定的时间 之前  返回的类型是Boolean类型  true
    System.out.println(nowTime.isAfter(endTime));
    
    //比较   现在的时间 比 设定的时间 之后  返回的类型是Boolean类型  false
    System.out.println(nowTime.isBefore(endTime));
    
    //比较   现在的时间 和 设定的时候  相等  返回类型是Boolean类型  false
    System.out.println(nowTime.equals(endTime));
    
  3. 获取年月日时分秒

    LocalDateTime newLDT = LocalDateTime.now();
    
    int year = newLDT.getYear();//年
    int month = newLDT.getMonth().getValue();//月
    int dayOfMonth = newLDT.getDayOfMonth();//日
    int hour = newLDT.getHour();//时
    int minute = newLDT.getMinute();//分
    int second = newLDT.getSecond();//秒
    
  4. 相差天数和月份

    public class Demo {
        public static void main(String[] args) throws ParseException {
    
            LocalDateTime of1 = LocalDateTime.of(2019, 9, 25, 1, 1);//2018-9-25 01:01
            LocalDateTime of2 = LocalDateTime.of(2020, 9, 25, 23, 16); //2019-9-25 23:16
            System.out.println(monthDiff(of1,of2));//相差月份 12
            System.out.println(dateDiff(of1,of2));//相差天数 365
        }
    
        /**
         * 计算两个时间点的天数差
         * @param dt1 第一个时间点
         * @param dt2 第二个时间点
         * @return int,即要计算的天数差
         */
        public static int dateDiff(LocalDateTime dt1,LocalDateTime dt2){
            //获取第一个时间点的时间戳对应的秒数
            long t1 = dt1.toEpochSecond(ZoneOffset.ofHours(0));
            //获取第一个时间点在是1970年1月1日后的第几天
            long day1 = t1 /(60*60*24);
            //获取第二个时间点的时间戳对应的秒数
            long t2 = dt2.toEpochSecond(ZoneOffset.ofHours(0));
            //获取第二个时间点在是1970年1月1日后的第几天
            long day2 = t2/(60*60*24);
            //返回两个时间点的天数差
            return (int)(day2 - day1);
        }
    
        /**
         * 获取两个时间点的月份差
         * @param dt1 第一个时间点
         * @param dt2 第二个时间点
         * @return int,即需求的月数差
         */
        public static int monthDiff(LocalDateTime dt1,LocalDateTime dt2){
            //获取第一个时间点的月份
            int month1 = dt1.getMonthValue();
            //获取第一个时间点的年份
            int year1 = dt1.getYear();
            //获取第二个时间点的月份
            int month2 = dt2.getMonthValue();
            //获取第二个时间点的年份
            int year2 = dt2.getYear();
            //返回两个时间点的月数差
            return (year2 - year1) *12 + (month2 - month1);
        }
    
    }
    
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值