简化Java日期操作的开源项目DATE4J方法

  本文介绍简化Java日期操作的开源项目DATE4J的方法。

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


    Date and its subclasses :
  java.util.Date
  java.sql.Date
  java.sql.Timestamp
  The calendar and time zone classes :
  java.util.Calendar
  java.util.GregorianCalendar
  java.util.TimeZone
  java.util.SimpleTimeZone (for use with the Gregorian calendar only)
  The formatting and parsing classes :
  java.text.DateFormat
  java.text.SimpleDateFormat
  java.text.DateFormatSymbols
  以上这么多类用起来是不是很麻烦呢,现在好了,有了DATE4J,就不用这么麻烦了。

  下面是一些简单的实例: 


 view plaincopy to clipboardprint?
  //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(); //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);
  //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(); //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);
  获取DATE4J.jar请访问DATE4J项目网站:http://www.date4j.net

  本文来自博客:http://blog.csdn.net/softwave/archive/2010/05/06/5564724.aspx

你可能感兴趣的:(java,sql,.net,Blog)