我需要从java中的Timestamp值中删除millsecond值。
例如:
Timestamp time=2013-09-23 17:57:19.860;
例外输出:
Time: 2013-09-23 17:57:19
请帮我减少毫秒
答案 0 :(得分:4)
String date = new java.text.SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(time.getDate());
答案 1 :(得分:2)
String t = "2013-09-23 17:57:19.860";
String[] split = t.split(java.util.regex.Pattern.quote("."));
String result= split[0];
System.out.println(result);
<强>更新强>
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = dateFormat.parse("2013-09-23 17:57:19.860");
long time = date.getTime();
Timestamp timestamp =new Timestamp(time);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
String result = dateFormat.format(new Date(timestamp.getTime() ));
答案 2 :(得分:2)
您可以使用TimeUnit:
long currentTimeMillis = System.currentTimeMillis();
long currentTimeSeconds = TimeUnit.SECONDS.convert(currentTimeMillis, TimeUnit.MILLISECONDS) ;
long millisUpToSecond = TimeUnit.MILLISECONDS.convert(currentTimeSeconds, TimeUnit.SECONDS);
// just to verify
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss,SSS");
System.out.println(dateFormat.format(new Date(millisUpToSecond )));
答案 3 :(得分:1)
这是一种做法。否则,请使用手动将最后3位数设置为0的困难方法。
Timestamp ts = new Timestamp(System.currentTimeMillis());
System.out.println(ts); // 2013-09-23 15:52:45.88
Calendar cal = Calendar.getInstance();
cal.clear();
cal.setTimeInMillis(ts.getTime());
cal.set(Calendar.MILLISECOND, 0);
System.out.println(cal.getTime()); // Mon Sep 23 15:52:45 CEST 2013