Reputation: 165
I am taking data from a sql database and then displaying this data in the form of graphs. A graph I am making bases the data off of the time. I want to get rid of the seconds because it is useless for my application and takes up room.
I tried using a calender object to remove the seconds from it like this:
ArrayList<Time> ints3 = new ArrayList<Time>();
while ( rs.next() ){
ints3.add(rs.getTime(ints.get(0)));
}
Calendar instance = Calendar.getInstance();
instance.setTime(ints3.get(1));
instance.clear(Calendar.SECOND);
ints3.set(1, (Time) instance.getTime());
This did not work however because you can not cast a java.util date into a sql time. How can I go about removing the seconds part of the time.
Upvotes: 0
Views: 3386
Reputation: 636
The answer to your need (which is to symbolize the Date without displaying the seconds) is to use the DateFormat class. Given that Time is a java.sql.Time object, convert it to a Date first:
Date myDate=new Date(Time.getTime());
DateFormat df=new SimpleDateFormat("H:mm");
String myDateStr=df.format(myDate);
Upvotes: 5
Reputation: 2060
Well you can use setTime method in Time class which accepts a long value that you can get by calling getTimeInMillis() of Calendar class(in your case after clearing seconds part).But even if you do that the Time object will take Hour,minute and the seconds part(with seconds part set to zero).If you don't want seconds part in the display then you can use concatenation of getHours and getMinutes methods of Time class.
Upvotes: 0
Reputation: 85779
This did not work however because you can not cast a java.util date into a sql time That's strange because java.sql.Time
extends java.util.Date
.
You're downcasting the result of Calendar#getTime
but this won't work. Instead, create an instance of java.sql.Time
and pass as parameter the time in millis:
//ints3.set(1, (Time) instance.getTime())
ints3.set(1, new Time(instance.getTime().getTime()));
Still, it will be way better to change ArrayList<Time>
to List<Date>
(java.util.Date
).
Upvotes: 0
Reputation: 5825
I'd like to comment this but I do not have enough reputation points. I believe this answer could help you
https://stackoverflow.com/a/21250071/2987444
You could use Jodatimes, methods to add/take away seconds and then convert back
Upvotes: 0