锘??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲成人av在线电影,国产视频一区二区在线,国产69久久http://www.aygfsteel.com/kohri/category/51291.htmlzh-cnSat, 28 Apr 2012 21:33:41 GMTSat, 28 Apr 2012 21:33:41 GMT60JAVA 綆鍗曠殑鏃ユ湡灝佽綾?/title><link>http://www.aygfsteel.com/kohri/archive/2012/04/25/376635.html</link><dc:creator>kohri</dc:creator><author>kohri</author><pubDate>Wed, 25 Apr 2012 14:36:00 GMT</pubDate><guid>http://www.aygfsteel.com/kohri/archive/2012/04/25/376635.html</guid><wfw:comment>http://www.aygfsteel.com/kohri/comments/376635.html</wfw:comment><comments>http://www.aygfsteel.com/kohri/archive/2012/04/25/376635.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.aygfsteel.com/kohri/comments/commentRss/376635.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/kohri/services/trackbacks/376635.html</trackback:ping><description><![CDATA[鏈潵浠ヤ負鏈変簡date4j灝變竾浜嬫棤浼戜簡錛岀粨鏋滃湪宸ヤ綔鐨勬椂鍊欏彂瑙夋湁涓嶅皯鑴氭浠呬粎闇瑕佷袱涓変釜綆鍗曠殑class鎵ц涓涓嬪氨鍙互瀹屾垚浠誨姟浜嗐備篃灝辨槸璇村嵆浣挎槸date4j錛岀浉瀵逛簬榪欎袱涓変釜鐢氳嚦鏄竴涓猚lass鏉ヨ榪樻槸榪囦簬鑷冭偪浜嗐備簬鏄箮鑷繁鍐欎簡涓畝鍗曠殑鏃ユ湡灝佽綾匯?br />涓昏鍔熻兘鏄?<br />1.鑾峰彇褰撳墠鏃墮棿<br />2.鑾峰彇褰撳墠騫達紝鏈堬紝鏃ワ紝鏃訛紝鍒嗭紝縐?br />3.鑾峰彇鎸囧畾鏃ユ湡鐨勫勾錛屾湀錛屾棩錛屾椂錛屽垎錛岀<br />4.鑾峰彇涓や釜鏃ユ湡鐨勬椂闂村樊(鍖呮嫭騫存湀鏃ユ椂鍒嗙)<br />5.灝嗗瓧絎︾獪綾誨瀷杞垚java.util.date綾誨瀷<br />6.鎸囧畾鏃ユ湡娣誨姞鏃墮棿<br /><br /><div style="background-color:#eeeeee;font-size:13px;border:1px solid #CCCCCC;padding-right: 5px;padding-bottom: 4px;padding-left: 4px;padding-top: 4px;width: 98%;word-break:break-all"><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF; ">package</span> com.kohri.date;<br /><br /><span style="color: #0000FF; ">import</span> java.text.DateFormat;<br /><span style="color: #0000FF; ">import</span> java.text.ParseException;<br /><span style="color: #0000FF; ">import</span> java.text.SimpleDateFormat;<br /><span style="color: #0000FF; ">import</span> java.util.Calendar;<br /><span style="color: #0000FF; ">import</span> java.util.Date;<br /><span style="color: #0000FF; ">import</span> java.util.GregorianCalendar;<br /><br /><span style="color: #008000; ">/**</span><span style="color: #008000; "><br /> * @descriped a simple class for date<br /> * </span><span style="color: #808080; ">@author</span><span style="color: #008000; "> Kohri<br /> * @date 2012/4/22<br /> * </span><span style="color: #808080; ">@version</span><span style="color: #008000; "> 1.0<br /> </span><span style="color: #008000; ">*/</span><br /><span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">class</span> SimpleDate {<br /><br />    <span style="color: #0000FF; ">private</span> <span style="color: #0000FF; ">static</span> String defaultFormat = "yyyy-MM-dd HH:mm:ss";<br />    <span style="color: #0000FF; ">private</span> <span style="color: #0000FF; ">static</span> SimpleDateFormat sf = <span style="color: #0000FF; ">null</span> ;<br />    <span style="color: #0000FF; ">private</span> <span style="color: #0000FF; ">static</span> Calendar cal = Calendar.getInstance();<br />    <span style="color: #0000FF; ">private</span> <span style="color: #0000FF; ">static</span> Date date = <span style="color: #0000FF; ">null</span>;<br /><br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get current date time (default 'yyyy-MM-dd HH:mm:ss')<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> string<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> String getDateNow() {<br />        sf = <span style="color: #0000FF; ">new</span> SimpleDateFormat(defaultFormat);<br />        String currentTime = "";<br />        currentTime = sf.format(<span style="color: #0000FF; ">new</span> Date());<br />        <span style="color: #0000FF; ">return</span> currentTime;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get year (default now) <br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> int <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">int</span> getYear(){<br />        <span style="color: #0000FF; ">int</span> currentYear = cal.get(Calendar.YEAR);<br />        <span style="color: #0000FF; ">return</span> currentYear;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get mon (default now) <br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> int <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">int</span> getMonth(){<br />        <span style="color: #0000FF; ">int</span> currentMonth = cal.get(Calendar.MONTH) + 1;<br />        <span style="color: #0000FF; ">return</span> currentMonth;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get day of month (default now)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> int <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">int</span> getDay(){<br />        <span style="color: #0000FF; ">int</span> currentDayOfMonth = cal.get(Calendar.DAY_OF_MONTH);<br />        <span style="color: #0000FF; ">return</span> currentDayOfMonth;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get hours (default now)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> int <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">int</span> getHours(){<br />        <span style="color: #0000FF; ">int</span> currentHours = cal.get(Calendar.HOUR_OF_DAY);<br />        <span style="color: #0000FF; ">return</span> currentHours;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get  minutes (default now)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> int <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">int</span> getMinutes(){<br />        <span style="color: #0000FF; ">int</span> currentMinute = cal.get(Calendar.MINUTE);<br />        <span style="color: #0000FF; ">return</span> currentMinute;<br />    }<br /><br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get seconds (default now)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> int <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">int</span> getSeconds(){<br />        <span style="color: #0000FF; ">int</span> currentSecond = cal.get(Calendar.SECOND);<br />        <span style="color: #0000FF; ">return</span> currentSecond;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * string change to date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strDate<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> dateFormat<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> date<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> Date toDate(String strDate, String dateFormat){<br />        <span style="color: #0000FF; ">if</span>(strDate == <span style="color: #0000FF; ">null</span> || strDate.length() == 0){<br />            <span style="color: #0000FF; ">return</span> <span style="color: #0000FF; ">null</span>;<br />        }<br />        Date date = <span style="color: #0000FF; ">null</span>;<br />        DateFormat df = <span style="color: #0000FF; ">new</span> SimpleDateFormat(dateFormat);<br />        <span style="color: #0000FF; ">try</span> {<br />            date = df.parse(strDate);<br />        } <span style="color: #0000FF; ">catch</span> (ParseException e) {<br />            e.printStackTrace();<br />        }<br />        <span style="color: #0000FF; ">return</span> date;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * Returns this Calendar's time value in milliseconds<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> p_date<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> long<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">long</span> getMillisOfDate(Date date) {<br />           cal.setTime(date);<br />           <span style="color: #0000FF; ">return</span> cal.getTimeInMillis();<br />    }<br />    <br />    <br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * compare two date <br />     * return the greater date <br />     * if equals return null<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strStartDate<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strEndDate<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> date <br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> Date getGreaterDate(String strStartDate, String strEndDate){<br />        Date date = <span style="color: #0000FF; ">null</span>;<br />        Date startDate = toDate(strStartDate, "yyyy-MM-dd");<br />        Date endDate = toDate(strEndDate, "yyyy-MM-dd");<br />        <span style="color: #0000FF; ">long</span> startTime = getMillisOfDate(startDate);<br />        <span style="color: #0000FF; ">long</span> endTime = getMillisOfDate(endDate);<br />        <span style="color: #0000FF; ">if</span>((startTime - endTime) > 0){<br />            <span style="color: #0000FF; ">return</span> startDate;<br />        }<span style="color: #0000FF; ">else</span> <span style="color: #0000FF; ">if</span>((endTime - startTime) > 0){<br />            <span style="color: #0000FF; ">return</span> endDate;<br />        }<br />        <span style="color: #0000FF; ">return</span> date;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get days between 2 different date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strStartDate less date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strEndDate greater date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> long<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">long</span> getDaysOftwoDiffDate(String strStartDate, String strEndDate){<br />           Date startDate = toDate(strStartDate, "yyyy-MM-dd");<br />           Date endDate = toDate(strEndDate, "yyyy-MM-dd");<br />           <span style="color: #0000FF; ">long</span> startTime = getMillisOfDate(startDate);<br />           <span style="color: #0000FF; ">long</span> endTime = getMillisOfDate(endDate);<br />           <span style="color: #0000FF; ">long</span> betweenDays = (<span style="color: #0000FF; ">long</span>) ((endTime - startTime) / ( 1000 * 60 * 60 * 24 ));<br />           <span style="color: #0000FF; ">return</span> betweenDays;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get weeks between 2 different date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strStartDate less date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strEndDate greater date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> long<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">long</span> getWeeksOfTwoDiffDate(String strStartDate, String strEndDate){<br />        <span style="color: #0000FF; ">return</span> getDaysOftwoDiffDate(strStartDate, strEndDate) / 7;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get months between 2 different date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strStartDate less date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strEndDate greater date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> long<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">long</span> getMonthsOfTwoDiffDate(String strStartDate, String strEndDate){<br />        <span style="color: #0000FF; ">return</span> getDaysOftwoDiffDate(strStartDate, strEndDate) / 30;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * get years between 2 different date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strStartDate less date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> strEndDate greater date (yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> long<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">long</span> getYearsOfTwoDiffDate(String strStartDate, String strEndDate){<br />        <span style="color: #0000FF; ">return</span> getDaysOftwoDiffDate(strStartDate, strEndDate) / 365;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * add date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> date<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> count <br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> field Calendar.YEAR(MONTH<img src="http://www.aygfsteel.com/Images/dot.gif" alt="" />..)<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> format DateFormat(yyyy-MM-dd)<br />     * </span><span style="color: #808080; ">@return</span><span style="color: #008000; "> string<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> String addDate(Date date,<span style="color: #0000FF; ">int</span> count,<span style="color: #0000FF; ">int</span> field,String format){<br />           cal.setTime(date);<br />           <span style="color: #0000FF; ">int</span> year = getYear();<br />           <span style="color: #0000FF; ">int</span> month = getMonth() - 1;<br />           <span style="color: #0000FF; ">int</span> day = getDay();<br />           <span style="color: #0000FF; ">int</span> hours = getHours();<br />           <span style="color: #0000FF; ">int</span> minutes = getMinutes();<br />           <span style="color: #0000FF; ">int</span> seconds = getSeconds();<br />           Calendar calendar = <span style="color: #0000FF; ">new</span> GregorianCalendar(year, month, day, hours, minutes, seconds);<br />           calendar.add(field,count);<br />           DateFormat df = <span style="color: #0000FF; ">new</span> SimpleDateFormat(format);<br />           String tmpDate = df.format(calendar.getTime());<br />           if(date == null){<div><span style="white-space:pre"> </span>cal.setTime(new Date());</div><div><span style="white-space:pre"> </span>}else{</div><div><span style="white-space:pre"> </span>cal.setTime(date);</div><div><span style="white-space:pre"> </span>}</div>           <span style="color: #0000FF; ">return</span> tmpDate;<br />    }<br />    <br />    <span style="color: #008000; ">/**</span><span style="color: #008000; "><br />     * calendar settime<br />     * </span><span style="color: #808080; ">@param</span><span style="color: #008000; "> date<br />     </span><span style="color: #008000; ">*/</span><br />    <span style="color: #0000FF; ">private</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">void</span> setCalTime(Date date){<br />        <span style="color: #0000FF; ">if</span>(date != <span style="color: #0000FF; ">null</span>){<br />            cal.setTime(date);<br />        }<br />    }<br />    <br />    <span style="color: #008000; ">//</span><span style="color: #008000; ">setter getter</span><span style="color: #008000; "><br /></span><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> String getDefaultFormat() {<br />        <span style="color: #0000FF; ">return</span> defaultFormat;<br />    }<br /><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">void</span> setDefaultFormat(String defaultFormat) {<br />        SimpleDate.defaultFormat = defaultFormat;<br />    }<br /><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> Date getDate() {<br />        <span style="color: #0000FF; ">return</span> date;<br />    }<br /><br />    <span style="color: #0000FF; ">public</span> <span style="color: #0000FF; ">static</span> <span style="color: #0000FF; ">void</span> setDate(Date date) {<br />        SimpleDate.date = date;<br />        setCalTime(date);<br />    }<br />    <br /><br />}</div><img src ="http://www.aygfsteel.com/kohri/aggbug/376635.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/kohri/" target="_blank">kohri</a> 2012-04-25 22:36 <a href="http://www.aygfsteel.com/kohri/archive/2012/04/25/376635.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍏充簬date4j,綆綰︾殑鏃ユ湡澶勭悊搴?Java's Date Classes Must Die.)http://www.aygfsteel.com/kohri/archive/2012/04/12/373885.htmlkohrikohriWed, 11 Apr 2012 17:05:00 GMThttp://www.aygfsteel.com/kohri/archive/2012/04/12/373885.htmlhttp://www.aygfsteel.com/kohri/comments/373885.htmlhttp://www.aygfsteel.com/kohri/archive/2012/04/12/373885.html#Feedback0http://www.aygfsteel.com/kohri/comments/commentRss/373885.htmlhttp://www.aygfsteel.com/kohri/services/trackbacks/373885.html      鍦ㄧ啛鎮(zhèn)夊叕鍙鎬笟鍔′唬鐮佺殑鏃跺欑粡甯哥湅瑙佹湁鏃ユ湡澶勭悊錛坉ate錛?鐢變簬欏圭洰杞墜嬈℃暟杈冨錛屽湪榪欐柟闈㈡病鍚堢悊灝佽澶勭悊濂斤紝浜庢槸灝辨兂鑷繁灝佽涓涓猟ate綾匯傜劧鑰岃緱杞簡鍑犲ぉ鍗村彂瑙夊凡緇忔湁date4j鐨勫瓨鍦紝浜庢槸鍦ㄨ繖閲岀畝鍗曞湴浠嬬粛涓涓嬭繖涓棩鏈熺被搴撱備互涓嬪寘鎷嚜宸辯殑浠g爜銆佺綉涓婃祦浼犺祫鏂欍佷互鍙奷ate4j瀹樼綉渚嬪瓙銆?

