Tuesday 8 October 2013

date difference in mysql , date difference between two date in java


Here we calculate date difference by mysql or java in both way :-



MYSQL

DATEDIFF() Functions returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

SELECT DATEDIFF('2013-10-30','2013-09-11') from dual; --  will return 49 days 
SELECT DATEDIFF('2013-09-30','2013-10-11') from dual; --   will return -11 days 

Java

package com.javastoreroom.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateDiffrence {

 public static void main(String[] args) {

  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  String dateStart = "2013-09-05";
  String dateStop = "2013-10-08";

  try {
   Date date = format.parse(dateStart);
   Date date1 = format.parse(dateStop);

   long time = date1.getTime() - date.getTime();
   long day = time / (24 * 60 * 60 * 1000);
   System.out.println("count total day ------- :"+day); // line print number of day between these two date count total day ------- :33

  } catch (ParseException e) {
   e.printStackTrace();
  }

 }

}

DONE :)

No comments:

Post a Comment