大家好,又见面了,我是你们的朋友全栈君。
工作中经常用到java日期类型Date、Calendar或SimpleDateFormat,但是这些日期工具类要么提供了过期的方法(Date),要么就是功能不够强大。最近突然用到了Joda-Time日期工具,发现非常好用,就撰写此文,以便学习和交流。
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>
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 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()));
}
}
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
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/149550.html原文链接:https://javaforall.cn