返回 登录
0

java 判断是否是周末

阅读284
  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;
       } 

学习Java的同学注意了!!!
学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:492139965 我们一起学Java!

评论