JodaTime时间处理工具使用详解

1. 说明:

工作中经常用到java日期类型Date、Calendar或SimpleDateFormat,但是这些日期工具类要么提供了过期的方法(Date),要么就是功能不够强大。最近突然用到了Joda-Time日期工具,发现非常好用,就撰写此文,以便学习和交流。

2. Maven依赖:

  
        joda-time
        joda-time
        2.9.7
  

3. 实战:

package com.joda.time.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by lance  on 2017/3/24.
 */
public class DateTimeFormatterTest {


    public static void main(String[] args) {


        DateTime dateTime = new DateTime();


        System.out.println("currentTimeMillis:" + System.currentTimeMillis());
        System.out.println("longDateTime:" + new DateTime().getMillis());


        // DateTime换java Date
        Date date = dateTime.toDate();
        // DateTime换java Calendar
        Calendar calendar = dateTime.toCalendar(Locale.CHINESE);


        System.out.println("year:" + dateTime.getYear());
        System.out.println("month:" + dateTime.getMonthOfYear());
        System.out.println("day:" + dateTime.getDayOfMonth());
        System.out.println("MinuteOfDay:" + dateTime.getMinuteOfDay());
        System.out.println("Hour:" + dateTime.getHourOfDay());
        System.out.println("Minute:" + dateTime.getMinuteOfHour());
        System.out.println("Second:" + dateTime.getSecondOfMinute());


        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy年MM月dd日 HH:mm:ss");
        System.out.println(format.print(dateTime.getMillis()));


        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy.MM.dd HH:mm:ss");
        System.out.println(format1.print(dateTime.getMillis()));


        DateTimeFormatter format2 = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss");
        System.out.println(format2.print(dateTime.getMillis()));
    }
}

4.输出结果:

currentTimeMillis:1496994136904
longDateTime:1496994136904
year:2017
month:6
day:9
MinuteOfDay:942
Hour:15
Minute:42
Second:16
2017年06月09日 15:42:16
2017.06.09 15:42:16
2017-06-09 03:42:16

你可能感兴趣的:(JodaTime时间处理工具使用详解)