我以这种格式2015-01-08 17:53:06
从表中获取时间戳值。
我需要增加一些时间来安排一份工作。那就是以毫秒为单位添加时间我该怎么办呢。
示例:
2015-01-08 17:53:06 +180000
那是30分钟,我怎么能实现呢?
答案 0 :(得分:0)
试试这样:
import java.util.*;
import java.sql.*;
public class MyClass {
public static void main(String[] args) {
long laterDate = System.currentTimeMillis();
int millisec = 18000;
Timestamp original = new Timestamp(laterDate);
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(original.getTime());
cal.add(Calendar.MILLISECOND, millisec);
Timestamp after = new Timestamp(cal.getTime().getTime());
System.out.println(original);
System.out.println(after);
}
}
答案 1 :(得分:0)
如果有人仍在寻找简短答案: 将新毫秒数添加到时间戳记的纪元毫秒中
赞: new Timestamp(timestamp.getTime()+30*60*1000);
示例:
public static void main(String[] args) {
Timestamp timestamp = Timestamp.valueOf("2020-07-07 10:10:26");
Long milliseconds = timestamp.getTime()+30*60*1000;
Timestamp updatedTimestamp = new Timestamp(milliseconds);
System.out.println("Updated timestamp: " + updatedTimestamp);
}
输出: 更新时间戳:2020-07-07 10:40:26.0