4

I have data+time in saved in database (sq lite) in milliseconds, now I want to get data from sq-lite of a specific date and I have date in this format "26-December-2012", how to compare this with milliseconds.

what should be the query to fetch data from database?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Saqib Abbasi
  • 607
  • 2
  • 6
  • 17

3 Answers3

1

You have to convert the milliseconds into date format then compare two dates

convert into date formate

        public static String getDate(long milliSeconds, String dateFormat)
{`enter code here`
    // Create a DateFormatter object for displaying date in specified format.
    DateFormat formatter = new SimpleDateFormat(dateFormat);

    // Create a calendar object that will convert the date and time value in milliseconds to date. 
     Calendar calendar = Calendar.getInstance();
     calendar.setTimeInMillis(milliSeconds);
     return formatter.format(calendar.getTime());
}

compare dates

SimpleDateFormat curFormater = new SimpleDateFormat("dd/MM/yyyy"); 
Date date1 = curFormater.parse(date1Str); 
Date date2 = curFormater.parse(date2Str); 
if (date1.before(date2)) 
{
}
Mudassar Shaheen
  • 1,397
  • 1
  • 9
  • 15
0

Simply create a new Calendar with the timeInMilliseconds data from the database.

So, if you have the time in a column called date and the data is in a table called myTable the query to get that is:

select date from myTable ... other constraints

In android, simply use the long value retrieved from the database to construct a new Calendar:

Calendar cal = new Calendar.getInstance();
cal.setTimeInMillis(timeInMsFromDatabase);

Once you have a Calendar object, you can retrieve the values you want with the get(int field) method.

Or, you can use the DateFormat class.

Make sense?

dcow
  • 7,765
  • 3
  • 45
  • 65
0

I hope this will be helpful to you

    public long getDateLong(String dateString, String format) throws ParseException
    {
        SimpleDateFormat f = new SimpleDateFormat(format);

        Date d = f.parse(dateString);

        return d.getTime();
    }

//

     long timeMillis; // Your long time millis
     boolean compare = timeMillis > getDateLong("26-December-2012", "dd-MMMM-yyyy");