项目中所用到的一些xx

来源:互联网 发布:java基础入门课程 编辑:程序博客网 时间:2024/05/18 01:39

 

第一,判断日期 格式

public static boolean checkDate(String str){   
        
boolean checkOk= true;   
        
try{   
            DateFormat df
=new SimpleDateFormat("yyyy/MM/dd");
            df.setLenient(
false);
            
if(str!=""){
    Date d
=df.parse(str);
            }
else{
                checkOk
=false;
            }

        }
catch(ParseException e){   
            checkOk
=false
        }
   
        
return checkOk;   
    }

 其中,日期字符串格式可以自己定义,以匹配你要判断的日期格式!                                                                                 这里df.setLenient(false),很有用的,避免出现2007/2/31的日期向后推移

第二,显示规定的日期

public String formatToDateHourMin(Date date){
    Calendar c
=Calendar.getInstance();
    String time
="";
    c.setTime(date);
//
    time+=c.get(Calendar.YEAR)+"/";
    
if((c.get(Calendar.MONTH)+1)<10)
        time
+="0"+(c.get(Calendar.MONTH)+1)+"/";
    
else
        time
+=(c.get(Calendar.MONTH)+1)+"/";
    
if(c.get(Calendar.DAY_OF_MONTH)<10)
        time
+="0"+c.get(Calendar.DAY_OF_MONTH)+" ";
    
else
         time
+=c.get(Calendar.DAY_OF_MONTH)+" ";
    time
+=c.get(Calendar.HOUR_OF_DAY)+":";//24
    if(c.get(Calendar.MINUTE)<10)
        time
+="0"+c.get(Calendar.MINUTE);
    
else
        time
+=c.get(Calendar.MINUTE); 
    
return time;
}

这里所显示的日期统一为2007/05/08 18:08:08

第三,把字符串转化为Date对象

public Date transStringToDate(String dateString,String dateFormat){
     SimpleDateFormat sdf
=new SimpleDateFormat(dateFormat);
     sdf.setLenient(
false);
     Date d
=null;
     
try{
          d
=sdf.parse(dateString);
      }
catch(Exception e){}
      
return d;
}

这里的字符串格式dateFormat可以为yyyy/MM/dd hh:mm:ss等等!!!!

 第四,计算两个日期之间的天数

public static int getDaysAfterSomeDay(Date date1,Date date2){
     
int days=0;
     Calendar c1
=Calendar.getInstance();
     
if(date1!=null && date2!=null){
           c1.setTime(date1);
           Calendar c2
=Calendar.getInstance();
           c2.setTime(date2);
           days
=(int)((c2.getTimeInMillis()-c1.getTimeInMillis())/1000/60/60/24);
     }

      
return days;
}

通过计算毫秒数一步步得到日期间的天数,可以轻易避开月份间天数的差别

第五,判断输入是否为数字

public static boolean checkNumber(String str)   {   
     
boolean checkOk= true;   
     
try{   
           
for (int j = 0; j < str.length(); j++
                 
if (str.charAt(j) < 48 || str.charAt(j) > 57{
                       checkOk
= false
          
break
                 }

           }

      }
catch(Exception e){   
           checkOk
=false
           System.out.println(
"it is not a number");
       }
   
       
return checkOk;   
}

通过判断输入的每个字符的ASCII码是否在[48,57]之间确定数字!

第六,半角/全角的判断

public static boolean checkHalf(String str)   {   
      
boolean checkOk= true;   
      
if(str.getBytes().length==str.length()) 
             checkOk
=true;
      
else
              checkOk
=false;
       checkOk;   
}
    
public static int checkFull(String str){
        
int n=0;
        
int length=str.length();  
        
for(int i=0;i<length;i++)
             
char c=str.charAt(i);  
             
if (c>255)  n++;     
         }

          
return n;
}

说明:checkHalf()判断字符串是否是半角。checkFull()判断字符中有多少全角字符。

第七,判断输入是否为字符和数字

public static boolean checkCharNumber(String str)   {   
     
boolean checkOk= true;   
     
try{   
         
for (int j = 0; j < str.length(); j++
                 
if (!((str.charAt(j) >= 48 && str.charAt(j) <=57|| (str.charAt(j)>=65 && str.charAt(j)<=90)
                  
|| (str.charAt(j)>=97 && str.charAt(j)<=122))) {
                         checkOk
= false
                        
break;
                 }

          }

      }
catch(Exception e){   
           checkOk
=false
           System.out.println(
"it is invalid chars");
       }
   
       
return checkOk;   
 }

同理通过ASCII来判断

 

原创粉丝点击