JavaSE Calendar 基础api

public void testCalender(){
		//获取当前系统时间
		Date date = new Date();
		//字符串转换日期格式
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = sdf.format(date);
		System.out.println(dateString);
		
		Calendar calendar = Calendar.getInstance();
		Date date2 = calendar.getTime();
		System.out.println(date2);
		//显示年份
		int year = calendar.get(Calendar.YEAR);
		System.out.println(year);
		//显示月份(月份为0-11,所以要进行+1)
		int month = calendar.get(Calendar.MONTH)+1;
		System.out.println(month);
		//本周几,周日为1,周一为2,周二为3...
		int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
		System.out.println(week);
		//今年的第几天
		int day_of_year = calendar.get(Calendar.DAY_OF_YEAR);
		System.out.println(day_of_year);
		//本月的第几天
		int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
		System.out.println(day_of_month);
		//当前小时为24小时制度,若是hour则为12小时制
		System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
		//本月的第几天
		int day = calendar.get(Calendar.DATE);
		System.out.println(day);
	}

你可能感兴趣的:(JavaSE Calendar 基础api)