-------------------------------------------------------------------------------------------------------------------------------------------------------------

java鏃ユ湡澶勭悊鐩稿叧綾伙細

java.util.Date 
java.sql.Date 
java.sql.Time 
java.sql.Timestamp 
java.util.Calendar 
java.util.TimeZone

     姣旇緝甯哥敤鐨勬槸java.util.date錛屽皢java.util.Date杞負java.sql.Date鐨勬椂鍊欙紝鏃ユ湡鏃跺垎縐掍細琚幓鎺夛紝澶卞幓綺懼害銆傝屼笖濡傛灉鐜板湪緲誨紑api鐪嬬湅灝卞彂瑙夎繖涓や釜綾誨ソ澶氭柟娉曞凡緇忚繃鏃訛紝鍑犺繎娌︿負搴熺墿銆?/p>

     Timestamp鑳藉拰java.util.date鏃犳崯杞崲錛屼絾鏄湪涓浜涢瀹氫箟sql涓細甯稿父鍑洪棶棰樸?/p>

錛堜互涓婂嚭鑷?nbsp;click me錛?/p>

--------------------------------------------------------------------------------------------------------------------------------------------------------------

Java鏈韓鐨勬棩鏈熺被鍦↗DK1.0鐗堟湰涔嬪悗灝卞啀涔熸病鏈夋洿鏂拌繃錛屽悓鏃惰繕瀛樺湪鐫涓浜涗紬鎵鍛ㄧ煡鐨勯棶棰橈紙渚嬪1鏈堜粠0寮濮嬶紝瀵艱嚧浜嗗緢澶氭湀浠藉樊涓鐨勬紡媧烇級銆備竴浠芥柊鐨凧ava瑙勮寖璇鋒眰錛圝SR錛孞ava Specification Request錛夊凡緇忚鎻愪氦錛岀洰鐨勫氨鏄瑙e喅涓婅堪闂錛屾鐗堟湰鐨勭被搴撲粛澶勫湪Alpha鐗堟湰銆傚湪鍏剁ǔ瀹氫箣鍓嶏紝寰堝寮鍙戣呰繕鏄細浣跨敤Joda Time綾誨簱錛岃綾誨簱涓嶫SR-310鐨勫弬鑰冨疄鐜扮被浼鹼紙浣嗕笉瀹屽叏鐩稿悓錛夈?Date4j涓哄湪Java涓鐞嗘棩鏈熸彁渚涗簡涓濂楁柊鐨勮В鍐蟲柟妗堬紝浣嗕笌Joda Time鎵鍏蟲敞鐨勮寖鍥村畬鍏ㄤ笉鍚屻傚姣斿涓嬶細

