<table style="color:rgb(68, 68, 68);"><tbody><tr><td><div><div><p>org.apache.commons.lang.time。这个包里面包含了如下5个类:</p><p> </p><p><span style="color:navy;">DateFormatUtils – </span><span style="color:navy;">提供格式化日期和时间的功能及相关常量;</span></p><p><span style="color:navy;">DateUtils – </span><span style="color:navy;">在</span><span style="color:navy;">Calendar</span><span style="color:navy;">和</span><span style="color:navy;">Date</span><span style="color:navy;">的基础上提供更方便的访问;</span></p><p><span style="color:navy;">DurationFormatUtils – </span><span style="color:navy;">提供格式化时间跨度的功能及相关常量;</span></p><p><span style="color:navy;">FastDateFormat – </span><span style="color:navy;">为</span><span style="color:navy;">java.text.SimpleDateFormat</span><span style="color:navy;">提供一个的线程安全的替代类;</span></p><p><span style="color:navy;">StopWatch – </span><span style="color:navy;">是一个方便的计时器。</span></p><p> </p><p>我们还是在一个例子中来看上述各个类的用法吧:</p><p> </p><p><strong><span style="color:navy;">package</span></strong> sean.study.jakarta.commons.lang;</p><p> </p><p><strong><span style="color:navy;">import</span></strong> java.util.Calendar;</p><p><strong><span style="color:navy;">import</span></strong> java.util.Date;</p><p> </p><p><strong><span style="color:navy;">import</span></strong> org.apache.commons.lang.StringUtils;</p><p><strong><span style="color:navy;">import</span></strong> org.apache.commons.lang.time.DateFormatUtils;</p><p><strong><span style="color:navy;">import</span></strong> org.apache.commons.lang.time.DateUtils;</p><p><strong><span style="color:navy;">import</span></strong> org.apache.commons.lang.time.FastDateFormat;</p><p><strong><span style="color:navy;">import</span></strong> org.apache.commons.lang.time.StopWatch;</p><p> </p><p><strong><span style="color:navy;">public</span> <span style="color:navy;">class</span></strong> DateTimeUsage {</p><p> </p><p> <strong><span style="color:navy;">public</span> <span style="color:navy;">static</span> <span style="color:navy;">void</span></strong> main(String[] args) {</p><p> demoDateUtils();</p><p> demoStopWatch();</p><p> }</p><p> </p><p> <strong><span style="color:navy;">public</span> <span style="color:navy;">static</span> <span style="color:navy;">void</span></strong> demoDateUtils() {</p><p> System.out.println(StringUtils.center(<span style="color:blue;">" demoDateUtils "</span>, 30, <span style="color:blue;">"="</span>));</p><p> Date date = <strong><span style="color:navy;">new</span></strong> Date();</p><p> String isoDateTime = DateFormatUtils.ISO_DATETIME_FORMAT.format(date);</p><p> String isoTime = DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(date);</p><p> FastDateFormat fdf = FastDateFormat.getInstance(<span style="color:blue;">"yyyy-MM"</span>);</p><p> String customDateTime = fdf.format(date);</p><p> System.out.println(<span style="color:blue;">"ISO_DATETIME_FORMAT: "</span> + isoDateTime);</p><p> System.out.println(<span style="color:blue;">"ISO_TIME_NO_T_FORMAT: "</span> + isoTime);</p><p> System.out.println(<span style="color:blue;">"Custom FastDateFormat: "</span> + customDateTime);</p><p> System.out.println(<span style="color:blue;">"Default format: "</span> + date);</p><p> System.out.println(<span style="color:blue;">"Round HOUR: "</span> + DateUtils.round(date, Calendar.HOUR));</p><p> System.out.println(<span style="color:blue;">"Truncate HOUR: "</span> + DateUtils.truncate(date, Calendar.HOUR));</p><p> System.out.println();</p><p> }</p><p> </p><p> <strong><span style="color:navy;">public</span> <span style="color:navy;">static</span> <span style="color:navy;">void</span></strong> demoStopWatch() {</p><p> System.out.println(StringUtils.center(<span style="color:blue;">" demoStopWatch "</span>, 30, <span style="color:blue;">"="</span>));</p><p> StopWatch sw = new StopWatch();</p><p> sw.start();</p><p> operationA();</p><p> sw.stop();</p><p> System.out.println(<span style="color:blue;">"operationA used "</span> + sw.getTime() + <span style="color:blue;">" milliseconds."</span>);</p><p> System.out.println();</p><p> }</p><p> </p><p> <strong><span style="color:navy;">public</span> <span style="color:navy;">static</span> <span style="color:navy;">void</span></strong> operationA() {</p><p> <strong><span style="color:navy;">try</span></strong> {</p><p> Thread.sleep(999);</p><p> }</p><p> <strong><span style="color:navy;">catch</span></strong> (InterruptedException e) {</p><p><span style="color:green;"> // do nothing</span></p><p> }</p><p> }</p><p> </p><p>}</p><p> </p><p>以下是运行结果:</p><p> </p><p align="left"><span style="color:navy;">======= demoDateUtils ========</span></p><p align="left"><span style="color:navy;">ISO_DATETIME_FORMAT: 2005-08-01T12:41:51</span></p><p align="left"><span style="color:navy;">ISO_TIME_NO_T_FORMAT: 12:41:51</span></p><p align="left"><span style="color:navy;">Custom FastDateFormat: 2005-08</span></p><p align="left"><span style="color:navy;">Default format: Mon Aug 01 12:41:51 CST 2005</span></p><p align="left"><span style="color:navy;">Round HOUR: Mon Aug 01 13:00:00 CST 2005</span></p><p align="left"><span style="color:navy;">Truncate HOUR: Mon Aug 01 12:00:00 CST 2005</span></p><p align="left"><span style="color:navy;"> </span></p><p align="left"><span style="color:navy;">======= demoStopWatch ========</span></p><p align="left"><span style="color:navy;">operationA used 1000 milliseconds.</span></p></div></div></td></tr></tbody></table>
org.apache.commons.lang.time.DateUtils(常用的一些函数) static java.util.Date addDays(java.util.Date date,int amount) //Adds a number of days to a date returning a new object. //一个日期对象+n天 static java.util.Date addHours(java.util.Date date,int amount) //Adds a number of hours to a date returning a new object. //一个日期对象+n小时 static java.util.Date addMilliseconds(java.util.Date date,int amount) //Adds a number of milliseconds to a date returning a new object. //一个日期对象+n毫秒 static java.util.Date addMinutes(java.util.Date date,int amount) //Adds a number of minutes to a date returning a new object. //一个日期对象+n分 static java.util.Date addMonths(java.util.Date date,int amount) //Adds a number of months to a date returning a new object. //一个日期对象+n月 static java.util.Date addSeconds(java.util.Date date,int amount) //Adds a number of seconds to a date returning a new object. //一个日期对象+n秒 static java.util.Date addWeeks(java.util.Date date,int amount) //Adds a number of weeks to a date returning a new object. //一个日期对象+N个星期 static java.util.Date addYears(java.util.Date date,int amount) //Adds a number of years to a date returning a new object. //一个日期对象+N年 static boolean isSameDay(java.util.Date date1,java.util.Date date2) //Checks if two date objects are on the same day ignoring time. //两个日期对象是否相等(只比较年-月-日) static boolean isSameInstant(java.util.Date date1,java.util.Date date2) //Checks if two date objects represent the same instant in time. //比较两个日期是否完全相等(精确到毫秒) static java.util.Date parseDate(java.lang.String str,java.lang.String[] parsePatterns) //Parses a string representing a date by trying a variety of different parsers. //例 1: String[] format={"yyyy-MM-dd"}; System.out.println(DateUtils.parseDate("2009-10-20",format)); 2: new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.addMilliseconds(new Date(), 7000))
时间: 2024-11-03 13:57:18