关于String.format的使用以及把秒转换为小时,分钟的代码

1.http://blog.csdn.net/lonely_fireworks/article/details/7962171/这个是String.format使用的理论的介绍,里面有个别简便的例子

2.输出时%d用%2d %2.0d 和%02d有什么不一样?

该网址https://zhidao.baidu.com/question/681666154238656332.html有介绍


实现把秒转换为小时,分钟的代码

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;
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值