Joda TimeDate4j
鎷ユ湁鐨勭被鐨勬暟閲? 140+ 鎷ユ湁鐨勭被鐨勬暟閲?a style="color: #0b59b2; ">< 10
鍖呭惈鍙彉鍜屼笉鍙彉綾?/td> 浠呭寘鍚笉鍙彉綾?/td>
寮鴻皟閫熷害鍜屽姛鑳?/td> 寮鴻皟綆鍗曞拰鏈夋晥
鏀寔鏍奸噷楂橀噷鍘嗭紙Gregorian錛夈?縐戞櫘鐗硅鏃ュ巻錛圕optic錛夈?浼婃柉鍏版暀鍘嗭紙Islamic錛夈佷經(jīng)鍘嗭紙Buddhist錛夌瓑絳?/td> 鍙彁渚涘鏍奸噷楂橀噷鍘嗙殑鏀寔
鍙互瀹屽叏鍙栦唬JDK鏃ユ湡綾?/td> 鍜孞DK鏃ユ湡綾婚厤鍚堜嬌鐢?/td>
綺劇‘鍒版縐掔駭鎿嶄綔 鏀寔鍒扮撼縐掞紙鍗佷嚎鍒嗕箣涓縐掞級綰ф搷浣?/strong>
淇浜嗗ぉ“婧㈠嚭”鐨勯棶棰?/td> 澶?#8220;婧㈠嚭”鐨勯棶棰樺彲閰嶇疆
閽堝鐨勬槸閫氬父鎰忎箟鐨勬棩鏈熺淮鎶?/td> 閫傜敤浜庨氳繃鏁版嵁搴撴潵緇存姢鐨勬棩鏈?/td>
閲囩敤Apache 2.0鎺堟潈璁稿彲 閲囩敤BSD鎺堟潈璁稿彲

