Manisha Mishra
Manisha Mishra

Reputation: 3

sqlite query to get date difference in android working on netbeans

I am making a reminder app and for that I need to calculate the date difference between the stored date in database with the current date. I have tried the julianday method but it doesn't seems to work. Please give the relevant sqlite query. I am new to android so please help!!!

Upvotes: 0

Views: 59

Answers (1)

Md Abdul Gafur
Md Abdul Gafur

Reputation: 6201

At First Query Data from database for getting date then convert this date data to Date Format

Covert Date to Date Format

String dateString = "03/26/2012 11:49:00 AM";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Then get System Current date and Compare database data to System current date.

Date Different Method

public void printDifference(Date startDate, Date endDate){

//milliseconds
long different = endDate.getTime() - startDate.getTime();

System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("different : " + different);

long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;

long elapsedDays = different / daysInMilli;
different = different % daysInMilli;

long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;

long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;

long elapsedSeconds = different / secondsInMilli;

System.out.printf(
    "%d days, %d hours, %d minutes, %d seconds%n", 
    elapsedDays,
    elapsedHours, elapsedMinutes, elapsedSeconds);

}

Upvotes: 1

Related Questions