获取系统时间和某天是星期几

来源:互联网 发布:马赛克拼图制作软件 编辑:程序博客网 时间:2024/06/08 16:08

获取系统时间和某天是星期几


获取当前系统年月日是星期几

[java] view plain copy
  1. Calendar calendar = Calendar.getInstance();  
  2. int year = calendar.get(Calendar.YEAR);  
  3. int month = calendar.get(Calendar.MONTH) + 1;// Java月份从0开始算  
  4. int day = calendar.get(Calendar.DAY_OF_MONTH);  

获取指定某年某月某日是星期几

[java] view plain copy
  1. Calendar calendar = Calendar.getInstance();  
  2. calendar.set(Calendar.YEAR, year);//指定年份  
  3. calendar.set(Calendar.MONTH, month - 1);/指定月份 Java月份从0开始算  
  4. int daysCountOfMonth = calendar.getActualMaximum(Calendar.DATE);//获取指定年份中指定月份有几天  
  5.   
  6. //获取指定年份月份中指定某天是星期几  
  7. calendar.set(Calendar.DAY_OF_MONTH, day);  //指定日
  8. int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);  
  9. switch (dayOfWeek)  
  10. {    
  11. case 1:    
  12.    weekTextView.setText("星期日");    
  13.    break;    
  14. case 2:    
  15.    weekTextView.setText("星期一");    
  16.    break;    
  17. case 3:    
  18.    weekTextView.setText("星期二");    
  19.    break;    
  20. case 4:    
  21.    weekTextView.setText("星期三");    
  22.    break;    
  23. case 5:    
  24.    weekTextView.setText("星期四");    
  25.    break;    
  26. case 6:    
  27.    weekTextView.setText("星期五");    
  28.    break;    
  29. case 7:    
  30.    weekTextView.setText("星期六");    
  31.    break;    
  32. }


0 0
原创粉丝点击