关于字符串和字符的问题

hui3zhihui 2004-04-13 05:35:49
谁可以帮忙解决以下,怎么判断一个String里面的字符只能由0--9 a--Z A--Z这些组成。还有时间的问题,怎么判断两个时间之间相差天数,如2004-02-25到2004-04-11之间相差多少天?
...全文
68 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hui3zhihui 2004-04-13
  • 打赏
  • 举报
回复
to CoolAbu(阿卜-Never Stop(★★★★)) :
不好意思,刚才眼睛看花了,找到了,Pattern p1 = Pattern.compile("[\\w]*");
这一句是什么意思,能给我讲一下吗?
hui3zhihui 2004-04-13
  • 打赏
  • 举报
回复
to CoolAbu(阿卜-Never Stop(★★★★)) :
请问一下Pattern,Matcher在那里有的?我在doc API里面没有看到阿
hui3zhihui 2004-04-13
  • 打赏
  • 举报
回复
使用char比较大小的时候,是转化成ascii码,这些字符应该是连在一起的吧!
CoolAbu 2004-04-13
  • 打赏
  • 举报
回复
1、
String yourText="abcedif12123sdf.";
Pattern p1 = Pattern.compile("[\\w]*");
Matcher m1 = p1.matcher(yourText);
if(m1.matches())
System.out.println(yourText+ " the string is valid");
else
System.out.println(yourText+ " the string is not valid");

2、
Calendar c1=Calendar.getInstance();
c1.set(2004,2,25);
Calendar c2=Calendar.getInstance();
c2.set(2004,4,11);
int days=(int)((c2.getTimeInMillis()-c1.getTimeInMillis())/(1000*60*60*24));
hui3zhihui 2004-04-13
  • 打赏
  • 举报
回复
to qwchung(小六) :
0--9 a--z A--Z如果用char c 的话,可以改成
for(int i=0;i<str.length();i++)
{
char c= str.charAt(i);
if(!(c>='0'&&c<='Z')){//||(c>='a'&&c<='z')||(c>='A'&&c<='Z')))
return false;
}
吗??
qqbz 2004-04-13
  • 打赏
  • 举报
回复
String对象有直接的方法判断是否是字母或数字类型的字符方法。
时间类型可能会有相关的方法直接可用。
GoldApple 2004-04-13
  • 打赏
  • 举报
回复
1。用正则表达式可以解决,你可以参考 apache 的一个 regexp 的包
http://jakarta.apache.org/regexp/index.html
2。想办法把他们转化为 long 值,然后相减再除以一天的毫秒数即可
qwchung 2004-04-13
  • 打赏
  • 举报
回复
//字符串
public boolean checkString(String str)
{
boolean result = true;
for(int i=0;i<str.length();i++)
{
char c= str.charAt(i);
if(!((c>='0'&&c<='9')||(c>='a'&&c<='z')||(c>='A'&&c<='Z')))
return false;
}
return result;
}

//时间
public long dayBetween(String rq1,String rq2)
{
String result = "0";
Timestamp kp = Timestamp.valueOf(rq1+" 00:00:00");
Timestamp sb = Timestamp.valueOf(rq2+" 00:00:00");
long l_time = sb.getTime()-kp.getTime();
long day=l_time/86400000;
return day;
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