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();
      }
}









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 ... an example from 'DateTime in Mysql'. To grasp this example, we use date(current
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... tutorial of "How to get month from a Date?": Java code to Get Month
Sql query from oracle
Sql query from oracle  Please help me that I want to get the table names form the existing database who does it having eid coloumn plz provide me query in oracle database
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 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
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
Removing character from a string in sql.
Removing character from a string in sql.  Removing special characters from a string in sql
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
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
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 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
date_parse_from_format
  date_parse_from_format date_parse_from_format function returns... date_parse_from_format ( string $format , string $date ) Parameters of Date...;25.8.2009 17:00-03:00"; print_r(date_parse_from_format("j.n.Y H:iP"
SQL Select From
SQL Select From       SQL Select From keyword show you the records from a specified table. Understand with ExampleADS_TO_REPLACE_1 The Tutorial illustrate an example from 'SQL Select
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... the year of the specified date. It ranges from 1000 to 9999 or returns 0
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
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
To retrieve image from SQL Server Database - Java Server Faces Questions
To retrieve image from SQL Server Database  Sir/Madam, I am trying to retrieve image from SQL Server 2000 database in Visual Web JSF Page using... or in Image Component. please help me in retrieving and displaying image from SQL Server
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... to retrieve the date of birth of persons from the database table. For this, we have
delete data from database - SQL
delete data from database  HOw to delete specific data from table> I want to delete one record from database. Thnx  Hi friend, -------------------------------------------- Visit for more information
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
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 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
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
SQL function extremely slow from 5.5
SQL function extremely slow from 5.5  SQL function extremely slow from 5.5
date_interval_create_from_date_string
date_interval_create_from_date_string date_interval_create_from_date_string... from the relative parts of the string. It uses the normal date parsers and sets up... DateInterval instance if success. Description about Set Up Dateinterval From Date
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
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
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
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
create a xml from sql server 2005 - XML
create a xml from sql server 2005  hello Dear, i want to know how we create a xml file which retrieve data from Sql server 2005 using java. i am briefing my problem as follows. let i have a table in sql server 2005
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)ADS
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
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... file spring-beans-list.xml from the class path. (adsbygoogle
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
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
retrive the result of a serach string from other sites - JSP-Servlet
retrive the result of a serach string from other sites  Sir, suppose i have create my own websites, and i want to search an item in my sites and as well as from other search sites and i want to show all the search result in my

Ads