Java:将今天的时间转换为时间戳
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5170309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: convert a time from today to a timestamp
提问by Jon Cox
I am using Java 6, and I have a time from the current date as a string, like this: 14:21:16
, and I need to convert this to a Timestamp
objectto store in a database.
我正在使用 Java 6,并且我有一个从当前日期开始的时间作为字符串,如下所示: 14:21:16
,我需要将其转换为Timestamp
对象以存储在数据库中。
However there seems to be no good way to get a Timestamp from this. Timestamp.valueOf(String)
is quite close, but requires a date. Is there a good way to make a Timestamp object from such a string?
但是,似乎没有什么好方法可以从中获取时间戳。Timestamp.valueOf(String)
很接近,但需要约会。有没有一种从这样的字符串制作 Timestamp 对象的好方法?
采纳答案by Sean Patrick Floyd
How about this:
这个怎么样:
final String str = "14:21:16";
final Timestamp timestamp =
Timestamp.valueOf(
new SimpleDateFormat("yyyy-MM-dd ")
.format(new Date()) // get the current date as String
.concat(str) // and append the time
);
System.out.println(timestamp);
Output:
输出:
2011-03-02 14:21:16.0
2011-03-02 14:21:16.0
回答by Jon Skeet
Personally, I'd use Joda Timeto parse the time to a LocalTime
, and add that to today's LocalDate
to get a LocalDateTime
, then convert that into an Instant
using whatever time zone you're interested in. (Or use LocalTime.toDateTimeToday(DateTimeZone)
.)
就个人而言,我会使用Joda Time将时间解析为LocalTime
,并将其添加到今天的LocalDate
以获取LocalDateTime
,然后Instant
使用您感兴趣的任何时区将其转换为。(或使用LocalTime.toDateTimeToday(DateTimeZone)
.)
Then just create a time stamp using the Timestamp(long)
constructor.
然后只需使用Timestamp(long)
构造函数创建一个时间戳。
There are plenty of other approaches (e.g. using SimpleDateFormat
instead of parsing with Joda Time, if you really want...) but ultimately you're likely to want the Timestamp(long)
constructor in the end. (The benefit of using Joda Time here is that it's obvious what's being represented at each stage - you're not trying to treat a "time only" as a "date and time" or vice versa.)
还有很多其他方法(例如,使用SimpleDateFormat
而不是解析 Joda Time,如果你真的想要......)但最终你可能最终想要Timestamp(long)
构造函数。(在这里使用 Joda Time 的好处是很明显每个阶段所代表的内容 - 您不会试图将“仅时间”视为“日期和时间”,反之亦然。)
回答by Tom Quarendon
Best I can come up with using standard API is not that pretty:
我能想到的最好的使用标准 API 并不是那么漂亮:
// Get today's date and time.
Calendar c1 = Calendar.getInstance();
c1.setTime(new Date());
// Get the required time of day, copy year, month, day.
Calendar c2 = Calendar.getInstance();
c2.setTime(java.sql.Time.valueOf("14:21:16"));
c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));
// Construct required java.sql.Timestamp object.
Timestamp time = new Timestamp(c2.getTimeInMillis());
Let's see what we've done.
System.out.println(time);
Note that java.sql.Time.valueOf accepts a string of the form "HH:MM:SS" as you require. Other formats would require use of SimpleDateFormat.
请注意, java.sql.Time.valueOf 根据需要接受“HH:MM:SS”形式的字符串。其他格式需要使用 SimpleDateFormat。
回答by Thomas
Use org.apache.commons.lang.time.DateUtils:
使用 org.apache.commons.lang.time.DateUtils:
Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date time = df.parse("14:21:16");
Timestamp time = new Timestamp(today.getTime() + time.getTime());
回答by corsiKa
Have a given day (say, unix epoch?) to serve as the day. When you use it, only use the time parameters that you care about, ignoring the day.
有一个给定的一天(比如,unix epoch?)作为这一天。使用的时候,只使用自己关心的时间参数,不考虑日期。
Another option would be java.sql.Time
另一种选择是 java.sql.Time
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Time.htm
http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Time.htm
回答by Mark Pope
String str = "14:21:16";
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date date = formatter.parse(str);
Timestamp timestamp = new Timestamp(date.getTime());