铏界劧涔嶄竴鐪婦ate4j鍙叿澶囦簡Joda涓竴閮ㄥ垎鐨勭壒鎬э紝浣嗗畠鏈変袱涓富瑕佺殑鐗圭偣鏄疛oda鎵涓嶅叿澶囩殑銆?/p>

棣栧厛錛孌ate4j鐨勫紑鍙戣?a target="_blank" style="color: #0b59b2; ">瀹gО綾誨簱涓嶅簲鑾悕鍏跺鍦板皢鏃ユ湡鎴柇銆侸oda鍙敮鎸佹縐掔駭鐨勭簿搴﹁屼笖鍦ㄥ皢鏉?a target="_blank" style="color: #0b59b2; ">鍙兘涔熶笉浼氭敼鍠?/a>銆備竴浜涙暟鎹簱涔熷凡緇忔湁浜嗘洿濂界殑瑙e喅鏂規(guī)銆傛瘮濡傛祦琛岀殑PostgreSQL鏁版嵁搴撳鏃墮棿鎴崇簿搴﹀氨宸茬粡鏀寔鍒?a target="_blank" style="color: #0b59b2; ">寰綰?/a>錛堢櫨涓囧垎涔嬩竴縐掞級銆侱ate4j鍙湪澶勭悊鏃ユ湡鏃跺綺懼害姣棤鎹熶激銆?/p>

