加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA判断当前日期是星期几

(2012-05-22 22:37:01)
标签:

杂谈

分类: 编程

方法1:

  1. public static int dayForWeek(String pTime) throws Exception {
  2. format = new SimpleDateFormat("yyyy-MM-dd");
  3. Calendar c = Calendar.getInstance();
  4. c.setTime(format.parse(pTime));
  5. int dayForWeek = 0;
  6. if(c.get(Calendar.DAY_OF_WEEK) == 1){
  7. dayForWeek = 7;
  8. }else{
  9. dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
  10. }
  11. return dayForWeek;
  12. }

 

方法2:

需要导入的包

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

  1. public static int dayForWeek(String pTime) throws Throwable {
  2. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  3. Date tmpDate = format.parse(pTime);
  4. Calendar cal = new GregorianCalendar();
  5. cal.set(tmpDate.getYear(), tmpDate.getMonth(), tmpDate.getDay());
  6. return cal.get(Calendar.DAY_OF_WEEK);
  7. }


public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);

int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;

return weekDays[w];
}






public static String getWeekOfDate(Date dt) {
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
cal.setTime(dt);

int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0)
w = 0;

return weekDays[w];
}

Date date=new Date();
//今天是几号
int day=date.getDate();
System.out.println("Today is :"+day+"号");
Calendar c=Calendar.getInstance();
c.setTime(date);
//今天是这个星期的第几天
int week=c.get(Calendar.DAY_OF_WEEK);
System.out.println("week:"+c.get(Calendar.DAY_OF_WEEK));
//当前月的最后一天是几号
int lastday=c.getActualMaximum(Calendar.DAY_OF_MONTH);
System.out.println("这个月最后一天是:"+lastday+"号");

 

转自:http://blog.csdn.net/yucf1988/article/details/6461988

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有