zoukankan      html  css  js  c++  java
  • 【转】Java时间日期包 JodaTime

    maven:

    http://mvnrepository.com/artifact/joda-time/joda-time

    1, 获取每天的零点

    DateTime dt=new DateTime().withMillisOfDay(0);
    System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));//结果2016-09-09 00:00:00

    2, 在每天的6:30处理一些东西

    DateTime dt=new DateTime().withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
    System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));//结果2016-09-09 06:30:00

    3, 在每月的7号的6:30处理一些东西

    DateTime dt=new DateTime().withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
    System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));//结果2016-09-07 06:30:00

    4, 在每年的8月的7号的6:30处理一些东西

    DateTime dt=new DateTime().withMonthOfYear(8).withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
    System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));//结果2016-08-07 06:30:00

    5, 获取每个月的第一天和最后一天

    DateTime dateTime=new DateTime();
    System.out.println(dateTime.dayOfMonth().withMinimumValue().dayOfMonth().get());
    System.out.println(dateTime.dayOfMonth().withMaximumValue().dayOfMonth().get());//结果130

    6, 获取每天的零点的下一天零点

    DateTime dt=new DateTime().withMillisOfDay(0).plusDays(1);
    System.out.println(dt.toString("yyyy-MM-dd HH:mm:ss"));

    7, 获取日期差

    DateTime start=new DateTime(2015,5,4,12,20);
            DateTime start=new DateTime(2015,5,4,12,20);
            DateTime end=new DateTime(2015,5,6,3,10);
            Period period=new Period(start,end);
            System.out.println("month:"+period.getMonths());
            System.out.println("days:"+period.getDays());
            System.out.println("hours:"+period.getHours());
            System.out.println("minutes:"+period.getMinutes());
            System.out.println("second:"+period.getSeconds());//结果month:0days:1hours:14minutes:50second:0

    8, 单独获取天,小时,分钟,秒

    DateTime start=new DateTime(2015,5,4,12,20);
            DateTime end  =new DateTime(2015,5,5,12,00);
            System.out.println(Days.daysBetween(start,end).getDays());
            System.out.println(Hours.hoursBetween(start,end).getHours());
            System.out.println(Minutes.minutesBetween(start,end).getMinutes());
            System.out.println(Seconds.secondsBetween(start,end));

    9, 时间判断是否在某个范围以及获取时间差的毫秒

    DateTime start=new DateTime(2015,5,4,12,20);
            DateTime end  =new DateTime(2015,5,5,12,00);
            Interval interval=new Interval(start,end);
            System.out.println(interval.contains(new DateTime(2015,5,5,11,00)));
    
            Duration duration=new Duration(start,end);
            System.out.println(duration.getStandardHours());

    官网连接

  • 相关阅读:
    Spring Cloud学习笔记【二】Eureka 服务提供者/服务消费者(ribbon)
    Spring Cloud学习笔记【一】Eureka服务注册与发现
    Springboot分布式限流实践
    Springboot分布式锁实践(redis)
    Springboot2本地锁实践
    Springboot集成mybatis通用Mapper与分页插件PageHelper
    Springboot多数据源配置
    redis主从集群搭建
    assert的基本用法
    spring中BeanPostProcessor笔记
  • 原文地址:https://www.cnblogs.com/hark0623/p/5863739.html
Copyright © 2011-2022 走看看