当前位置: 技术问答>java相关
JAVA里面怎样判断一个日期是否是星期六或者星期天,给出原代码,多谢!!!!!!!!!!
来源: 互联网 发布时间:2015-02-06
本文导语: 分数全部奉上。 | void test2(){ String tmp = "2001-09-02"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { Date myDate = sdf.parse(tmp); ...
分数全部奉上。
|
void test2(){
String tmp = "2001-09-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date myDate = sdf.parse(tmp);
Calendar myCalendar = Calendar.getInstance();
myCalendar.setTime(myDate);
int i = myCalendar.get(Calendar.DAY_OF_WEEK);
System.out.println(i);//星期日i==1,星期六i==7
if (i==1||i==7) System.out.println("OK!");
}
catch (Exception ex) {
System.out.println("Err");
}
}
String tmp = "2001-09-02";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date myDate = sdf.parse(tmp);
Calendar myCalendar = Calendar.getInstance();
myCalendar.setTime(myDate);
int i = myCalendar.get(Calendar.DAY_OF_WEEK);
System.out.println(i);//星期日i==1,星期六i==7
if (i==1||i==7) System.out.println("OK!");
}
catch (Exception ex) {
System.out.println("Err");
}
}
|
import java.util.*;
import java.text.*;
public final class getdateweek
{
private static String day_Of_The_Week [] = { "Monday 星期一", "Tuesday 星期二", "Wednesday 星期三", "Thursday 星期四", "Friday 星期五", "Saturday 星期六","Sunday 星期日"};
public static void main(String args[])
{
getdateweek dc = new getdateweek();
String asd = dc.getDayOfWeek("9/27/2001");
System.out.println("8/15/2001 "+asd);
}
public static String getDayOfWeek (String theDate)
{
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yy");
try
{
d = sdf.parse(theDate);
}
catch (ParseException e)
{
System.out.println (e);
e.printStackTrace();
}
// Create a GregorianCalendar object
Calendar c = new GregorianCalendar (TimeZone.getTimeZone ("EST"), Locale.US);
c.setTime (d);
return(day_Of_The_Week [(c.get(Calendar.DAY_OF_WEEK)-1)]);
}
}
print
8/15/2001 Thursday 星期四
import java.text.*;
public final class getdateweek
{
private static String day_Of_The_Week [] = { "Monday 星期一", "Tuesday 星期二", "Wednesday 星期三", "Thursday 星期四", "Friday 星期五", "Saturday 星期六","Sunday 星期日"};
public static void main(String args[])
{
getdateweek dc = new getdateweek();
String asd = dc.getDayOfWeek("9/27/2001");
System.out.println("8/15/2001 "+asd);
}
public static String getDayOfWeek (String theDate)
{
Date d = null;
SimpleDateFormat sdf = new SimpleDateFormat ("MM/dd/yy");
try
{
d = sdf.parse(theDate);
}
catch (ParseException e)
{
System.out.println (e);
e.printStackTrace();
}
// Create a GregorianCalendar object
Calendar c = new GregorianCalendar (TimeZone.getTimeZone ("EST"), Locale.US);
c.setTime (d);
return(day_Of_The_Week [(c.get(Calendar.DAY_OF_WEEK)-1)]);
}
}
8/15/2001 Thursday 星期四