问题描述
我想将时间转换为hh:mm from hh:mm:ss
它来自数据库(我的sql),格式为hh:mm:ss。
我尝试了以下代码,但我没有得到我想要的。
I want to convert time to hh:mm from hh:mm:ssit 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.
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>");
感谢您的帮助。
推荐答案
您需要将 java.util.Date
对象转换为 java.lang.String
object使用。您不能再次将其转换回 Date
(或其任何子类,如 java.sql.Time
等等)不失格式。该格式即不存储在 Date
中。 Date
只知道自大纪元以来的毫秒数。
You need to convert a java.util.Date
object to a java.lang.String
object representing the human readable time in the desired format using DateFormat#format()
. You cannot convert it back to Date
again (or any of its subclasses like java.sql.Time
and so on) without losing the format. The format is namely not stored in Date
. The Date
only knows about the amount of milliseconds since the Epoch.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
java.util.Date date = object.getTimeFrom();
String string = sdf.format(date);
System.out.println(string);
这篇关于在java中将时间从hh:mm:ss转换为hh:mm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!