sqlite查询获取在netbeans上工作的android的日期差异

时间:2016-03-10 09:13:17

标签: android sqlite netbeans

我正在制作一个提醒应用程序,为此我需要计算数据库中存储日期与当前日期之间的日期差异。我已经尝试过julianday方法,但它似乎不起作用。请给出相关的sqlite查询。我是android新手所以请帮助!!!

1 个答案:

答案 0 :(得分:1)

首先从数据库查询数据以获取日期,然后将此日期数据转换为日期格式

隐藏日期格式

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

然后获取系统当前日期并将数据库数据与系统当前日期进行比较。

日期不同方法

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

}