String date = convertDate(System.currentTimeMillis(), "yyyyMM"); ResultSet rs = conn.createStatement().executeQuery("select count(*)+1 from table"); String count = rs.getString(1); rs.close(); while (count.length() < 3) { count = "0" + count; } String outtext = date + count;
用一个变量count纪录相同的次数 4楼那里写的那样。
public class test { private static Hashtable<String, String> ht = new Hashtable<String, String>(); public static String zz(String date){ String num; String x = "000"; int i;
num = ht.get(date); if (num != null){ i = Integer.valueOf(num) + 1; num = x.substring(0, x.length() - String.valueOf(i).length()) + String.valueOf(i); ht.put(date, num); }else{ num = "001"; ht.put(date, num); }
ResultSet rs = conn.createStatement().executeQuery("select count(*)+1 from table");
String count = rs.getString(1);
rs.close();
while (count.length() < 3) {
count = "0" + count;
}
String outtext = date + count;
4楼那里写的那样。
public class test {
private static Hashtable<String, String> ht = new Hashtable<String, String>();
public static String zz(String date){
String num;
String x = "000";
int i;
num = ht.get(date);
if (num != null){
i = Integer.valueOf(num) + 1;
num = x.substring(0, x.length() - String.valueOf(i).length()) + String.valueOf(i);
ht.put(date, num);
}else{
num = "001";
ht.put(date, num);
}
return num;
}
public static void main(String[] args) {
System.out.println(zz("200104"));
System.out.println(zz("200105"));
System.out.println(zz("200104"));
System.out.println(zz("200104"));
System.out.println(zz("200105"));
}
}
DecimalFormat decimalFormat = new DecimalFormat("000");
使用:
String s = decimalFormat.format(number);//format方法有多个重载,得自己慢慢看了,呵呵
这个变量就可以自己建一个如:
class item{
String m;
int number;
}