项目中所用到的一些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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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来判断
- 项目中所用到的一些xx
- python中一些链接数据库所用到的包
- jstl简介及自己项目中所用到的jstl
- 公司项目所用到的一些配置(SpringMvc+maven+ngix+tomcat)
- 关于在项目中以及日常生活中所用到的密码加密。
- HTTP RFC中所用到的规则
- java 中所用到的集合
- Spring中所用到的设计模式
- ASP.NET三层项目中DAL层方法所用到的SQL语句
- 数据挖掘所用到的一些数据集资源
- 一些我所用到的前端与PS基础知识总结
- .NET做项目所用到的N层架构
- 盘点 Github 所用到的开源项目
- 软件开发中所用到的标准化文档(转载)
- PhoneBook中所用到的数据结构及其作用【转】
- PhoneBook中所用到的数据结构及其作用(MTK)【转】
- cuda By exmaple书中所用到的代码
- (4)SVG中所用到的形状简介------矩形
- 线程同步
- Oracle中用pl/sql实现货币数字格式到汉字格式的转化
- 杀掉马王(DLL病毒)其实十分简单
- 全方位了解黑客如何入侵WindowsNT系统
- 第一个跨平台OpenOffice.org病毒出现
- 项目中所用到的一些xx
- 给C++初学者的50个忠告
- 易于debug的代码,开发优势巨大
- 认识问题的高度和深度不够害人害己
- 一个股民的选股方法:如何在中国做价值投资
- C标准库学习stdlib.h
- Lucene功能包简介
- 土豆地开张
- 当前OGRE特性