字符串的截取有一个很简单的方法,如下:
String s1 = s.substring(0,5); 不用多说了吧
那么关于format呢?一直以来我很讨厌这个的。
打个比如,数据库里面存的是年月日时分秒这样的格式,可是你取出来的话只需要年月日的表达方式,那么就是这样写:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String currTime = sdf.format(rs.getDate("create_date"));
同样的,要把规定的格式插入数据库,这样写到:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currTime = sdf.format(new Date());
关于字符串截取的补充:
String temp="S1111-S233-sdddd-001";
string a=temp.substring(temp.length-3);
reponse.write(a);
这样输出的是001