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. 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
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
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
Java Get Month from Date
Java Get Month from Date     ... month from the current date using Java Application Language. You can see... to get month from a Date?": Java code to Get Month from Date
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
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
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
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 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
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
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
Removing character from a string in sql.
Removing character from a string in sql.  Removing special characters from a string in sql
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
Java get year from date
Java get year from date       In this section you will study how to retrieve the year from the date. You can see in the given example that we have called SimpleDateFormat class
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
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
Removing character from a string in sql.
Removing character from a string in sql.  How to remove or replace character from a string
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
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
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
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
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
JavaScript calculate age from date of birth
JavaScript calculate age from date of birth  How to calculate age from date of birth?   <html> <head> <script type="text/javascript"> function ageCount() { var date1 = new Date
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
Sql Date and Time Format
Sql Date and Time Format       The Tutorial illustrate an example from the 'Sql Date and Time...; is used to  format date and time.  SYNTAX:- DATE_FORMAT(date,format
SQL function extremely slow from 5.5
SQL function extremely slow from 5.5  SQL function extremely slow from 5.5
get data between date from msaccess database
get data between date from msaccess database  here is my code, i want to get data between date using jsp with msaccess.i stored date... like this DATE NUMBER 01-09-2012 1 02-08-2012
Reading the Date and time values from excel sheet
Reading the Date and time values from excel sheet  hi guys , iam trying to read excel sheet data using apache poi api .my problem is i cant read the date(dd/mm/yy) and time(hh:mm:ss) values concurrently from two different cells
Java convert string to date from format yyyy-mm-dd hh mm ss
Java convert string to date from format yyyy-mm-dd hh mm ss  Hi, I...; /* * Convert String from yyyy-mm-dd hh mm ss * to date object in Java */ public... date into the date format. How to convert string to date from format yyyy-mm-dd
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
create the SQL tables from an XML schema
create the SQL tables from an XML schema  I'm using Eclipse, Please let me know if anyone developped an application that would automatically create the SQL tables from an XML schema
How to create a .mdf file from script (.sql)
How to create a .mdf file from script (.sql)  Hi, I have a sql database in 2008 server. now i want it to convert to 2005. so i script it. just help me to create a .mdf file back from the script
Set encoding from within SQL*Plus
Set encoding from within SQL*Plus  Hi, In Oracle server I am running sql query from sqlplus which is adding Spanish data. But data is converted... following query in your sql at first line: alter session set NLS_LANGUAGE=SPANISH
SPEL-Wiring values from other bean properties
SpEL: Wiring value from other bean properties Spring 3 provides powerful... take an example to demonstrate how SpEL is used to wire value from other bean... the context configuration file spring-beans-list.xml from the class path
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
Importing data into sql plus from a text file...
Importing data into sql plus from a text file...  How to import a text file into oracle 10g enterprise edition directly to create tables and data
How to add value from sql server in dropdownlist
How to add value from sql server in dropdownlist  I have created a hospital management project using jsp servlet, where one form name is patient... id from other table like out patient, and this id will be stored dynamically
date range in sql
date range in sql  How can I select a random date from a date range in SQL server
Retriving value from dropdown list nad disply it in other page
Retriving value from dropdown list nad disply it in other page  Hello, I have a dropdownbox on my webpage and a map which contains a marker... of the selected value from the dropdown list and dispaly it to on the next page. i
Dialling from computer with other phone via modem card
Dialling from computer with other phone via modem card  Hello everyone I would like to create a c# desktop application that has ability to call a phone number through a modem connected in the computer. During research I ended up
select result from three tables using sql.
select result from three tables using sql.  I want to get a result from three tables. i dont how to write the sql query. Please help me. "I want to display Name,Username and Product_Name where Id=007" table "register" Name
insert rows from browsed file to sql database
insert rows from browsed file to sql database  i need to insert rows from excel to database by browsing the file in jsp. by connecting both the databases using odbc i can do this. but after browsing and uploaded the file

Ads