calendar中获得日期的年月日

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

JavaCalendar获取年、月、日、时间Calendarc=Calendar.getInstance(TimeZone.getTimeZone(GMT+08:00));//获取东八区时间intyear=c.get(Calendar.YEAR);//获取年intmonth=c.get(Calendar.MONTH)+1;//获取月份,0表示1月份intday=c.get(Calendar.DAY_OF_MONTH);//获取当前天数intfirst=c.getActualMinimum(c.DAY_OF_MONTH);//获取本月最小天数intlast=c.getActualMaximum(c.DAY_OF_MONTH);//获取本月最大天数inttime=c.get(Calendar.HOUR_OF_DAY);//获取当前小时intmin=c.get(Calendar.MINUTE);//获取当前分钟intxx=c.get(Calendar.SECOND);//获取当前秒SimpleDateFormats=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss);StringcurDate=s.format(c.getTime());//当前日期System.out.println(当前时间:+year+-+month+-+day++time+:+min+:+xx);System.out.println(第一天和最后天:+first+,+last);System.out.println(当前日期curDate====:+curDate);输出结果:当前时间:2012-9-2522:50:54第一天和最后天:1,30当前日期curDate:2012-09-2522:50:54Calendar的计算c.add(Calendar.YEAR,1);c.add(Calendar.MONTH,1);c.add(Calendar.DAY_OF_MONTH,1);intyear2=c.get(Calendar.YEAR);intmonth2=c.get(Calendar.MONTH)+1;intday2=c.get(Calendar.DAY_OF_MONTH);intfirstD=c.getActualMinimum(c.DAY_OF_MONTH);intlastD=c.getActualMaximum(c.DAY_OF_MONTH);System.out.println(当前时间:+year2+-+month2+-+day2++time+:+min+:+xx);System.out.println(第一天和最后天:+firstD+,+lastD);输出结果:当前时间:2013-10-2623:4:3第一天和最后天:1,31获取上个月的年、月、日Calendarc=Calendar.getInstance();c.add(Calendar.MONTH,-1);//上个月SimpleDateFormats=newSimpleDateFormat(yyyy-MM-ddHH:mm:ss);Stringyear=String.valueOf(c.get(Calendar.YEAR));StringtopDay=String.valueOf(c.getActualMinimum(Calendar.DAY_OF_MONTH));StringlastDay=String.valueOf(c.getActualMaximum(Calendar.DAY_OF_MONTH));//上个月StringlastMonth=String.valueOf(c.get(Calendar.MONTH)+1).length()==2?String.valueOf(c.get(Calendar.MONTH)+1):0+String.valueOf(c.get(Calendar.MONTH)+1);StringtopDayMonth=year+-+lastMonth+-+01+00:00:00;StringlastDayMonth=year+-+lastMonth+-+lastDay+23:59:59;System.out.println(###year:+year);System.out.println(###lastmonth:+lastMonth);System.out.println(###topday:+topDayMonth);System.out.println(###lastday:+lastDayMonth);输出结果###year:2013###lastmonth:04###topday:2013-04-0100:00:00###lastday:2013-04-3023:59:59转自

1 / 3
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功