joda-time使用教程
- 2019 年 10 月 5 日
- 筆記
版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。
本文鏈接:https://blog.csdn.net/qq_37933685/article/details/84977052
文章目錄
- joda-time使用教程
- 介紹
- 類總覽
- 環境
- 配置
- 簡單使用
joda-time使用教程
介紹
The Joda project provides quality low-level libraries for the Java platform. Joda項目為Java平台提供了高品質的低級庫。https://www.joda.org/ Joda-Time為Java日期和時間類提供了高品質的替代品。Joda-Time是Java SE 8之前Java的事實上*標準日期和時間庫。現在要求用戶遷移到
java.time
(JSR-310)。Joda-Time根據業務友好Apache 2.0許可證(https//www.joda.org/joda-time/licenses.html)獲得許可。https://www.joda.org/joda-time/ 官方文檔-快速開始
類總覽
- LocalDate – 沒有時間的日期
- LocalTime – 沒有日期的時間
- Instant – 時間線上的瞬時點
- DateTime – 帶時區的完整日期和時間
- DateTimeZone – 一個更好的時區
- Duration – 時間量 Interval – 兩個瞬間之間的時間
環境
IntelliJ IDEA 2018.2.7 (Ultimate Edition) JRE: 1.8.0_152-release-1248-b22 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o Windows 10 10.0
配置
使用maven導包
注意:jdk版本問題,這裡選用依賴jdk1.5的版本,即2.3版,jdk1.8選用更高版本吧,因為jdk1.8的java.time 裡面的api估摸著夠用了。
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> <version>2.4</version> </dependency>
簡單使用
- joda 轉 str
@Test public void testJodaToStr(){ DateTime dateTime = new DateTime(); String string = dateTime.toString(TIME_PATTERN); String string2 = dateTime.toString(TIME_PATTERN,Locale.CHINA); System.out.println(string+string2); }
- joda 轉 calendar
@Test public void testJodaToCalendar(){ DateTime dateTime = new DateTime(new Date()); Calendar calendar = dateTime.toCalendar(Locale.CHINA); System.out.println(calendar); }
- str 轉換為joda
@Test public void testStrToJodaDate(){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATETIME_PATTERN); DateTime parse = DateTime.parse("2018-12-11 17:06:30", dateTimeFormatter); System.out.println(parse); }
- date 轉 joda
@Test public void testDateToJodaDate(){ Date date = new Date(); DateTime dateTime = new DateTime(date); System.out.println(dateTime); }
- calendar 轉 joda
@Test public void testCalendarToJodaDate(){ Calendar instance = Calendar.getInstance(); DateTime dateTime = new DateTime(instance); System.out.println(dateTime); }
- date 轉 str
@Test public void testDateToStr(){ Date date = new Date(); SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATETIME_PATTERN); String format = simpleDateFormat.format(date); System.out.println(format); }
- string 轉 date
@Test public void testStrToDate(){ DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(DATE_PATTERN); String str="2018-12-07"; DateTime parse = DateTime.parse(str, dateTimeFormatter); Date date = parse.toDate(); System.out.println(date); }