当前位置: 首页 > 工具软件 > date4j > 使用案例 >

Joda-Time&Date4j使用研究-开源JAVA日期时间处理类库

太叔景曜
2023-12-01

1)Joda-Time简介

Joda-Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time。可以利用它把JDK Date和Calendar类完全替换掉,而且仍然能够提供很好的集成。

Joda-Time主要的特点包括:

1. 易于使用:Calendar让获取"正常的"的日期变得很困难,使它没办法提供简单的方法,而Joda-Time能够 直接进行访问域并且索引值1就是代表January。
2. 易于扩展:JDK支持多日历系统是通过Calendar的子类来实现,这样就显示的非常笨重而且事实 上要实现其它日历系统是很困难的。Joda-Time支持多日历系统是通过基于Chronology类的插件体系来实现。
3. 提供一组完整的功能:它打算提供 所有关系到date-time计算的功能.Joda-Time当前支持6种日历系统,而且在将来还会继续添加。有着比JDK Calendar更好的整体性能等等。

 

2)代码实现如下:

package test;

 

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

import org.joda.time.DateTime;

import org.joda.time.LocalDate;

import org.joda.time.LocalTime;

 

/**

 * @author gaozzsoft

 *

 */

public class MyJodaTimeTest {

 

/**

* @param args

*/

public static void main(String[] args) {

//1

Calendar calendar = Calendar.getInstance();

calendar.set(2000, Calendar.JANUARY, 1, 0, 0, 0);

 

DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 0, 0);

System.out.println(dateTime.toString("E MM/dd/yyyy HH:mm:ss.SSS"));

 

 

//2 

Calendar calendar2 = Calendar.getInstance();

calendar2.set(2000, Calendar.JANUARY, 1, 0, 0, 0);

SimpleDateFormat sdf =

 new SimpleDateFormat("E MM/dd/yyyy HH:mm:ss.SSS");

calendar2.add(Calendar.DAY_OF_MONTH, 90);

System.out.println(sdf.format(calendar2.getTime()));

 

 

DateTime dateTime2 = new DateTime(2000, 1, 1, 0, 0, 0, 0);

System.out.println(dateTime2.plusDays(90).toString("E MM/dd/yyyy HH:mm:ss.SSS"));

 

DateTime dateTime3 = new DateTime(2000, 1, 1, 0, 0, 0, 0);

System.out.println(dateTime3.plusDays(45).plusMonths(1).dayOfWeek()

 .withMaximumValue().toString("E MM/dd/yyyy HH:mm:ss.SSS"));

 

//3

Calendar calendar3 = Calendar.getInstance();

DateTime dateTime4 = new DateTime(2000, 1, 1, 0, 0, 0, 0);

System.out.println(dateTime4.plusDays(45).plusMonths(1).dayOfWeek()

 .withMaximumValue().toString("E MM/dd/yyyy HH:mm:ss.SSS"));

calendar3.setTime(dateTime4.toDate());

System.out.println(calendar3.getTime());

 

 

//4

DateTime dateTime5 = new DateTime();

//DateTime dateTime6 = SystemFactory.getClock().getDateTime();

 

DateTime dateTime7 = new DateTime(

 2000, //year

 1,    // month

 1,    // day

 0,    // hour (midnight is zero)

 0,    // minute

 0,    // second

 0     // milliseconds

);

 

//java.util.Date jdkDate = obtainDateSomehow();

//long timeInMillis = jdkDate.getTime();

//DateTime dateTime = new DateTime(timeInMillis);

 

 

//java.util.Date jdkDate = obtainDateSomehow();

//dateTime = new DateTime(jdkDate);

 

//5

 

// Use a Calendar

//java.util.Calendar calendar4 = obtainCalendarSomehow();

//dateTime = new DateTime(calendar4);

// Use another Joda DateTime

//DateTime anotherDateTime = obtainDateTimeSomehow();

//dateTime = new DateTime(anotherDateTime);

// Use a String (must be formatted properly)

String timeString = "2006-01-26T13:30:00-06:00";

dateTime = new DateTime(timeString);

timeString = "2006-01-26";

DateTime dateTime8 = new DateTime(timeString);

System.out.println(dateTime8.toString("E MM/dd/yyyy HH:mm:ss"));

 

//6

//LocalDate localDate1 = SystemFactory.getClock().getLocalDate();

LocalDate localDate2 = new LocalDate(2009, 9, 6);// September 6, 2009

//LocalTime localTime1 = SystemFactory.getClock().getLocalTime();

LocalTime localTime2 = new LocalTime(13, 30, 26, 0);// 1:30:26PM

 

 

//计算 11 月中第一个星期一之后的第一个星期二

//LocalDate now = SystemFactory.getClock().getLocalDate();

//LocalDate electionDate = now.monthOfYear()

// .setCopy(11)        // November

// .dayOfMonth()       // Access Day Of Month Property

// .withMinimumValue() // Get its minimum value

// .plusDays(6)        // Add 6 days

// .dayOfWeek()        // Access Day Of Week Property

// .setCopy("Monday")  // Set to Monday (it will round down)

// .plusDays(1);       // Gives us Tuesday

//

//

//LocalDate now = SystemFactory.getClock().getLocalDate();

//LocalDate lastDayOfPreviousMonth =\

//  now.minusMonths(1).dayOfMonth().withMaximumValue(); 

//

//DateTime now = SystemFactory.getClock().getDateTime();

//DateTime then = now.plusWeeks(2);

//

//DateTime now = SystemFactory.getClock().getDateTime();

//DateTime tomorrow = now.plusDays(1);

//DateTime then = tomorrow.plusDays(90);

//

//DateTime now = SystemFactory.getClock().getDateTime();

//DateTime then = now.plusSeconds(156);

 

 

//DateTime now = SystemFactory.getClock().getDateTime();

//DateTime then = now.minusYears(5) // five years ago

//               .monthOfYear()     // get monthOfYear property

//               .setCopy(2)        // set it to February

//               .dayOfMonth()      // get dayOfMonth property

//               .withMaximumValue();// the last day of the month

 

 

//DateTime dateTime = SystemFactory.getClock().getDateTime();

//Calendar calendar = dateTime.toCalendar(Locale.getDefault());

//Date date = dateTime.toDate();

//DateMidnight dateMidnight = SystemFactory.getClock()

//  .getDateMidnight();

//date = dateMidnight.toDate();

 

 

//LocalDate localDate = SystemFactory.getClock().getLocalDate();

//Date date = localDate.toDateMidnight().toDate();

 

 

//DateTime dateTime = SystemFactory.getClock().getDateTime();

//dateTime.toString(ISODateTimeFormat.basicDateTime());

//dateTime.toString(ISODateTimeFormat.basicDateTimeNoMillis());

//dateTime.toString(ISODateTimeFormat.basicOrdinalDateTime());

//dateTime.toString(ISODateTimeFormat.basicWeekDateTime());

 

 

//DateTime dateTime = SystemFactory.getClock().getDateTime();

//dateTime.toString("MM/dd/yyyy hh:mm:ss.SSSa");

//dateTime.toString("dd-MM-yyyy HH:mm:ss");

//dateTime.toString("EEEE dd MMMM, yyyy HH:mm:ssa");

//dateTime.toString("MM/dd/yyyy HH:mm ZZZZ");

//dateTime.toString("MM/dd/yyyy HH:mm Z");

//

//09/06/2009 02:30:00.000PM

//06-Sep-2009 14:30:00

//Sunday 06 September, 2009 14:30:00PM

//09/06/2009 14:30 America/Chicago

//09/06/2009 14:30 -0500

 

 

}

 

}

