Subtraction of a date from Sql and other from java.

Ads
 

Subtraction of a date from Sql and other from java.

I want to subtract a date that i retrieve from mysql database with the system date that i retrieve from java. I want the difference between the two date in the integer form. how do i do it ? Please help me. Thank you !

View Answers

April 18, 2011 at 3:42 PM

import java.sql.*;
import java.util.*;
public class DateDifference{
       public int daysBetween(java.util.Date d1, java.util.Date d2){
       return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
       }
     public DateDifference(){
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select dob from student where id=7");
    java.util.Date date=null;
    if(rs.next()){
    java.sql.Date d = rs.getDate("dob");
    date = new java.util.Date(d.getTime());
    }
    java.util.Date currentDate=new java.util.Date();
     System.out.println("Days= "+daysBetween(date,currentDate));
    }
    catch(Exception e){
        System.out.println(e);
    }
    }
    public static void main(String[] args){
      DateDifference inner = new DateDifference();
      }
}

Ads









Related Tutorials/Questions & Answers:
Subtraction of a date from Sql and other from java.
Subtraction of a date from Sql and other from java.  I want to subtract a date that i retrieve from mysql database with the system date that i retrieve from java. I want the difference between the two date in the integer form
java [ cannot retrive date from sql ] why??
java [ cannot retrive date from sql ] why??   import..._date, d,m,y; public void date_mdt()throws ParseException{ String date...-yyyy"); Date da=sdf.parse(getdate); String format=sdf.format(da); try
Advertisements
How to get day from date in SQL?
How to get day from date in SQL?  Hi, How to get day from date in SQL? For your information I am working on the MySQL database. Thanks   Hi, You can use the following queries to get the data: select date_format(now
How to get month name from date(like-25/06/2012) using java?
How to get month name from date(like-25/06/2012) using java?  How to get month name from date(like-25/06/2012) using java
Java subtract days from date
Java subtract days from date In this tutorial, you will learn how to subtract days from date. Using the Calendar class, you can add and subtract date...(-). This will reduce date from date. Here, we are going to subtract few days from
Java subtract months from date
Java subtract months from date In this tutorial, you will learn how to subtract months from date. Using the Calendar class, you can add and subtract date...(-). This will reduce date from date. Here, we are going to subtract few months
Java subtract years from date
Java subtract years from date In this tutorial, you will learn how to subtract years from date. Using the Calendar class, you can add and subtract date...(-). This will reduce date from date. Here, we are going to subtract few years from
Mysql Date from Date
Mysql Date from Date       Mysql  Date from Date Time in Mysql return the Current  date. Understand with Example The Tutorial illustrate an example from 'DateTime
How to get day from date in Java using Calendar?
How to get day from date in Java using Calendar?  Hi, I have a calendar object in my Java program. How I can get the day of date from this? How to get day from date in Java using Calendar? Thanks   Hi, If you don't
how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object containing real date. date frequently vary in from of only month & year
how to get date format in string from date object containing real date in java
how to get date format in string from date object containing real date in java  i want to get data string format from date object where date object containing real date. date frequently vary in from of only month & year
Need Solution to get vaues from sql database in java
Need Solution to get vaues from sql database in java  hi I have written a code in java to get these xml tags < company comp:loc=" "/> now i need to get values from sql database for values in the blank field(" "). how do i
display date to jsp from database
display date to jsp from database   display date to jsp from database to calender if the start date and end date is available than calender date will disappeared or non-clickable and its show in red color. those date
Removing character from a string in sql.
Removing character from a string in sql.  Removing special characters from a string in sql
i want to copy files from one machine to other using java and the code will be running on third machine
i want to copy files from one machine to other using java and the code will be running on third machine  i want to copy some files from one machine say 'A' to some other machine say 'B' by using the java program running on third
update date from stored procedure
update date from stored procedure  how to works on insert,update data from stored procedure.   Insert data using stored procedure
vaidate a date from datepicker in javasript
vaidate a date from datepicker in javasript  validation should be the user should be alerted wen a future date is selected..validation can be done in javascript or jsp itself
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all... submit button should show the user various textfields on the same panel from
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all... submit button should show the user various textfields on the same panel from
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application
How to invoke other java class by selecting from dropdown menu and subsequently clicking button in java swing application  Hello Sir, first of all... submit button should show the user various textfields on the same panel from
Removing character from a string in sql.
Removing character from a string in sql.  How to remove or replace character from a string
JDBC : Get year from Date
JDBC : Get year from Date In this section, you will learn how to get year from date. Get year from Date : In MySql you can get year from the given Date... of the specified date. It ranges from 1000 to 9999 or returns 0 for no date. You can
How to move a file from a directory to other directory
How to move a file from a directory to other directory  I tried the above example it will move the folder to other directory but it does not move the image file or other file to a specified directory. Can anyone help me? Thanks
SQL Select From
SQL Select From       SQL Select From keyword show you the records from a specified table. Understand with Example The Tutorial illustrate an example from 'SQL Select From
Sql Date Conversion
Sql Date Conversion       The Tutorial illustrate an example from 'Sql Date Conversion...':-This is the date which is to be converted. %d:-Returns the date from the date you specified
Addintion in year fields of sql date in a java page
Addintion in year fields of sql date in a java page  In my database i have a field StartDate and EndDate Now entering date from my jsp..., HttpServletResponse response) throws ServletException, IOException { Date
image displaying from database with some other information
image displaying from database with some other information  hi... only a cross mark is shows. same code runs if i am not retriveing any data from..."); // smt=con.prepareStatement("select aname,aemail from admin where
ModuleNotFoundError: No module named 'date-from-num'
ModuleNotFoundError: No module named 'date-from-num'  Hi, My... 'date-from-num' How to remove the ModuleNotFoundError: No module named 'date-from-num' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'date-from-num'
ModuleNotFoundError: No module named 'date-from-num'  Hi, My... 'date-from-num' How to remove the ModuleNotFoundError: No module named 'date-from-num' error? Thanks   Hi, In your python
Retrieve date from MYSQL database
Retrieve date from MYSQL database In this tutorial, you will learn how to retrieve date from database. Storing and Retrieving dates from the database... the SQL statement to the database. Here, we are going to retrieve the date
SQL function extremely slow from 5.5
SQL function extremely slow from 5.5  SQL function extremely slow from 5.5

Ads