絎簩涓壒寰佹槸鏃ユ湡“婧㈠嚭”鐨勯棶棰橈紝渚嬪鍚戞煇涓棩鏈熷鍔犱竴孌墊椂闂村悗錛屾棩鏈熻惤鍦ㄤ笅鏈堢殑鎯呭喌銆傛渶綆鍗曠殑渚嬪瓙灝辨槸鍦?鏈?1鏃ュ鍔犱竴涓湀鐨勮綆楋細

DateTime dt = new DateTime("2011-03-31"); 
DateTime result = dt.plusMonths(1); (鏈鏂扮増鏈鏂規(guī)硶搴旇宸茬粡琚垹闄わ紝鍙湁plus(...)涓巔lusDay())
System.out.println(result.toString());

褰撲嬌鐢↗oda Time鏃訛紝浼氳緭鍑?鏈?0鏃ワ紝浣嗚繖涔熻騫朵笉鏄綘鎯寵鐨勭粨鏋溿?/p>

閴翠簬榪欑涓嶇‘瀹氭э紝Date4j涓烘?zhèn)ㄦ彁渚涗?a target="_blank" style="color: #0b59b2; ">4縐嶉夋嫨錛?/p>
1. 絎竴澶?/td>
2. 鏈鍚庝竴澶╋紙涓嶫oda Time鐩稿悓錛?/td>
3. 鏃ユ湡欏哄歡
4. 鎶涘嚭寮傚父

