java 日历 Calendar 获取当前时间是星期几

   1、 怎样格式化当前时间:格式化是要是想获取几天是今年的第多少天,把“dd”改成“DD”那个位置出现的就是今年的第多少天。

Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
String currentDate = sdf.format(date);

 2、获取一个日期是星期几:w 就是星期几。

Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
    w = 0;

 3、这也是一个获取星期的方法:

public static String getWeek1(Date date){
			 String str = "";
			 Calendar c=Calendar.getInstance();
			 c.setTime(date);
			 int weekDay = c.get(Calendar.DAY_OF_WEEK);
			 if(Calendar.MONDAY == weekDay){
				 str = "星期一";
			 }else if(Calendar.TUESDAY==weekDay){
				 str = "星期二";
			 }else if(Calendar.WEDNESDAY==weekDay){
				 str ="星期三";
			 }else if(Calendar.THURSDAY==weekDay){
				 str ="星期四";
			 }else if(Calendar.FRIDAY==weekDay){
				 str ="星期五";
			 }else if(Calendar.SATURDAY==weekDay){
				 str ="星期六";
			 }else {
				 str ="星期日";
			 }
			 return str;
		}

 

你可能感兴趣的:(java)