java hh mm ss,在java中将时间从hh:mm:ss转换为hh:mm

这篇博客讲述了如何从MySQL数据库中获取时间数据,并将其格式化为易于阅读的HH:mm格式。作者遇到的问题是尝试将时间从HH:mm:ss转换为HH:mm,但在解析过程中遇到了困难。解决方案涉及使用SimpleDateFormat进行日期格式化,首先将数据库中的时间转换为java.util.Date对象,然后使用format()方法将其转换为所需格式的字符串。
摘要由CSDN通过智能技术生成

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值