zoukankan      html  css  js  c++  java
  • joda-time使用

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.yanguobin</groupId>
        <artifactId>jodatimeDemo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.10.5</version>
            </dependency>
        </dependencies>
    </project>

    main.java

    import org.joda.time.DateTime;
    import org.joda.time.Days;
    import org.joda.time.LocalDate;
    
    import java.util.Date;
    
    public class main {
        public static void main(String[] args) {
            //取得当前时间
            DateTime dateTime = new DateTime();
            System.out.println(dateTime);   //2019-12-15T15:51:47.187+08:00
    
            //转化成java.util.Date对象
            Date date = dateTime.toDate();
            System.out.println(date);   //Sun Dec 15 15:52:54 CST 2019
    
            //格式化时间
            String time1 = dateTime.toString("yyyy-MM-dd hh:mm:ss.SSSa");
            System.out.println(time1);  //2019-12-15 03:56:05.277下午
    
            //初始化时间
            DateTime dateTime2 = new DateTime(2019, 8, 23, 13,
                    9, 22, 444);
            System.out.println(dateTime2);  //2019-08-23T13:09:22.444+08:00
    
            //计算两个日期间隔的天数
            LocalDate start = new LocalDate(2018,4,5);
            LocalDate end = new LocalDate(2019, 4, 5);
            int days = Days.daysBetween(start, end).getDays();
            System.out.println(days);   //365
        }
    }
  • 相关阅读:
    Java学习:面向对象三大特征:封装、继承、多态之封装性
    Java学习:构造方法
    Java学习:集合双列Map
    斗地主综合案例:有序版本(双列)
    JDK9对集合添加的优化
    Java学习:Debug调试程序
    计算一个字符串中每一个字符出现的次数
    Java学习:Map接口
    Java学习:可变参数
    Pediy CTF 2018 reverse
  • 原文地址:https://www.cnblogs.com/yanguobin/p/12044480.html
Copyright © 2011-2022 走看看