java日期处理(六)

来源:互联网 发布:咸阳广电网络客服热线 编辑:程序博客网 时间:2024/05/22 17:02

        在日期时间类型的操作中,还有一类比较常用的操作就是根据一个制定的日期,然后计算出该日期是星期几,该日期是一年的第几天等,本篇将分析一下这些问题的解决方案,我们还是先看代码

Code:
  1. /**  
  2.      * 获取日期时间型对象的属性,比如日期属于所在年份的第几天、日期是星期几等信息  
  3.      *  
  4.      */  
  5.     public static void dateProperty(){   
  6.            
  7.         Date currentDate;//当前系统日期   
  8.         Calendar currentCalendar;   
  9.            
  10.         //创建日期对象   
  11.         currentDate = new Date();   
  12.         //将Date对象转化成Calendar对象以便于计算   
  13.         currentCalendar = Calendar.getInstance();   
  14.         currentCalendar.setTime(currentDate);   
  15.            
  16.         //--------------------------------------------------------------   
  17.         //取得当前日期是星期几   
  18.         //   
  19.         //week的值是一个数字为1-7 其中 1代表星期天,依次类推7代表星期六   
  20.         //根据西方的习惯,一周时从星期天开始的。   
  21.         //---------------------------------------------------------------   
  22.         int week = currentCalendar.get(Calendar.DAY_OF_WEEK);   
  23.         //取得当前日期是当年的第几天   
  24.         int daycount = currentCalendar.get(Calendar.DAY_OF_YEAR);   
  25.            
  26.         System.out.println(week);   
  27.         System.out.println(daycount);   
  28.            
  29.     }   
  30.       

上述代码分别获取的当前日期星期信息和天数信息。   未完待续!!!!!

 

 如果您对我的文章感兴趣的话,请点击这里加我为好友,让我们一起进步
 
http://student.csdn.net/invite.php?u=106708&c=2383a3846076c876