获取当前时间的年月日方法
Calendar now = Calendar.getInstance();SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd hhmmss");
Date d = new Date();
String time = format.format(d); //获取当前日期
String time = format.format(now.getTime()); //获取当前日期注释: now.get(Calendar.YEAR)/now.get(Calendar.MONTH) + 1)/now.get(Calendar.DAY_OF_MONTH)...//获取当前年月日
常用日期方法
privateSimpleDateFormat sf = null; /*获取系统时间 格式为:"yyyy/MM/dd "*/ public static String getCurrentDate() { Date d = newDate(); sf = newSimpleDateFormat("yyyy年MM月dd日"); returnsf.format(d); } /*时间戳转换成字符窜*/ public static String getDateToString(long time) { Date d = newDate(time); sf = newSimpleDateFormat("yyyy年MM月dd日"); returnsf.format(d); } /*将字符串转为时间戳*/ public static long getStringToDate(String time) { sdf = newSimpleDateFormat("yyyy年MM月dd日"); Date date = newDate(); try{ date = sdf.parse(time); } catch(ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } returndate.getTime(); }
/** * 获取该月的第一天和最后一天 * pos 0当前月 1 下个月 -1 上个月 */ public void showMonthDate(int pos) { if(pos ==0){ //本月 curMonthCalender = Calendar.getInstance(); } else if(pos ==1){ //下个月 curMonthCalender.add(Calendar.MONTH,1); } else { //上个月 curMonthCalender.add(Calendar.MONTH,-1); } curMonthCalender.set(Calendar.DATE,1);//得到该月第一天 String startday = formatter.format(curMonthCalender.getTime()); curMonthCalender.roll(Calendar.DATE, -1);得到该月最后一天 String endday = formatter.format(curMonthCalender.getTime()); timeStr = startday + "至" + endday; tvTime.setText(timeStr); } /** * 获取该年日期 * pos 0当前年 1 下一年 -1 上一年
*/ public void showYearDate(int pos) { if(pos ==0){ //本年 Calendar curYearCalendar = Calendar.getInstance(); curYear = curYearCalendar.get(Calendar.YEAR); } else if(pos ==1){ //下个年 curYear +=1; } else { //上个年 curYear -=1; } String startday = curYear + "-01-01"; String endday = curYear + "-12-31"; timeStr = startday + "至" + endday; tvTime.setText(timeStr); } /** * 获取该季度的第一天和最后一天 * pos 0当前季度 1 下个季度 -1 上个季度 */ public void showQuaterDate(int pos) { if(pos ==0){ //本季度 curQuaterCalender = Calendar.getInstance(); int month = curQuaterCalender.MONTH; int stMonth =0; if(month >=0 && month<=3){ stMonth =0; } else if(month >=4 && month<=8){ stMonth =4; } else{ stMonth =9; } curQuaterCalender.set(Calendar.MONTH, stMonth); } else if(pos ==1){ //下季度 curQuaterCalender.add(Calendar.MONTH,3); } else { //上季度 curQuaterCalender.add(Calendar.MONTH,-3); } curQuaterCalender.set(Calendar.DATE,1);//得到该月第一天 String startday = formatter.format(curQuaterCalender.getTime()); Calendar tempDate = Calendar.getInstance(); tempDate.set(Calendar.YEAR, curQuaterCalender.get(Calendar.YEAR)); tempDate.set(Calendar.MONTH, curQuaterCalender.get(Calendar.MONTH)+3); tempDate.set(Calendar.DAY_OF_MONTH, curQuaterCalender.get(Calendar.DAY_OF_MONTH)); tempDate.roll(Calendar.DATE, -1); String endday = formatter.format(tempDate.getTime()); timeStr = startday + "至" + endday; tvTime.setText(timeStr); }