配色: 字号:
获取一年的第一天
2016-09-20 | 阅:  转:  |  分享 
  
?开始使用newDate()测试,并用通过date.getMonth(),和date.getDay()获取,不过后来发现这两个访求是jdk
1.1版本的,现在已经不用了,而且结果也不正确.所以改用了Calendar如下CalendarrightNow=Calen
dar.getInstance();?intmonth?=rightNow.MONTH;?intday=rightNow
.DAY_OF_MONTH;结果是month为2,而day为5,和现在的日期4.26没有关系我然后用System.out.prin
tln(rightNow);,不过输出的很多内容里面的MONTH和DAY_OF_MONTH是对的后来,在网上查找,不能这么用应该用
?intmonth?=(date.get(Calendar.MONTH))+1;intday=date.get(Ca
lendar.DAY_OF_MONTH);获取当前的月份和日期试了一下,果然正确后来查看javadoc文档,MONTH字段解释如
下Fieldnumberfor?get?and?set?indicatingthemonth.Thisisacal
endar-specificvalue.Thefirstmonthoftheyearis?JANUARY?whic
his0;thelastdependsonthenumberofmonthsinayear.这个字段的值
只是说明get()的属性字段值,来获取month的?以下为获取其它:Calendarcal=Calendar.getInst
ance();//当前年intyear=cal.get(Calendar.YEAR);//当前月intmonth
=(cal.get(Calendar.MONTH))+1;//当前月的第几天:即当前日intday_of_month=
cal.get(Calendar.DAY_OF_MONTH);//当前时:HOUR_OF_DAY-24小时制;HOUR-12小时
制inthour=cal.get(Calendar.HOUR_OF_DAY);//当前分intminute=ca
l.get(Calendar.MINUTE);//当前秒intsecond=cal.get(Calendar.SECON
D);//0-上午;1-下午intampm=cal.get(Calendar.AM_PM);//当前年的第几周int
week_of_year=cal.get(Calendar.WEEK_OF_YEAR);//当前月的第几周intwee
k_of_month=cal.get(Calendar.WEEK_OF_MONTH);//当前年的第几天intday_o
f_year=cal.get(Calendar.DAY_OF_YEAR);
献花(0)
+1
(本文系haohaojava首藏)