我們在開發程式的時候,
常常會需要處理日期,
使用使用 java.util.Calendar 多少會讓開發者有些難處理,
所以因緣際會下我發現了Joda-Time 套件,
發現這套件易於使用是這套件主要的目的,
以下有幾個項目會將展示出其功能
<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.sample</groupId>
<artifactId>joda-time</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<joda-time.version>2.6</joda-time.version>
</properties>
<dependencies>
<!-- Joda-Time -->
<!-- http://www.ibm.com/developerworks/cn/java/j-jodatime.html -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
</dependencies>
</project>
package com.joda.example;
import java.util.Date;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
public class JodaTimeExample_1 {
/**
* 基本時間構成
*/
public static void main(String[] args) {
//取得系統時間
DateTime dt1 = new DateTime();
System.out.println("取得系統時間");
System.out.println(dt1);
System.out.println("--------------------------------------------------");
//通過java.util.Date對象生成
DateTime dt2 = new DateTime(new Date());
System.out.println("通過java.util.Date對象生成");
System.out.println(dt2);
System.out.println("--------------------------------------------------");
//指定年月日點分秒生成(參數依次是:年,月,日,時,分,秒,毫秒)
DateTime dt3 = new DateTime(2012, 5, 20, 13, 14, 0, 0);
System.out.println("指定年月日點分秒生成(參數依次是:年,月,日,時,分,秒,毫秒)");
System.out.println(dt3);
System.out.println("--------------------------------------------------");
//ISO8601形式生成
DateTime dt4 = new DateTime("2012-05-20");
DateTime dt5 = new DateTime("2012-05-20T13:14:00");
System.out.println("ISO8601形式生成");
System.out.println(dt4);
System.out.println(dt5);
System.out.println("--------------------------------------------------");
//只需要年月日的時候
LocalDate localDate = new LocalDate(new Date());
System.out.println("只需要年月日的時候");
System.out.println(localDate);
System.out.println("--------------------------------------------------");
//只需要時分秒毫秒的時候
LocalTime localTime = new LocalTime(new Date());
System.out.println("只需要時分秒毫秒的時候");
System.out.println(localTime);
System.out.println("--------------------------------------------------");
}
}
package com.joda.example;
import java.util.Locale;
import org.joda.time.DateTime;
public class JodaTimeExample_2 {
/**
* 取得年月日時分秒
*/
public static void main(String[] args) {
//取得系統時間
DateTime dt = new DateTime();
System.out.println("取得系統時間");
System.out.println(dt);
System.out.println("--------------------------------------------------");
System.out.println("取得系統年");
System.out.println(dt.getYear());
System.out.println("--------------------------------------------------");
System.out.println("取得系統月");
System.out.println(dt.getMonthOfYear());
System.out.println(dt.monthOfYear().getAsText());
System.out.println(dt.monthOfYear().getAsShortText(Locale.US));
System.out.println(dt.monthOfYear().getAsText(Locale.US));
System.out.println("--------------------------------------------------");
System.out.println("取得系統日");
System.out.println(dt.getDayOfMonth());
System.out.println("--------------------------------------------------");
System.out.println("取得系統星期");
System.out.println(dt.getDayOfWeek());
System.out.println(dt.dayOfWeek().getAsText());
System.out.println(dt.dayOfWeek().getAsShortText(Locale.US));
System.out.println(dt.dayOfWeek().getAsText(Locale.US));
System.out.println("--------------------------------------------------");
System.out.println("取得系統小時");
System.out.println(dt.getHourOfDay());
System.out.println("--------------------------------------------------");
System.out.println("取得系統分鐘");
System.out.println(dt.getMinuteOfHour());
System.out.println("--------------------------------------------------");
System.out.println("取得系統秒");
System.out.println(dt.getSecondOfMinute());
System.out.println("--------------------------------------------------");
System.out.println("取得系統毫秒");
System.out.println(dt.getMillisOfSecond());
System.out.println("--------------------------------------------------");
System.out.println("取得今年第幾週");
System.out.println(dt.getWeekOfWeekyear());
System.out.println("--------------------------------------------------");
System.out.println("取得今年第幾天");
System.out.println(dt.getDayOfYear());
System.out.println("--------------------------------------------------");
System.out.println("取得時區");
System.out.println(dt.getZone());
System.out.println("--------------------------------------------------");
System.out.println("是否閏年");
System.out.println(dt.year().isLeap());
System.out.println("--------------------------------------------------");
System.out.println("當月最大的日期");
System.out.println(dt.dayOfMonth().getMaximumValue());
}
}
package com.joda.example;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
public class JodaTimeExample_3 {
/**
* 星期的特殊處理
*/
public static void main(String[] args) {
// 取得系統時間
DateTime dt = new DateTime();
System.out.println("取得系統時間");
System.out.println(dt);
System.out.println("------------------------------------------------------");
switch (dt.getDayOfWeek()) {
case DateTimeConstants.SUNDAY:
System.out.println("星期日");
break;
case DateTimeConstants.MONDAY:
System.out.println("星期一");
break;
case DateTimeConstants.TUESDAY:
System.out.println("星期二");
break;
case DateTimeConstants.WEDNESDAY:
System.out.println("星期三");
break;
case DateTimeConstants.THURSDAY:
System.out.println("星期四");
break;
case DateTimeConstants.FRIDAY:
System.out.println("星期五");
break;
case DateTimeConstants.SATURDAY:
System.out.println("星期六");
break;
}
}
}
package com.joda.example;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import org.joda.time.DateTime;
public class JodaTimeExample_4 {
/**
* 與JDK日期物件轉換
*/
public static void main(String[] args) {
//取得系統時間
DateTime dt = new DateTime();
System.out.println("取得系統時間");
System.out.println(dt);
System.out.println("------------------------------------------------------");
//轉換成java.util.Date物件
System.out.println("轉換成java.util.Date物件");
Date d1 = new Date(dt.getMillis());
Date d2 = dt.toDate();
System.out.println(d1);
System.out.println(d2);
System.out.println("------------------------------------------------------");
//轉換成java.util.Calendar物件
System.out.println("轉換成java.util.Calendar物件");
Calendar c1 = Calendar.getInstance();
c1.setTimeInMillis(dt.getMillis());
Calendar c2 = dt.toCalendar(Locale.getDefault());
System.out.println(c1.getTime());
System.out.println(c2.getTime());
}
}
package com.joda.example;
import org.joda.time.DateTime;
public class JodaTimeExample_5 {
/**
* 日期前後推算
*/
public static void main(String[] args) {
//取得系統時間
DateTime dt = new DateTime();
System.out.println("取得系統時間");
System.out.println(dt);
System.out.println("------------------------------------------------------");
//昨天
System.out.println("昨天");
System.out.println(dt.minusDays(1));
//明天
System.out.println("明天");
System.out.println(dt.plusDays(1));
//1個月前
System.out.println("1個月前");
System.out.println(dt.minusMonths(1));
//1個月後
System.out.println("1個月後");
System.out.println(dt.plusMonths(1));
//2年前
System.out.println("2年前");
System.out.println(dt.minusYears(2));
//2年後
System.out.println("2年後");
System.out.println(dt.plusYears(2));
}
}
package com.joda.example;
import org.joda.time.DateTime;
public class JodaTimeExample_6 {
/**
* 取得特殊日期
*/
public static void main(String[] args) {
//取得系統時間
DateTime dt = new DateTime();
System.out.println("取得系統時間");
System.out.println(dt);
System.out.println("------------------------------------------------------");
//這個月的最後一天日期
System.out.println("這個月的最後一天日期");
System.out.println(dt.dayOfMonth().withMaximumValue());
System.out.println("------------------------------------------------------");
//90天後那個星期的星期一日期
System.out.println("90天後那個星期的星期一日期");
System.out.println(dt.plusDays(90).dayOfWeek().withMinimumValue());
}
}
package com.joda.example;
import org.joda.time.Chronology;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.chrono.CopticChronology;
public class JodaTimeExample_7 {
/**
* 時區
*/
public static void main(String[] args) {
//取得預設的時區
Chronology coptic = CopticChronology.getInstance();
System.out.println("取得預設的時區");
System.out.println(coptic.getZone());
System.out.println("------------------------------------------------------");
//日本時間
DateTimeZone.setDefault(DateTimeZone.forID("Asia/Tokyo"));
DateTime dt = new DateTime();
System.out.println("日本時間");
System.out.println(dt);
System.out.println("------------------------------------------------------");
//倫敦時間
DateTime dt1 = new DateTime(DateTimeZone.forID("Europe/London"));
System.out.println("倫敦時間");
System.out.println(dt1);
System.out.println("------------------------------------------------------");
//台灣時間
DateTime dt2 = new DateTime(DateTimeZone.forID("Asia/Taipei"));
System.out.println("台灣時間");
System.out.println(dt2);
System.out.println("------------------------------------------------------");
//台灣時區轉為日本時區
DateTime jDate1 = new DateTime(dt2, DateTimeZone.forOffsetHours(9));
DateTime jDate2 = new DateTime(dt2, DateTimeZone.forID("Asia/Tokyo"));
System.out.println("台灣時區轉為日本時區");
System.out.println(jDate1);
System.out.println(jDate2);
}
}
package com.joda.example;
import org.joda.time.DateTime;
public class JodaTimeExample_8 {
/**
* 日期比較
*/
public static void main(String[] args) {
DateTime d1 = new DateTime("2015-01-01");
DateTime d2 = new DateTime("2015-05-01");
//與系統時間比較
boolean b1 = d1.isAfterNow();
boolean b2 = d1.isBeforeNow();
boolean b3 = d1.isEqualNow();
System.out.println("與系統時間比較");
System.out.println(b1);
System.out.println(b2);
System.out.println(b3);
System.out.println("------------------------------------------------------");
//與其他時間比較
boolean f1 = d1.isAfter(d2);
boolean f2 = d1.isBefore(d2);
boolean f3 = d1.isEqual(d2);
int f4 = d1.compareTo(d2);
boolean f5 = d1.equals(d2);
System.out.println("與其他時間比較");
System.out.println(f1);
System.out.println(f2);
System.out.println(f3);
System.out.println(f4);
System.out.println(f5);
}
}
package com.joda.example;
import org.joda.time.DateTime;
public class JodaTimeExample_9 {
/**
* 日期格式化
*/
public static void main(String[] args) {
DateTime dateTime = new DateTime();
//default
System.out.println(dateTime);
System.out.println("------------------------------------------------------");
//yyyy/MM/dd hh:mm:ss.SSSa
System.out.println("yyyy/MM/dd hh:mm:ss.SSSa");
System.out.println(dateTime.toString("yyyy/MM/dd hh:mm:ss.SSSa"));
System.out.println("------------------------------------------------------");
//yyyy/MM/dd hh:mm:ss
System.out.println("yyyy/MM/dd hh:mm:ss");
System.out.println(dateTime.toString("yyyy/MM/dd hh:mm:ss"));
System.out.println("------------------------------------------------------");
//EEEE dd MMMM, yyyy HH:mm:ssa
System.out.println("EEEE dd MMMM, yyyy HH:mm:ssa");
System.out.println(dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa"));
System.out.println("------------------------------------------------------");
//yyyy/MM/dd HH:mm ZZZZ
System.out.println("yyyy/MM/dd HH:mm ZZZZ");
System.out.println(dateTime.toString("yyyy/MM/dd HH:mm ZZZZ"));
System.out.println("------------------------------------------------------");
//yyyy/MM/dd HH:mm Z
System.out.println("yyyy/MM/dd HH:mm Z");
System.out.println(dateTime.toString("yyyy/MM/dd HH:mm Z"));
}
}
package com.joda.example;
import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
public class JodaTimeExample_10 {
/**
* 計算區間
*/
public static void main(String[] args) {
DateTime begin = new DateTime("2015-01-01");
DateTime end = new DateTime("2015-05-01");
//計算區間毫秒數
Duration d = new Duration(begin, end);
System.out.println("計算區間毫秒數");
System.out.println(d.getMillis());
System.out.println("------------------------------------------------------");
//計算區間天數
Period p = new Period(begin, end, PeriodType.days());
System.out.println("計算區間天數");
System.out.println(p.getDays());
System.out.println("------------------------------------------------------");
//計算特定日期是否在該區間內
Interval i = new Interval(begin, end);
System.out.println("計算特定日期是否在該區間內");
System.out.println(i.contains(new DateTime("2015-03-01")));
System.out.println(i.contains(new DateTime("2014-03-01")));
}
}
- 首頁
- JAVA 目錄
- JAVA WEB 目錄
- JAVA 其他未分類 目錄
- Grails目錄
- jQuery 目錄
- Apache 目錄
- JavaScript 目錄
- Spring + Hibernate 目錄
- Hibernate 目錄
- Linux 目錄
- 程式開發工具目錄
- MySQL 目錄
- PHP目錄
- C/C++ 目錄
- Google App Engine 目錄
- HTML5/CSS3
- 程式開發基本資訊
- Android
- Oracle 目錄
- Struts 目錄
留言列表