lundi 9 mai 2016

Only fetch date from datetime value of db sqlite android? Compare with current date and the then display time of the saved value?

I am trying to fetch date from my db, where its stored with time. I want to compare the date with in my adapter, if the date is equal to the current date then adapter fetch time only of the corresponding date, and if date is not equal to the current date then adapter display the date of the corresponding value,

This is how I am inserting date+time in DB:

long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("MMM-dd-yyyy h:mm:a");
String dateString = sdf.format(date);
Log.i("result", s.getTime());
DatabaseHandler db = new DatabaseHandler(this);
db.creatScan(new Scan(s.getMsg().toString(), s.getTime().toString()));

In my Adapter:

 TextView details = (TextView)convertView.findViewById(;

        TextView times = (TextView)convertView.findViewById(;


This is the Result:

enter image description here

What I want is: It should only display the time, if the date is equal to the current date, else it should only display the date.

I am trying this if/else statement but its not working, Your help would be very appreciated, thanks in advance

long date = System.currentTimeMillis();

SimpleDateFormat sdfDate = new SimpleDateFormat("MMM-dd-yyyy");

String dateString = sdfDate.format(date);

Log.i("date", dateString);

SimpleDateFormat sdfTime = new SimpleDateFormat("h:mm:a");

String timeString = sdfTime.format(date);

Log.i("date", timeString);


    Log.i("data", timeString);
}else {
    String time =  scan.getTime().toString();
    Log.i("date", time);

Aucun commentaire:

Enregistrer un commentaire