方法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:
需要導(dǎo)入的包
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+"號");
轉(zhuǎn)自:http://blog.csdn.net/yucf1988/article/details/6461988
|