JAVA判断当前日期是星期几
(2012-05-22 22:37:01)
标签:
杂谈 |
分类: 编程 |
方法1:
- public static int dayForWeek(String pTime) throws Exception {
- format = new SimpleDateFormat("yyyy-MM-dd");
- Calendar c = Calendar.getInstance();
- c.setTime(format.parse(pTime));
- int dayForWeek = 0;
- if(c.get(Calendar.DAY_OF_WEEK) == 1){
- dayForWeek = 7;
- }else{
- dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
- }
- return dayForWeek;
- }
方法2:
需要导入的包
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
- public static int dayForWeek(String pTime) throws Throwable {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
- Date tmpDate = format.parse(pTime);
- Calendar cal = new GregorianCalendar();
- cal.set(tmpDate.getYear(), tmpDate.getMonth(), tmpDate.getDay());
- return cal.get(Calendar.DAY_OF_WEEK);
- }
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+"号");