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
    }
}
posted @ 2019-12-15 16:11  请叫我小老弟  阅读(1345)  评论(0编辑  收藏  举报