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.
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("
"+ts+"
");thanks for your help.
解决方案
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);