浠ヤ笂杞嚜 click me
-----------------------------------------------------------------------------------------------------------------------------------------------------------

date4j瀹樼綉&&瀹炰緥錛?br />


import hirondelle.date4j.DateTime;
import hirondelle.date4j.DateTime.DayOverflow;

import java.util.TimeZone;

public class Date4jExamples {

    public static void main(String[] args) {
        Date4jExamples examples = new Date4jExamples();
        examples.currentDateTime();
        examples.ageIfBornOnCertainDate();
        examples.daysTillChristmas();
        examples.whenIs90DaysFromToday();
        examples.dateDifference();
        examples.whenIs3Months5DaysFromToday();
        examples.hoursDifferenceBetweenParisAndPerth();
        examples.weeksSinceStart();
        examples.timeTillMidnight();
        examples.imitateISOFormat();
        examples.firstDayOfThisWeek();
        examples.jdkDatesSuctorial();

    }

    private static void log(Object aMsg) {
        System.out.println(String.valueOf(aMsg));
    }

    /**   
     * 鑾峰彇褰撳墠鏃墮棿 what is the current date-time in the JRE's default time zone
     * ------------------------------------------------------------------------
     * Here are some practical examples of using the above formatting symbols:
     *
     *    Format                                  Output
     *    YYYY-MM-DD hh:mm:ss.fffffffff a     1958-04-09 03:05:06.123456789 AM
     *    YYYY-MM-DD hh:mm:ss.fff a             1958-04-09 03:05:06.123 AM
     *    YYYY-MM-DD                             1958-04-09
     *    hh:mm:ss.fffffffff                     03:05:06.123456789
     *    hh:mm:ss                             03:05:06
     *    YYYY-M-D h:m:s                         1958-4-9 3:5:6
     *    WWWW, MMMM D, YYYY                     Wednesday, April 9, 1958
     *    WWWW, MMMM D, YYYY |at| D a         Wednesday, April 9, 1958 at 3 AM
     *
     *----------------------------------------------------------------------
     * ---
    private void currentDateTime() {
        DateTime now = DateTime.now(TimeZone.getDefault());
        String result = now.format("YYYY-MM-DD hh:mm:ss");
        log("Current date-time in default time zone : " + result);
        // result Current date-time in default time zone : 2012-04-12 00:55:54
    }

    /**
     * 騫撮緞璁$畻 what's the age of someone born may 16,1995
     
*/
    private void ageIfBornOnCertainDate() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        DateTime birthdate = DateTime.forDateOnly(1995, 5, 16);
        int age = today.getYear() - birthdate.getYear();
        // getDayOfYear鑾峰彇璺濈騫村垵鐨勬誨ぉ鏁?/span>
        if (today.getDayOfYear() < birthdate.getDayOfYear()) {
            age = age - 1;
        }
        log("Age of someone born May 16, 1995 is : " + age);
        // result Age of someone born May 16, 1995 is : 16
    }

    /**
     * 鏃ユ湡鐩歌窛 How many days till the next December 25
     
*/
    private void daysTillChristmas() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        DateTime christmas = DateTime.forDateOnly(today.getYear(), 12, 25);
        int result = 0;
        if (today.isSameDayAs(christmas)) {
            // do nothing
        } else if (today.lt(christmas)) {
            result = today.numDaysFrom(christmas);
        } else if (today.gt(christmas)) {
            DateTime christmasNextYear = DateTime.forDateOnly(
                    today.getYear() + 1, 12, 25);
            result = today.numDaysFrom(christmasNextYear);
        }
        log("Number of days till Christmas : " + result);
        // result Number of days till Christmas : 257
    }

    /**
     * What day is 90 days from today
     
*/
    private void whenIs90DaysFromToday() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        log("90 days from today is : "
                + today.plusDays(90).format("YYYY-MM-DD"));
        // result 90 days from today is : 2012-07-11
    }

    /**
     * 鏃ユ湡鐩稿樊
     
*/
    private void dateDifference() {
        // DayOverflow.Abort result throw Exception
        
// DayOverflow.Spillover result 2011-05-01
        
// DayOverflow.LastDay result 2011-04-30
        
// DayOverflow.FirstDay result 2011-05-01
        
// public enum DayOverflow {
        
/** Coerce the day to the last day of the month. */
        
// LastDay,
       
 /** Coerce the day to the first day of the next month. */
        
// FirstDay,
        
/** Spillover the day into the next month. */
        
// Spillover,
       
/** Throw a RuntimeException. */
        
// Abort;
        
// }
        /**
         * 
@param aNumYears
         *            positive, required, in range 09999
         * 
@param aNumMonths
         *            positive, required, in range 09999
         * 
@param aNumDays
         *            positive, required, in range 09999
         * 
@param aNumHours
         *            positive, required, in range 09999
         * 
@param aNumMinutes
         *            positive, required, in range 09999
         * 
@param aNumSeconds
         *            positive, required, in range 09999 method plus(Integer
         *            aNumYears, Integer aNumMonths, Integer aNumDays, Integer
         *            aNumHours, Integer aNumMinutes, Integer aNumSeconds,
         *            DayOverflow aDayOverflow)
         * 
         
*/
        DateTime dt = new DateTime("2011-03-31");
        DateTime result = dt.plus(0, 1, 0, 0, 0, 0, DayOverflow.Spillover);
        log("date difference : " + result.toString());
        // result date difference : 2011-05-01 00:00:00
    }

    /** What day is 3 months and 5 days from today? */
    private void whenIs3Months5DaysFromToday() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        DateTime result = today.plus(0, 3, 5, 0, 0, 0,
                DateTime.DayOverflow.FirstDay);
        log("3 months and 5 days from today is : "
                + result.format("YYYY-MM-DD"));
        // result 3 months and 5 days from today is : 2012-07-17
    }

    /**
     * Current number of hours difference between Paris, France and Perth,
     * Australia.
     
*/
    private void hoursDifferenceBetweenParisAndPerth() {
        // this assumes the time diff is a whole number of hours; other styles
        
// are possible
        DateTime paris = DateTime.now(TimeZone.getTimeZone("Europe/Paris"));
        DateTime perth = DateTime.now(TimeZone.getTimeZone("Australia/Perth"));
        int result = perth.getHour() - paris.getHour();
        if (result < 0) {
            result = result + 24;
        }
        log("Numbers of hours difference between Paris and Perth : " + result);
        // result Numbers of hours difference between Paris and Perth : 6
    }

    /** How many weeks is it since Sep 6, 2010? */
    private void weeksSinceStart() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        DateTime startOfProject = DateTime.forDateOnly(2010, 9, 6);
        int result = today.getWeekIndex() - startOfProject.getWeekIndex();
        log("The number of weeks since Sep 6, 2010 : " + result);
        // result The number of weeks since Sep 6, 2010 : 83
    }

    /** How much time till midnight? */
    private void timeTillMidnight() {
        DateTime now = DateTime.now(TimeZone.getDefault());
        DateTime midnight = now.plusDays(1).getStartOfDay();
        long result = now.numSecondsFrom(midnight);
        log("This many seconds till midnight : " + result);
        // result This many seconds till midnight : 83046
    }

    /** Format using ISO style. */
    private void imitateISOFormat() {
        DateTime now = DateTime.now(TimeZone.getDefault());
        log("Output using an ISO format: " + now.format("YYYY-MM-DDThh:mm:ss"));
        // result Output using an ISO format: 2012-04-12T00:55:54
    }

    private void firstDayOfThisWeek() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        DateTime firstDayThisWeek = today; // start value
        int todaysWeekday = today.getWeekDay();
        int SUNDAY = 1;
        if (todaysWeekday > SUNDAY) {
            int numDaysFromSunday = todaysWeekday - SUNDAY;
            firstDayThisWeek = today.minusDays(numDaysFromSunday);
        }
        log("The first day of this week is : " + firstDayThisWeek);
        // result The first day of this week is : 2012-04-08
    }

    /** For how many years has the JDK date-time API been suctorial? */
    private void jdkDatesSuctorial() {
        DateTime today = DateTime.today(TimeZone.getDefault());
        DateTime jdkFirstPublished = DateTime.forDateOnly(1996, 1, 23);
        int result = today.getYear() - jdkFirstPublished.getYear();
        log("The number of years the JDK date-time API has been suctorial : "
                + result);
        // result The number of years the JDK date-time API has been suctorial :
        
// 16
    }
}


kohri 2012-04-12 01:05 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 沈丘县| 山阴县| 白沙| 南岸区| 介休市| 文昌市| 句容市| 玉龙| 当雄县| 阳江市| 通化市| 屯门区| 淮阳县| 雅江县| 固原市| 克东县| 淮滨县| 乐山市| 黄石市| 隆安县| 同仁县| 搜索| 洪泽县| 绥棱县| 淮阳县| 怀远县| 苏尼特右旗| 本溪市| 玛沁县| 绍兴县| 灵寿县| 贡觉县| 宁波市| 刚察县| 德庆县| 海原县| 唐海县| 察哈| 利津县| 宜良县| 青阳县|