日期:2014-05-20  浏览次数:20665 次

求教字符串替换问题
select a.record from phone where initTime between 2012-12-12 8:0 and 2012-12-12 8:10
select a.record from phone where initTime between 2012-12-12 8:10 and 2012-12-12 8:20
select a.record from phone where initTime between 2012-12-12 8:20 and 2012-12-12 8:30
select a.record from phone where initTime between 2012-12-12 8:30 and 2012-12-12 8:40
select a.record from phone where initTime between 2012-12-12 8:40 and 2012-12-12 8:50
select a.record from phone where initTime between 2012-12-12 8:50 and 2012-12-12 8:60
select a.record from phone where initTime between 2012-12-12 9:0 and 2012-12-12 9:10




 问下 这个 8:0我要替换成8:00  8:60 这个要替换到9:00 这些是循环出来的 要怎么做
代码:
int k = 8;
for (k = 8; k < 18; k++) {
for (int i = 0, j = 10; j <= 60; i = i + 10, j = j + 10) {

String sql = "select a.record from phone where initTime between 2012-12-12 "
+ k + ":" + i + " and 2012-12-12 " + k + ":" + j;
                
System.out.println(sql);

}
}
------解决方案--------------------

public class Test {

public static void main(String[] args) {
String s = "select a.record from phone where initTime between 2012-12-12 8:0 and 2012-12-12 8:10";
s = s.replaceAll("(\\s+\\d+:)0(\\s
------解决方案--------------------
$)","$100$2");
System.out.println(s);
String s1 = "select a.record from phone where initTime between 2012-12-12 8:50 and 2012-12-12 8:60";
Matcher m = Pattern.compile("\\s+(\\d+):60(\\s+
------解决方案--------------------
$)").matcher(s1);
if(m.find()){
int h = Integer.parseInt(m.group(1));
s1 = s1.replaceAll("(\\s+)\\d+:60(\\s+
------解决方案--------------------
$)", "$1"+ ++h +":00$2");
}
System.out.println(s1);
}

}