Agus Virga Tendean
Agus Virga Tendean

Reputation: 1

Convert String to java.sql.Time and java.sql.Time to Double

I have a String "02:30" for example. I want to convert it to java.sql.Time, from java.sql.Time then convert it again to double, so be 2.5 hours ..... What is the best solution for it ???

Upvotes: 0

Views: 1149

Answers (1)

user7205283
user7205283

Reputation:

This is your code:

 public static void main(String[] args) {
     String res="02:30";
      Time tm=new Time(Integer.valueOf(res.split(":")[0]), Integer.valueOf(res.split(":")[1]), 0);
      Double dbl=new Double(tm.getHours()+Math.floor(tm.getMinutes()*100/60)/100);
      System.out.println(dbl.toString());
}

Note The constructor Time(int, int, int) is deprecated, we should use long instead.

Upvotes: 1

Related Questions