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

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);  

 

 

}

 

}

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.hexiang.utils; import java.text.SimpleDateFormat; import java.util.*; public class CalendarUtil { public static void main(String args[]) { System.out.println("First day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByWeek(new Date()))); System.out.println("Last day of week is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByWeek(new Date()))); System.out.println("First day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getFirstDateByMonth(new Date()))); System.out.println("Last day of month is : " + new SimpleDateFormat("yyyy-MM-dd") .format(getLastDateByMonth(new Date()))); } /** * 获得所在星期的第一天 */ public static Date getFirstDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 now.set(Calendar.DATE, first_day_of_week); return now.getTime(); } /** * 获得所在星期的最后一天 */ public static Date getLastDateByWeek(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); int today = now.get(Calendar.DAY_OF_WEEK); int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一 int last_day_of_week = first_day_of_week + 6; // 星期日 now.set(Calendar.DATE, last_day_of_week); return now.getTime(); } /** * 获得所在月份的最后一天 * @param 当前月份所在的时间 * @return 月份的最后一天 */ public static Date getLastDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1); now.set(Calendar.DATE, 1); now.set(Calendar.DATE, now.get(Calendar.DATE) - 1); now.set(Calendar.HOUR, 11); now.set(Calendar.MINUTE, 59); now.set(Calendar.SECOND, 59); return now.getTime(); } /** * 获得所在月份的第一天 * @param 当前月份所在的时间 * @return 月份的第一天 */ public static Date getFirstDateByMonth(Date date) { Calendar now = Calendar.getInstance(); now.setTime(date); now.set(Calendar.DATE, 0); now.set(Calendar.HOUR, 12); now.set(Calendar.MINUTE, 0); now.set(Calendar.SECOND, 0); return now.getTime(); } }
Joda-Time 是一个 Java 编程语言的日期时间处理,它提供了简化日期时间操作的功能。使用 Joda-Time,你可以轻松地进行日期时间的计算、格式化、解析等操作。 下面是 Joda-Time 的一些常见用法: 1. 创建日期对象: ```java DateTime now = new DateTime(); // 创建当前日期时间对象 DateTime specificDate = new DateTime(2022, 1, 1, 0, 0, 0); // 创建指定日期时间对象 ``` 2. 获取日期时间的各个部分: ```java int year = now.getYear(); // 获取年份 int month = now.getMonthOfYear(); // 获取月份 int day = now.getDayOfMonth(); // 获取日期 int hour = now.getHourOfDay(); // 获取小时 int minute = now.getMinuteOfHour(); // 获取分钟 int second = now.getSecondOfMinute(); // 获取秒数 ``` 3. 格式化日期时间: ```java String formattedDate = now.toString("yyyy-MM-dd"); // 将日期格式化为指定格式的字符串 String formattedTime = now.toString("HH:mm:ss"); // 将时间格式化为指定格式的字符串 String formattedDateTime = now.toString("yyyy-MM-dd HH:mm:ss"); // 将日期时间格式化为指定格式的字符串 ``` 4. 解析字符串为日期对象: ```java DateTime parsedDate = DateTime.parse("2022-01-01"); // 解析字符串为日期对象 ``` 5. 对日期进行计算和操作: ```java DateTime modifiedDate = now.plusDays(7); // 将日期加上指定天数 DateTime result = now.minusYears(1).plusMonths(3); // 对日期进行复合操作 ``` 以上是 Joda-Time 的一些基本用法,你可以根据自己的需求进一步探索该工具类的其他功能。请注意,Joda-Time 在 Java 8 及更高版本中已经被官方的 java.time 包所取代,因此在使用新的 Java 版本时,你可以直接使用 java.time 包中的类来处理日期时间

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值