You can use SimpleDateFormat#format()
to format a Date into a String in a certain pattern.
String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(newstring); // 2011-01-18
Question
Does anyone know how to implement this SQL code in Java code?
to_char(to_date(time,'sssss'),'hh24:mi')
Time format is like 36000 in database and then after this sql command it is: 10:00
I want to make this function work in Java not SQL.
Solution 2
You can use SimpleDateFormat#format()
to format a Date into a String in a certain pattern.
String newstring = new SimpleDateFormat("yyyy-MM-dd").format(date);
System.out.println(newstring); // 2011-01-18
OTHER TIPS
How about this? Not tested.
public String toHH24MI(int seconds) {
int hour = seconds/3600;
int min = (seconds%3600)/60;
return String.format("%02d:%02d",hour,min);
}
It seems that there is no Java function to do that so i made up my own.
public String getTimeFromSeconds(Integer totalSecs) {
int hoursi = totalSecs / 3600;
int minutesi = (totalSecs % 3600) / 60;
String hours="";
String minutes = "";
if( (hoursi > 0 || hoursi == 0) && hoursi < 10)
hours = "0" + hoursi;
else
hours = String.valueOf(hoursi);
if( (minutesi > 0 || minutesi==0) && minutesi < 10)
minutes = "0" + minutesi;
else
minutes = String.valueOf(minutesi);
return hours + ":" + minutes;
}