1. 程式人生 > >關於String.format的使用以及把秒轉換為小時,分鐘的程式碼

關於String.format的使用以及把秒轉換為小時,分鐘的程式碼

package test.java;


import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;


import com.mysql.jdbc.Connection;


public class UpdateFormatTimeResourceInfo {
public static void main(String[] args) {
Connection conn = ImportMapper.get215connection("dayadata01");
Map<String, String> map = new HashMap<String, String>();
try {
PreparedStatement ps = conn.prepareStatement("SELECT resource_id,view_elements from resource_info where id > 181877 AND id < 182232");
ResultSet rs = ps.executeQuery();
while (rs.next()) {
map.put(rs.getString("resource_id"), rs.getString("view_elements"));
}

PreparedStatement ps1 = conn.prepareStatement("update resource_info set view_elements = ? where resource_id = ? and id > 181877 AND id < 182232");
String str = "";
for (Entry<String, String> strs : map.entrySet()) {
str = getFormal(strs.getValue());
ps1.setString(1, str);
ps1.setString(2, strs.getKey());
int r = ps1.executeUpdate();
if(r>0) {
System.out.println("更新成功");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


private static String getFormal(String value) {
String str = "";
value = value.replaceAll("\\D", "");
if(value != null && "" != value) {
try {

int a = Integer.parseInt(value);
if(a <3600) {
str = String.format("%02d:%02d:%02d", a/3600,a/60,a%60);
} else {
str = String.format("%02d:%02d:%02d", a/3600,(a-a/3600*3600)/60,a%60);
}
} catch (Exception e) {

}
}
return str;
}
}