Online Java Compiler By
JavaTpoint.com
import java.util.Date; public class JavaSQLDateGetHoursExample1 { public static void main(String [] args) { //using deprecate's method alternative //using java.util.Date System.out.println("!! this output is shown by using java.util.Date as an alternate to deprecated method java.sql.Date "); java.util.Date utild=new Date(); int hours=utild.getHours(); System.out.println("Current time in hours = " + hours); System.out.println("!! this output is shown by using java.sql.Date and proves why this method is deprecated and we can't access time components in java.sql.Date class. "); //using java.sql.Date //java.sql.Date do not have time components so we can't access the time components //Thats why this method is deprecated and it will give IllegalArgumentException because we can't access time components long ctm=System.currentTimeMillis(); java.sql.Date sqld=new java.sql.Date(ctm); int sqlhours=sqld.getHours(); System.out.println("Current time in Seconds = " + sqlhours); } }
Output