日期:2014-05-20  浏览次数:20660 次

日期获取 相关问题
比如说今天是2月27号,我想输出三天后的日期和星期
也就是 3月1号 周四
 
Java code

  Calendar c=Calendar.getInstance();  
  c.add(Calendar.DAY_OF_YEAR, 3);
  int month = c.get(Calendar.MONTH);
  String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  int w = c.get(Calendar.DAY_OF_WEEK);
  if (w < 0)
  w = 0;
  date = c.get(Calendar.MONTH)+"/"+ c.get(Calendar.DAY_OF_MONTH)+"  "+weekDays[w];


输出结果是:2/1 星期五
两个问题:1、为什么我设置的c.add(Calendar.DAY_OF_YEAR, 3);月份不变?
  2、我week的数组是从sunday开始的呀,而DAY_OF_WEEK也是从sunday 为什么还是出错了?还得去做一次减法?

我用date方法是对的,如下,不过弄不清Calendar很不爽…………
 
Java code

Calendar c=Calendar.getInstance();  
          SimpleDateFormat format=new SimpleDateFormat("MM月dd日 E" );
          c.add(Calendar.DAY_OF_YEAR, 3);
          Date d1=new Date(c.getTimeInMillis());
          date = format.format(d1);



有没有其他的日期获取方法?求教

------解决方案--------------------
Calendar c=Calendar.getInstance();
c.add(Calendar.DAY_OF_YEAR, 3);
int month = c.get(Calendar.MONTH);
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
int w = c.get(Calendar.DAY_OF_WEEK);
if (w < 0)
w = 0;
date = c.get(Calendar.MONTH)+"/"+ c.get(Calendar.DAY_OF_MONTH)+" "+weekDays[w];


注: Calendar.get(Calendar.MONTH) 是以0开始的 ,即1月 返回 0 ,2月份返回1 ,3--2 ...
如果想得到实际的月份 ,应该是 int month = Calendar.get(Calendar.MONTH) +1 ;


得到星期几 ; int w = c.get(Calendar.DAY_OF_WEEK); //==6 就是星期五啊