简化Java日期操作的开源项目DATE4J 超级好用

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

以往要使用Java对时间日期进行操作,可能会用到以下的一些类:

Date and its subclasses :

The calendar and time zone classes :

The formatting and parsing classes :

以上这么多类用起来是不是很麻烦呢,现在好了,有了DATE4J,就不用这么麻烦了。

下面是一些简单的实例:

  1. //Examples  
  2. //Here are some quick examples of using date4j's DateTime class :  
  3. DateTime dateAndTime = new DateTime("2010-01-19 23:59:59");  
  4. DateTime dateAndTime = new DateTime("2010-01-19 23:59:59.123456789");  
  5. DateTime dateOnly = new DateTime("2010-01-19");  
  6. DateTime timeOnly = new DateTime("23:59:59");  
  7. DateTime dateOnly = DateTime.forDateOnly(2010,01,19);  
  8. DateTime timeOnly = DateTime.forTimeOnly(23,59,59,0);  
  9. DateTime dt = new DateTime("2010-01-15 13:59:15");  
  10. boolean leap = dt.isLeapYear(); //false  
  11. dt.getNumDaysInMonth(); //31  
  12. dt.getStartOfMonth(); //2010-01-01, 00:00:00.000000000  
  13. dt.getEndOfDay(); //2010-01-15, 23:59:59.999999999  
  14. dt.format("YYYY-MM-DD"); //formats as '2010-01-15'  
  15. dt.plusDays(30); //30 days after Jan 15  
  16. dt.numDaysFrom(someDate); //returns an int  
  17. dueDate.lt(someDate); //less-than  
  18. dueDate.lteq(someDate); //less-than-or-equal-to  
  19. //Although DateTime carries no TimeZone information internally, there are methods that take a TimeZone as a parameter :  
  20. DateTime now = DateTime.now(someTimeZone);  
  21. DateTime today = DateTime.today(someTimeZone);  
  22. DateTime fromMilliseconds = DateTime.forInstant(31313121L, someTimeZone);  
  23. birthday.isInFuture(someTimeZone);  
  24. dt.changeTimeZone(fromOneTimeZone, toAnotherTimeZone);  
//Examples//Here are some quick examples of using date4j's DateTime class :DateTime dateAndTime = new DateTime("2010-01-19 23:59:59");DateTime dateAndTime = new DateTime("2010-01-19 23:59:59.123456789");DateTime dateOnly = new DateTime("2010-01-19");DateTime timeOnly = new DateTime("23:59:59");DateTime dateOnly = DateTime.forDateOnly(2010,01,19);DateTime timeOnly = DateTime.forTimeOnly(23,59,59,0);DateTime dt = new DateTime("2010-01-15 13:59:15");boolean leap = dt.isLeapYear(); //falsedt.getNumDaysInMonth(); //31dt.getStartOfMonth(); //2010-01-01, 00:00:00.000000000dt.getEndOfDay(); //2010-01-15, 23:59:59.999999999dt.format("YYYY-MM-DD"); //formats as '2010-01-15'dt.plusDays(30); //30 days after Jan 15dt.numDaysFrom(someDate); //returns an intdueDate.lt(someDate); //less-thandueDate.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);

 

获取DATE4J.jar请访问DATE4J项目网站:http://www.date4j.net

           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 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(); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值