(二)date4j是一个用于简化日期和时间操作的Java工具。可以替换java.util.Date。

代码实现如下:

package test;

 

import hirondelle.date4j.DateTime;

 

/**

 * @author gaozzsoft

 *

 */

public class MyDate4jTest {

 

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//Examples  

//Here are some quick examples of using date4j's DateTime class :  

DateTime dateAndTime = new DateTime("2010-01-19 23:59:59");  

DateTime dateAndTime2 = new DateTime("2010-01-19 23:59:59.123456789");  

DateTime dateOnly = new DateTime("2010-01-19");  

DateTime timeOnly = new DateTime("23:59:59");  

DateTime dateOnly2 = DateTime.forDateOnly(2010,01,19);  

DateTime timeOnly2 = DateTime.forTimeOnly(23,59,59,0);  

DateTime dt = new DateTime("2010-01-15 13:59:15");  

boolean leap = dt.isLeapYear(); //false  

dt.getNumDaysInMonth(); //31  

dt.getStartOfMonth(); //2010-01-01, 00:00:00.000000000  

dt.getEndOfDay(); //2010-01-15, 23:59:59.999999999  

dt.format("YYYY-MM-DD"); //formats as '2010-01-15'  

dt.plusDays(30); //30 days after Jan 15  

//dt.numDaysFrom(someDate); //returns an int  

//dueDate.lt(someDate); //less-than  

//dueDate.lteq(someDate); //less-than-or-equal-to  

//Although DateTime carries no TimeZone information internally, there are methods that take a TimeZone as a parameter :  

//DateTime now = DateTime.now(someTimeZone);  

//DateTime today = DateTime.today(someTimeZone);  

//DateTime fromMilliseconds = DateTime.forInstant(31313121L, someTimeZone);  

//birthday.isInFuture(someTimeZone);  

//dt.changeTimeZone(fromOneTimeZone, toAnotherTimeZone);  

 

 

}

 

}

 

 

 

 

 

 

 类似资料: