Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JDBC Subtraction of a date from Sql and other from java.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Apekshit Bhingardive
Subtraction of a date from Sql and other from java.
1 Answer(s)      3 years and 8 months ago
Posted in : JDBC


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 !


Advertisement
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
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 Get Month from Date
Java Get Month from Date     ... month from the current date using Java Application Language.  You can see... java.util package in the program code.   Java code to Get Month from Date
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 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
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
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
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
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
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
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
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
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
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
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.  How to remove or replace character from a string
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
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 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
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
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
date_parse_from_format
  date_parse_from_format date_parse_from_format function returns... about given date. Description on PHP Date Parse Format array date_parse_from...:00"; print_r(date_parse_from_format("j.n.Y H:iP", $date
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
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
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
 

 

 

DMCA.com