I have tried many questions posted here. Still I have not found solution to my problem,
I want to compare dates enter by user, in my android app, I store the dates in a ArrayList.
I dont want to store clone to already saved dates, so I have to check. I am using Java sql date to avoid time part.
static List<DatefromTo> myList;
myList = new ArrayList<Dateclassobject>();
i =myList.size();
for(int j = 1; j <= myList.size(); j ++){
int results2 = myList.get(i).datefrom.compareTo(myList.get(j).datefrom);
if(results2 > 0 ){
Toast.makeText(getApplicationContext(),"First Date is after second",Toast.LENGTH_LONG).show();
}else if(results2 < 0){
Toast.makeText(getApplicationContext(), "Wrong", Toast.LENGTH_LONG).show();
}else if(results2 == 0){
Toast.makeText(getApplicationContext(),String.valueOf(myList.get(i).datefrom) + "------" + String.valueOf(myList.get(j).datefrom),Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), "Hi", Toast.LENGTH_LONG).show();
}
}
datefrom and dateto are stored in everyobject of Dateclassobject
So what I want to achieve is that if user has entered a time period from datefrom to dateto, and that time period is already present in another time period or is present in list itself it wont get enter again. So how can I check this at best?
Also I would like to know why getYear(), or getDate() give wrong answer?
Here is the DateClassObject code :
import java.util.Date;
public class DateClassObject {
public static Date datefrom;
public static Date dateto;
public static Date difference;
public DateClassObject() {
super();
}
@SuppressWarnings("static-access")
public DateClassObject(Date datefrom, Date dateto) {
super();
this.datefrom = datefrom;
this.dateto = dateto;
}
}
Below is the whole code of this class Dateadd , where I am doing everything
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
//import java.util.Date;
import java.util.List;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class DateAdd extends Activity {
private Button buttonsubmit;
DatePickerDialog dpdfrom, dpdto;
Date dtFrom, dtTo;
long milisto, milisfrom, days, diff;
int i;
Button delete;
TableLayout stk;
TextView tvadd, tvadd2, tvadd3, tvfrompick, tvtopick, t1v;
String abc;
DatefromTo datefromto, dtFT;
int listSize;
int mYearfrom, mMonthfrom, mDayfrom, mYearto, mMonthto, mDayto;
static List<DateClassObject> myList;
Calendar from; // get the from date from user and store here
Calendar to; // get the to date from user and store here
EditText et;
String datetoday, difference, string1, string2;
Long bringto, bringfrom, between;
boolean wrong;
boolean entryremover;
// boolean yrfm , mnfm , dyfm , yrto , mnto , dyto;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dateadd);
from = Calendar.getInstance();
to= Calendar.getInstance();
final Calendar c = Calendar.getInstance();
mYearfrom = c.get(Calendar.YEAR);
mMonthfrom = c.get(Calendar.MONTH);
mDayfrom = c.get(Calendar.DAY_OF_MONTH);
mYearto = c.get(Calendar.YEAR);
mMonthto = c.get(Calendar.MONTH);
mDayto = c.get(Calendar.DAY_OF_MONTH);
buttonsubmit = (Button) findViewById(R.id.buttonsubmit);
et = (EditText) findViewById(R.id.et);
// tvspecial = (TextView) findViewById(R.id.tvspecial);
tvfrompick = (TextView) findViewById(R.id.tvfrompick);
tvtopick = (TextView) findViewById(R.id.tvtopick);
myList = new ArrayList<DateClassObject>();
i = (myList.size()-1);
init();
if (entryremover == true) {
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myList.remove(dtFT);
}
});
}
buttonsubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dtFT = new DatefromTo(from,to);
if(from.before(to)){
myList.add(dtFT);
AddEntry();
}
for(int j = 0; j < myList.size(); j ++){ //Note. you've written,
//for(int j = 1; j <= myList.size(); j ++) , ArrayList index start from 0. you'll miss the first element. I hope it helps you in the future
Calendar dateFrom = myList.get(j).dateFrom;
Calendar dateTo = myList.get(j).dateTo;
if(isSameDay(from, dateFrom) && isSameDay(to, dateTo)){ // checking same period allready exists
Toast.makeText(getApplicationContext(),
"Already exist",
Toast.LENGTH_LONG).show();
}
else{
// if it does not exists, I assume that you want to add it your arrayList
myList.add(new DatefromTo(from, to));
}
t1v.setText((myList.get(j).dateFrom
+ "\n" + myList.get(j).dateTo + "\n"));
}
tvfrompick.setText("");
tvtopick.setText("");
}
});
tvfrompick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dpdfrom.show();
}
});
tvtopick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dpdto.show();
}
});
dpdfrom = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tvfrompick.setText(dayOfMonth + "-" + monthOfYear + 1
+ "-" + year);
mDayfrom = dayOfMonth;
mMonthfrom = (monthOfYear);
mYearfrom = year;
// dtFrom = new Date(mYearfrom - 1900, mMonthfrom,mDayfrom);
from.set(mYearfrom, mMonthfrom,
mDayfrom);
}
}, mYearfrom, mMonthfrom, mDayfrom);
dpdto = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@SuppressWarnings({ "deprecation", "deprecation" })
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tvtopick.setText(dayOfMonth + "-" + monthOfYear + 1
+ "-" + year);
mDayto = dayOfMonth;
mMonthto = (monthOfYear);
mYearto = year;
// dtTo = new Date((mYearto - 1900), mMonthto, mDayto);
to.set( mYearto, mMonthto, mDayto);
}
}, mYearto, mMonthto, mDayto);
}
public boolean isSameDay(Calendar cal1, Calendar cal2) {
if (cal1 == null || cal2 == null) {
throw new IllegalArgumentException("null");
}
return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
}
public void init() {
stk = (TableLayout) findViewById(R.id.table_main);
TableRow tbrow0 = new TableRow(this);
TextView tv0 = new TextView(this);
tv0.setText("From");
tv0.setTextColor(Color.WHITE);
tv0.setPadding(20, 10, 20, 10);
tbrow0.addView(tv0);
TextView tv1 = new TextView(this);
tv1.setText(" To " + myList.size());
tv1.setTextColor(Color.WHITE);
tv1.setPadding(20, 10, 20, 10);
tv1.getPaddingLeft();
tbrow0.addView(tv1);
// Button tv2 = new Button(this);
// tv2.setText("Delete ");
// tv2.getPaddingLeft();
// //tv2.setTextColor(Color.WHITE);
// tbrow0.addView(tv2);
stk.addView(tbrow0);
}
public void AddEntry() {
entryremover = true;
TableRow tbrow = new TableRow(this);
t1v = new TextView(this);
// t1v.setText("From" + i);
myList.get(myList.size() - 1);
t1v.setTextColor(Color.WHITE);
t1v.setGravity(Gravity.CENTER);
t1v.setPadding(20, 10, 20, 10);
tbrow.addView(t1v);
delete = new Button(this);
delete.setText("Delete");
delete.setTextColor(Color.WHITE);
delete.setGravity(Gravity.CENTER);
delete.setPadding(20, 10, 20, 10);
tbrow.addView(delete);
stk.addView(tbrow);
}
public void removeEntry() {
}
}
