java 在java中将时间从hh:mm:ss转换为hh:mm
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2949699/
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
converting time from hh:mm:ss to hh:mm in java
提问by kawtousse
I want to convert time to hh:mm from hh:mm:ss it comes from database (my sql) in the form of hh:mm:ss. I tried the following code but i didn't get what i want.
我想将时间从 hh:mm:ss 转换为 hh:mm 它以 hh:mm:ss 的形式来自数据库(我的 sql)。我尝试了以下代码,但没有得到我想要的。
try {
s= HibernateUtil.currentSession();
tx=s.beginTransaction();
Query query = s.createQuery("select from Dailytimesheet dailytimesheet where dailytimesheet.IdDailyTimeSheet=6083 " );
for(Iterator it=query.iterate();it.hasNext();)
{
if(it.hasNext())
{
Dailytimesheet object=(Dailytimesheet)it.next();
String dt=object.getTimeFrom().toString();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
long ms=0;
try {
ms = sdf.parse(dt).getTime();
}
catch (ParseException e) {e.printStackTrace();}
Time ts = new Time(ms);
out.println("<h2>"+ts+"</h2>");
thanks for your help.
谢谢你的帮助。
回答by BalusC
You need to convert a java.util.Dateobject to a java.lang.Stringobject representing the human readable time in the desired format using DateFormat#format(). You cannot convert it back to Dateagain (or any of its subclasses like java.sql.Timeand so on) without losing the format. The format is namely not stored in Date. The Dateonly knows about the amount of milliseconds since the Epoch.
您需要使用 将java.util.Date对象转换为以java.lang.String所需格式表示人类可读时间的对象DateFormat#format()。您不能在不丢失格式的情况下Date再次将其转换回(或其任何子类java.sql.Time,等等)。格式即不存储在Date. 该Date只知道自从毫秒为单位的量。
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
java.util.Date date = object.getTimeFrom();
String string = sdf.format(date);
System.out.println(string);
回答by Michael Krelin - hacker
Converting HH:MM:SS to HH:MM is best done by hhmmss.substring(0,5);-) Alternatively, you may prefer to format the data right in the SELECTstatement.
最好通过hhmmss.substring(0,5);-)将 HH:MM:SS 转换为 HH:MM或者,您可能更喜欢在SELECT语句中正确格式化数据。
回答by John Uckele
Perhaps you could add a new method to Dailytimesheet called getTimeWithoutSecondsFrom() which copies the date object and then sets the copy's seconds to zero before returning the date.
也许您可以向 Dailytimesheet 添加一个名为 getTimeWithoutSecondsFrom() 的新方法,该方法复制日期对象,然后在返回日期之前将副本的秒数设置为零。
As an added benefit you can eschew the SimpleDateFormat sdf bit altogether then and the code will be easier to read.
作为额外的好处,您可以完全避开 SimpleDateFormat sdf 位,代码将更易于阅读。
The code might look like this:
代码可能如下所示:
public Date getTimeWithoutSecondsFrom()
{
Date result = this.getTimeFrom();
result.setSeconds(0);
return(result);
}
回答by Matschie
What about trying the java.util.Calendar Class? You could do something like this:
尝试 java.util.Calendar 类怎么样?你可以这样做:
Calendar c = new Calendar();
Calendar.setDate(new Date(object.getTimeFrom().toString()));
String date = Calendar.HOUR + ":" + Calendar.MINUTE
Or an I not getting your question?
或者我没有得到你的问题?
回答by goedson
You should use the DateFormat's format method. Something like:
您应该使用DateFormat的格式方法。就像是:
out.println("<h2>"+sdf.format(ts)+"</h2>");
回答by Mayank Gupta
try {
String strDate = object.getTimeFrom().toString();
SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
Date date = sdf1.parse(strDate);
System.out.println(sdf2.format(date));
} catch (ParseException e) {
e.printStackTrace();
}
回答by CoolBeans
I would just do the conversion in the SQL in this case. Here is the MySql referenceon date conversion. In your case, it should be something like this -
在这种情况下,我只会在 SQL 中进行转换。这是关于日期转换的 MySql参考。在你的情况下,它应该是这样的 -
Blockquote DATE_FORMAT('2007-10-04 22:23:00', '%H:%i');
Blockquote DATE_FORMAT('2007-10-04 22:23:00', '%H:%i');
It looks like you are needing to do the conversion for each row you get from your query and it might be more cost effective to have it done by the database engine if you are retrieving many records.
看起来您需要对从查询中获得的每一行进行转换,如果您要检索许多记录,由数据库引擎完成转换可能更具成本效益。

