博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中关于时间日期操作的常用函数
阅读量:4069 次
发布时间:2019-05-25

本文共 20797 字,大约阅读时间需要 69 分钟。

<p><span style="font-weight: bold;">1.计算某一月份的最大天数</span><br>Calendar time=Calendar.getInstance(); <br>time.clear(); <br>time.set(Calendar.YEAR,year); <br>time.set(Calendar.MONTH,i-1);//注意,Calendar对象默认一月为0 <br><span style="color: #ff3300;">int day=time.getActualMaximum(Calendar.DAY_OF_MONTH)</span>;//本月份的天数<br><span style="color: #0000ff;">注:在使用set方法之前,必须先clear一下,否则很多信息会继承自系统当前时间</span><br><span style="font-weight: bold;">2.Calendar和Date的转化</span><br>(1) Calendar转化为Date<br>Calendar cal=Calendar.getInstance();<br>Date date=cal.getTime();<br>(2) Date转化为Calendar<br>Date date=new Date();<br>Calendar cal=Calendar.getInstance();<br>cal.setTime(date);<br><span style="font-weight: bold;">3.格式化输出日期时间</span><br>Date date=new Date();<br>SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");<br>System.out.println(df.format(date));<br><span style="font-weight: bold;">4.计算一年中的第几星期</span><br>(1)计算某一天是一年中的第几星期<br>Calendar cal=Calendar.getInstance();<br>cal.set(Calendar.YEAR, 2006);<br>cal.set(Calendar.MONTH, 8);<br>cal.set(Calendar.DAY_OF_MONTH, 3);<br><span style="color: #ff3300;">int weekno=cal.get(Calendar.WEEK_OF_YEAR);</span><br>(2)计算一年中的第几星期是几号<br>SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");<br>Calendar cal=Calendar.getInstance();<br>cal.set(Calendar.YEAR, 2006);<br>cal.set(Calendar.WEEK_OF_YEAR, 1);<br>cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);<br>System.out.println(df.format(cal.getTime()));<br>输出:<br>2006-01-02<br><span style="font-weight: bold;">5.add()和roll()的用法</span><br>(1)add()方法<br>SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");<br>Calendar cal=Calendar.getInstance();<br>cal.set(Calendar.YEAR, 2006);<br>cal.set(Calendar.MONTH, 8);<br>cal.set(Calendar.DAY_OF_MONTH, 3);<br>cal.add(Calendar.DATE, -4);<br>Date date=cal.getTime();<br>System.out.println(df.format(date));<br>cal.add(Calendar.DATE, 4);<br>date=cal.getTime();<br>System.out.println(df.format(date));<br>输出:<br> 2006-08-30<br> 2006-09-03<br>(2)roll方法<br>cal.set(Calendar.YEAR, 2006);<br>cal.set(Calendar.MONTH, 8);<br>cal.set(Calendar.DAY_OF_MONTH, 3);<br>cal.roll(Calendar.DATE, -4);<br>date=cal.getTime();<br>System.out.println(df.format(date));<br>cal.roll(Calendar.DATE, 4);<br>date=cal.getTime();<br>System.out.println(df.format(date));<br>输出:<br> 2006-09-29<br> 2006-09-03<br>可见,<span style="color: #ff3300;">roll()方法在本月内循环,一般使用add()方法;</span><br><span style="font-weight: bold;">6.计算两个任意时间中间的间隔天数</span><br>(1)传进Calendar对象</p>
<div style="">
<div>
<img id="_0_80_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top"><img id="_0_80_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" alt="" align="top"><span id="_0_80_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">/***/</span><span id="_0_80_Open_Text"><span style="color: #008000;">/**</span><span style="color: #008000;">计算两个时间之间相隔天数<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">*</span><span style="color: #808080;">@param</span><span style="color: #008000;">startday开始时间<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">*</span><span style="color: #808080;">@param</span><span style="color: #008000;">endday结束时间<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">*</span><span style="color: #808080;">@return</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top"></span><span style="color: #008000;">*/</span></span><span style="color: #000000;"><br><img id="_144_430_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top"><img id="_144_430_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">public</span><span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">getIntervalDays(Calendarstartday,Calendarendday)</span><span id="_144_430_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_144_430_Open_Text"><span style="color: #000000;">{<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">确保startday在endday之前</span><span style="color: #008000;"><br><img id="_198_263_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top"><img id="_198_263_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(startday.after(endday))</span><span id="_198_263_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_198_263_Open_Text"><span style="color: #000000;">{<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">Calendarcal</span><span style="color: #000000;">=</span><span style="color: #000000;">startday;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">startday</span><span style="color: #000000;">=</span><span style="color: #000000;">endday;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">endday</span><span style="color: #000000;">=</span><span style="color: #000000;">cal;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top">}</span></span><span style="color: #000000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">分别得到两个时间的毫秒数</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">long</span><span style="color: #000000;">sl</span><span style="color: #000000;">=</span><span style="color: #000000;">startday.getTimeInMillis();<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">long</span><span style="color: #000000;">el</span><span style="color: #000000;">=</span><span style="color: #000000;">endday.getTimeInMillis();<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">long</span><span style="color: #000000;">ei</span><span style="color: #000000;">=</span><span style="color: #000000;">el</span><span style="color: #000000;">-</span><span style="color: #000000;">sl;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">根据毫秒数计算间隔天数</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">return</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)(ei</span><span style="color: #000000;">/</span><span style="color: #000000;">(</span><span style="color: #000000;">1000</span><span style="color: #000000;">*</span><span style="color: #000000;">60</span><span style="color: #000000;">*</span><span style="color: #000000;">60</span><span style="color: #000000;">*</span><span style="color: #000000;">24</span><span style="color: #000000;">));<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top">}</span></span>
</div>
</div>
<p>(2)传进Date对象</p>
<div style="">
<div>
<img id="_0_80_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top"><img id="_0_80_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" alt="" align="top"><span id="_0_80_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">/***/</span><span id="_0_80_Open_Text"><span style="color: #008000;">/**</span><span style="color: #008000;">计算两个时间之间相隔天数<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">*</span><span style="color: #808080;">@param</span><span style="color: #008000;">startday开始时间<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">*</span><span style="color: #808080;">@param</span><span style="color: #008000;">endday结束时间<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">*</span><span style="color: #808080;">@return</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top"></span><span style="color: #008000;">*/</span></span><span style="color: #000000;"><br><img id="_136_402_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top"><img id="_136_402_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">public</span><span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">getIntervalDays(Datestartday,Dateendday)</span><span id="_136_402_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_136_402_Open_Text"><span style="color: #000000;">{<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">确保startday在endday之前</span><span style="color: #008000;"><br><img id="_190_251_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top"><img id="_190_251_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(startday.after(endday))</span><span id="_190_251_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_190_251_Open_Text"><span style="color: #000000;">{<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">Datecal</span><span style="color: #000000;">=</span><span style="color: #000000;">startday;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">startday</span><span style="color: #000000;">=</span><span style="color: #000000;">endday;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">endday</span><span style="color: #000000;">=</span><span style="color: #000000;">cal;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top">}</span></span><span style="color: #000000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">分别得到两个时间的毫秒数</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">long</span><span style="color: #000000;">sl</span><span style="color: #000000;">=</span><span style="color: #000000;">startday.getTime();<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">long</span><span style="color: #000000;">el</span><span style="color: #000000;">=</span><span style="color: #000000;">endday.getTime();<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">long</span><span style="color: #000000;">ei</span><span style="color: #000000;">=</span><span style="color: #000000;">el</span><span style="color: #000000;">-</span><span style="color: #000000;">sl;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #008000;">//</span><span style="color: #008000;">根据毫秒数计算间隔天数</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #000000;"></span><span style="color: #0000ff;">return</span><span style="color: #000000;">(</span><span style="color: #0000ff;">int</span><span style="color: #000000;">)(ei</span><span style="color: #000000;">/</span><span style="color: #000000;">(</span><span style="color: #000000;">1000</span><span style="color: #000000;">*</span><span style="color: #000000;">60</span><span style="color: #000000;">*</span><span style="color: #000000;">60</span><span style="color: #000000;">*</span><span style="color: #000000;">24</span><span style="color: #000000;">));<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top">}</span></span>
</div>
</div>
<p>同理,可以用相同的方法计算出任意两个时间相隔的小时数,分钟数,秒钟数等<br>注:以上方法是完全按时间计算,有时并不能令人满意,如:<br>startday="2006-10-11 20:00:00"<br>endday="2006-10-12 8:00:00"<br>计算结果为0,但是我们也许相让计算结果变为1,此时可以用如下方法实现:<br>在传参之前,先设定endday的时间,如:<br> endday.set(Calendar.HOUR_OF_DAY, 23);<br> endday.set(Calendar.MINUTE, 59);<br> endday.set(Calendar.SECOND, 59);<br> endday.set(Calendar.MILLISECOND, 59);<br>这样再传进去startday,endday,则结果就如我们所愿了。不过,如果嫌以上方法麻烦,可以参考以下方法:<br>(3)改进精确计算相隔天数的方法</p>
<div style="">
<div>
<img id="_54_607_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top"><img id="_54_607_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" alt="" align="top"><span style="color: #000000;"></span><span style="color: #0000ff;">public</span><span style="color: #000000;"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">getDaysBetween(Calendard1,Calendard2)</span><span id="_54_607_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_54_607_Open_Text"><span style="color: #000000;">{<br><img id="_79_212_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top"><img id="_79_212_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(d1.after(d2))</span><span id="_79_212_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_79_212_Open_Text"><span style="color: #000000;">{</span><span style="color: #008000;">//</span><span style="color: #008000;">swapdatessothatd1isstartandd2isend</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #000000;">java.util.Calendarswap</span><span style="color: #000000;">=</span><span style="color: #000000;">d1;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">d1</span><span style="color: #000000;">=</span><span style="color: #000000;">d2;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">d2</span><span style="color: #000000;">=</span><span style="color: #000000;">swap;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top">}</span></span><span style="color: #000000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">days</span><span style="color: #000000;">=</span><span style="color: #000000;">d2.get(Calendar.DAY_OF_YEAR)</span><span style="color: #000000;">-</span><span style="color: #000000;">d1.get(Calendar.DAY_OF_YEAR);<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">int</span><span style="color: #000000;">y2</span><span style="color: #000000;">=</span><span style="color: #000000;">d2.get(Calendar.YEAR);<br><img id="_366_586_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top"><img id="_366_586_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">if</span><span style="color: #000000;">(d1.get(Calendar.YEAR)</span><span style="color: #000000;">!=</span><span style="color: #000000;">y2)</span><span id="_366_586_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_366_586_Open_Text"><span style="color: #000000;">{<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">d1</span><span style="color: #000000;">=</span><span style="color: #000000;">(Calendar)d1.clone();<br><img id="_417_542_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top"><img id="_417_542_Closed_Image" style="display: none;" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">do</span><span style="color: #000000;"></span><span id="_417_542_Closed_Text" style="background-color: #ffffff; display: none; border: #808080 1px solid;">...</span><span id="_417_542_Open_Text"><span style="color: #000000;">{<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top">days</span><span style="color: #000000;">+=</span><span style="color: #000000;">d1.getActualMaximum(Calendar.DAY_OF_YEAR);</span><span style="color: #008000;">//</span><span style="color: #008000;">得到当年的实际天数</span><span style="color: #008000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #000000;">d1.add(Calendar.YEAR,</span><span style="color: #000000;">1</span><span style="color: #000000;">);<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top">}</span></span><span style="color: #000000;"></span><span style="color: #0000ff;">while</span><span style="color: #000000;">(d1.get(Calendar.YEAR)</span><span style="color: #000000;">!=</span><span style="color: #000000;">y2);<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif" alt="" align="top">}</span></span><span style="color: #000000;"><br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" alt="" align="top"></span><span style="color: #0000ff;">return</span><span style="color: #000000;">days;<br><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" alt="" align="top">}</span></span>
</div>
</div>

转载地址:http://lbaji.baihongyu.com/

你可能感兴趣的文章
vu 是什么
查看>>
io口的作用
查看>>
IO口的作用
查看>>
UIView的使用setNeedsDisplay
查看>>
归档与解归档
查看>>
Window
查看>>
为什么button在设置标题时要用一个方法,而不像lable一样直接用一个属性
查看>>
字符串的截取
查看>>
2. Add Two Numbers
查看>>
17. Letter Combinations of a Phone Number (DFS, String)
查看>>
93. Restore IP Addresses (DFS, String)
查看>>
19. Remove Nth Node From End of List (双指针)
查看>>
49. Group Anagrams (String, Map)
查看>>
139. Word Break (DP)
查看>>
23. Merge k Sorted Lists (Divide and conquer, Linked List) 以及java匿名内部类
查看>>
Tensorflow入门资料
查看>>
剑指_用两个栈实现队列
查看>>
剑指_顺时针打印矩阵
查看>>
剑指_栈的压入弹出序列
查看>>
剑指_复杂链表的复制
查看>>