package untitled7;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.text.DateFormat;
public class test {
public static void main(String args[]){
Date date= new Date();
String dateStr = "";
String weekStr = "";
Calendar calendar = Calendar.getInstance();
int week = calendar.get(Calendar.DAY_OF_WEEK)-1;
switch(week){
case 0:
weekStr = "星期日";
break;
case 1:
weekStr = "星期一";
break;
case 2:
weekStr = "星期二";
break;
case 3:
weekStr = "星期三";
break;
case 4:
weekStr = "星期四";
break;
case 5:
weekStr = "星期五";
break;
case 6:
weekStr = "星期六";
break;
}
Date dateBegin = new Date();
Date dateEnd = new Date();
DateFormat shortDateFormat = DateFormat.getDateInstance(0);
dateStr = shortDateFormat.format(date);
dateBegin.setTime(date.getTime()-(long)(week)*24*60*60*1000);
String dateBeginStr = shortDateFormat.format(dateBegin);
dateEnd.setTime(date.getTime()+(long)(7-week-1)*24*60*60*1000);
String dateEndStr = shortDateFormat.format(dateEnd);
System.out.println("今天是当年的第"+calendar.get(Calendar.WEEK_OF_YEAR )+"周");
System.out.println("今天是当月的"+calendar.get(calendar.DAY_OF_WEEK_IN_MONTH)+"周");
System.out.println("今天是"+weekStr);
System.out.println("本周的开始时间是"+dateBeginStr);
System.out.println("本周的结束时间是"+dateEndStr);
calendar.set( Calendar.DAY_OF_WEEK, 1 );
System.out.println( "本周的开始时间是"+(calendar.get(Calendar.MONTH)+1)+"月" + calendar.get( Calendar.DATE )+"日");
calendar.set(Calendar.DAY_OF_WEEK,7);
System.out.println( "本周的开始结束时间是"+(calendar.get(Calendar.MONTH)+1)+"月" + calendar.get( Calendar.DATE )+"日");
}
}
-------------------------------------------------------------------------------------------------------------------------------------------
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class text {
public static void main(String[] args) {
final String dayNames[] = { "星期日", "星期一", "星期二", "星期三", "星期四",
"星期五", "星期六" };
String s = "2006-01-1 16:30";
SimpleDateFormat sdfInput = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar calendar = Calendar.getInstance();
Date date = new Date();
try {
date = sdfInput.parse(s);
} catch (ParseException e) {
e.printStackTrace();
}
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
System.out.println(dayNames[dayOfWeek - 1]);
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/*
* 判斷字串是否為有效的日期
* 日期格式20060101共8碼 (有效true,無效false)
*/
public static boolean isDateValidate(String timeString){
if(timeString==null || timeString.length()!=8){
return false;}
try{
java.text.SimpleDateFormat dFormat = new SimpleDateFormat("yyyyMMdd");
dFormat.setLenient(false);
java.util.Date d = dFormat.parse(timeString);
java.util.Date ddd = dFormat.parse(timeString);
}catch(Exception e){
return false;
}
return true;
}
本文转自kenty博客园博客,原文链接http://www.cnblogs.com/kentyshang/archive/2007/07/06/808058.html如需转载请自行联系原作者
kenty