通过日期来知道当天是星期几,及日期,String转为Date
public class Once{
public final static String dayNames[] = {
"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
public static String dateToDay(String date) throws ParseException
{
Calendar calendar = Calendar.getInstance();
calendar.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(date));
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
String day=dayNames[dayOfWeek - 1];
return day;
}
public static void main(String[] args) throws ParseException {
String day= Once.dateToDay("2009-04-01");
System.out.println("这一天是:"+day);
Calendar calendar = Calendar.getInstance();
Calendar calendar2 = Calendar.getInstance();
Date newit=new SimpleDateFormat("yyyy-MM-dd").parse("2009-04-01");
Date newit2=new SimpleDateFormat("yyyy-MM-dd").parse("2009-04-02");
calendar.setTime(newit);
calendar2.setTime(newit2);
System.out.println("比大小:"+(calendar2.getTimeInMillis()-calendar.getTimeInMillis()>0));
}
}