This question already has answers here:
Android calculate days between two dates
(17 answers)
How to get number of days between two calendar instance?
(12 answers)
Closed 2 years ago.
i create a booking hotels apps and it hase 3 textview and 2 button to choose the date. if button click it display a date and selected date will displayed to 2 textview. how to display the day between dates in the third textview??
here's my code ( the third textview not displayed as day between dates)
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
tvDateResult = (TextView) findViewById(R.id.tv_dateresult);
res = findViewById(R.id.tv_dateresult1);
ress = findViewById(R.id.tv_dateresult2);
btDatePicker = (Button) findViewById(R.id.bt_datepicker);
date = findViewById(R.id.bt_datepicker1);
get = findViewById(R.id.getprice);
date.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDateDialog2();
}
});
btDatePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDateDialog();
}
});
}
private void showDateDialog2() {
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
res.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
if(tvDateResult !=null || res !=null) {
try {
String a = tvDateResult.getText().toString();
String b = res.getText().toString();
Date aa = dateFormatter.parse(a);
Date bb = dateFormatter.parse(b);
long diff = bb.getTime() - aa.getTime() / 24 * 60 * 60 * 1000;
ress.setText((int) TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
private void showDateDialog(){
Calendar newCalendar = Calendar.getInstance();
datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
tvDateResult.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
Declare 2 global variables:
Calendar date1 = null, date2 = null;
In onDateSet method of both date pickers, save the selected date in global variables.
In onDateSet of first date picker,
Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
Similarly, In onDateSet of second date picker,
Calendar newDate = Calendar.getInstance();
date1 = newDate
updateTextView()
Call a method from both onDateSet methods.
private void updateTextView() {
if(date1 != null && date2 != null) {
tvDateResult .setText(dateFormatter.format(daysBetween(date1,date2)));
}
}
public static long daysBetween(Calendar startDate, Calendar endDate) {
// Make sure we don't change the parameter passed
Calendar newStart = Calendar.getInstance();
newStart.setTimeInMillis(startDate.getTimeInMillis());
newStart.set(Calendar.HOUR_OF_DAY, 0);
newStart.set(Calendar.MINUTE, 0);
newStart.set(Calendar.SECOND, 0);
newStart.set(Calendar.MILLISECOND, 0);
Calendar newEnd = Calendar.getInstance();
newEnd.setTimeInMillis(endDate.getTimeInMillis());
newEnd.set(Calendar.HOUR_OF_DAY, 0);
newEnd.set(Calendar.MINUTE, 0);
newEnd.set(Calendar.SECOND, 0);
newEnd.set(Calendar.MILLISECOND, 0);
long end = newEnd.getTimeInMillis();
long start = newStart.getTimeInMillis();
return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start));
}
Reference: How to get number of days between two calendar instance?
Related
This is where I am picking time from TimePicker and it works perfectly fine but I cannot get it in time variable.
eText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar cldr = Calendar.getInstance();
int hour = cldr.get(Calendar.HOUR_OF_DAY);
int minutes = cldr.get(Calendar.MINUTE);
// time picker dialog
picker = new TimePickerDialog(AddMeeting.this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker tp, int sHour, int sMinute) {
eText.setText(sHour + ":" + sMinute);
}
}, hour, minutes, true);
picker.show();
//time1 = eText.getText().toTime();
}
});
final Calendar myCalendar = Calendar.getInstance();
Use set methods in Calendar instance.
...
#Override
public void onTimeSet(TimePicker tp, int sHour, int sMinute) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, sHour);
cal.set(Calendar.MINUTE, sMinute);
Date d = cal.getTime();
}
...
I have this code, I want to add 40 weeks to the date I get from the date Picker and get the new date after the 40 weeks (280 days) has been added to the date from the date picker.
Code:
public class MainActivity extends AppCompatActivity {
DatePickerDialog picker;
EditText eText;
Button btnGet;
TextView tvw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvw=(TextView)findViewById(R.id.textView1);
eText=(EditText) findViewById(R.id.editText1);
eText.setInputType(InputType.TYPE_NULL);
eText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final Calendar cldr = Calendar.getInstance();
int day = cldr.get(Calendar.DAY_OF_MONTH);
int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
// date picker dialog
picker = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
eText.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, year, month, day);
picker.show();
}
});
btnGet=(Button)findViewById(R.id.button1);
btnGet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tvw.setText("Selected Date: "+ eText.getText());
}
});
}
}
Joda time is a very convenient library for handling such cases. Add this to your project:
dependencies {
compile 'joda-time:joda-time:2.10.2'
}
And then you can manipulate the dates like this:
DateTime dt = DateTime.now();
DateTime laterDate = dt.withYear(2020)
.withMonthOfYear(3)
.withDayOfMonth(14)
.plusWeeks(40);
Remember that in JodaTime date objects are immutable (which is a very good idea), so each manipulation produces a new object.
First, convert the current format to milliseconds and then add specific days milliseconds and then again get it in the desired format. Like this way:
new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(year,monthOfYear + 1,dayOfMonth);
long timeInMilliseconds =
calendar.getTimeInMillis()+TimeUnit.DAYS.toMillis(280);
calendar.setTimeInMillis(timeInMilliseconds);
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
eText.setText(mDay + "/" + mMonth + "/" + mYear);
}
}, year, month, day);
picker.show();
}
});
Use add(Calendar.DAY_OF_MONTH, int) function in this way:
cldr.add(Calendar.DAY_OF_MONTH, 280);
Logically speaking, you don't have to add 40 weeks per say, a week has specific number of days, thus you can just add 40*7 = 280 days in your current day picked up from date picker.
[current date] + TimeUnit.DAYS.toMillis(280)
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Date date = (Date) getArguments().getSerializable(ARG_DATE);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
View v = LayoutInflater.from(getActivity())
.inflate(R.layout.dialog_date, null);
mDatePicker = (DatePicker) v.findViewById(R.id.dialog_date_picker);
mDatePicker.init(year, month, day, null);
return new AlertDialog.Builder(getActivity())
.setView(v)
.setTitle(R.string.date_picker_title)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
int year = mDatePicker.getYear();
int month = mDatePicker.getMonth();
int day = mDatePicker.getDayOfMonth();
Date date = new GregorianCalendar(year,
month, day).getTime();
sendResult(Activity.RESULT_OK, date);
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
}
I feel like getTime is what
My problem is, but I cant fix it
I just want to display the date
and not the time at all but it wont let me. It always comes back as MM DD nn hhmmss zzz. All I want is the time to be displayed. here is where I want to display it
private TextView mTitleTextView;
private TextView mDateTextView;
private Crime mCrime;
public CrimeHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.list_item_crime, parent, false));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return false;
}
});
mTitleTextView = (TextView) itemView.findViewById
(R.id.crime_tile);
mDateTextView = (TextView) itemView.findViewById
(R.id.crime_date);
}
public void bind(Crime crime) {
mCrime = crime;
mTitleTextView.setText(mCrime.getTitle());
mDateTextView.setText(mCrime.getDate().toString());
}
In my list view here it always shows the format with the time in it and I just want the day, month, day of month, and year. Not sure if this makes sense, but does anyone have any ideas?
This is how I handle dates:
public static String DATE_FORMAT_NOW="MMM d yyyy"
final SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
final Calendar cal = Calendar.getInstance();
final String timestamp = "Last Modified: " + sdf.format(cal.getTime());
You can change the DATE_FORMAT_NOW to your needs
This particular format will put the date as: Jul 15 2017
SimpleDateFormat might have a lint warning, you can add SuppressLint to it if you want. I've never had any issue doing it this way
I want to use the calendarDaysBetween() below method in my TextView, or I want my TextView to display difference of two dates. Can anyone help me with this?
public class MainActivity extends FragmentActivity {
static EditText metTodate, metFromdate, metInTime, metOutTime;
static long no_of_days1;
static long no_of_days2;
Button mbtnApplyLeave;
ImageView mivBack;
RadioButton halfday1, halfday2, first_half, second_half, first_half1, second_half1, full_day1, full_day2;
static TextView no_of_days;
static TextView no_of_days3;
public static String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
metTodate = (EditText)findViewById(R.id.etTodate);
metFromdate = (EditText)findViewById(R.id.etFromdate);
metInTime = (EditText)findViewById(R.id.etInTime);
metOutTime = (EditText)findViewById(R.id.etOutTime);
mivBack = (ImageView)findViewById(R.id.ivBack);
halfday1 = (RadioButton)findViewById(R.id.halfday1);
halfday2 = (RadioButton)findViewById(R.id.halfday2);
first_half = (RadioButton)findViewById(R.id.firsthalf1);
second_half = (RadioButton)findViewById(R.id.secondhalf1);
first_half1 = (RadioButton)findViewById(R.id.firsthalf2);
second_half1 = (RadioButton)findViewById(R.id.secondhalf2);
full_day1 = (RadioButton)findViewById(R.id.fullday1);
full_day2 = (RadioButton)findViewById(R.id.fullday2);
no_of_days = (TextView)findViewById(R.id.etnoofdays);
// Here is my method where I want my text view to display dates.
no_of_days.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calendarDaysBetween(Calendar metFromdate, Calendar metTodate);
}
});
}
// Both date picker dialogs
public void showTruitonDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metTodate.setText(day + "/" + (month + 1) + "/" + year);
}
}
public void showFromDatePickerDialog(View v) {
DialogFragment newFragment = new FromDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
public static class FromDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c1 = Calendar.getInstance();
int year = c1.get(Calendar.YEAR);
int month = c1.get(Calendar.MONTH);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day1);
// calendarDaysBetween(Calendar metFromdate, Calendar metTodate);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metFromdate.setText(day + "/" + (month + 1) + "/" + year);
}
}
public static long calendarDaysBetween(Calendar metFromdate, Calendar metTodate) {
// Create copies so we don't update the original calendars.
Calendar start = Calendar.getInstance();
start.setTimeZone(metFromdate.getTimeZone());
start.setTimeInMillis(metFromdate.getTimeInMillis());
Calendar end = Calendar.getInstance();
end.setTimeZone(metTodate.getTimeZone());
end.setTimeInMillis(metTodate.getTimeInMillis());
// Set the copies to be at midnight, but keep the day information.
start.set(Calendar.HOUR_OF_DAY, 0);
start.set(Calendar.MINUTE, 0);
start.set(Calendar.SECOND, 0);
start.set(Calendar.MILLISECOND, 0);
end.set(Calendar.HOUR_OF_DAY, 0);
end.set(Calendar.MINUTE, 0);
end.set(Calendar.SECOND, 0);
end.set(Calendar.MILLISECOND, 0);
// At this point, each calendar is set to midnight on
// their respective days. Now use TimeUnit.MILLISECONDS to
// compute the number of full days between the two of them.
no_of_days1 = TimeUnit.MILLISECONDS.toDays(Math.abs(end.getTimeInMillis() - start.getTimeInMillis()));
String finalresult = new Double(no_of_days1).toString();
no_of_days.setText(finalresult);
return no_of_days1;
}
}
try this
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
Date past = format.parse("05/06/2015");
Date now = new Date();
System.out.println(TimeUnit.MILLISECONDS.toMinutes(now.getTime() - past.getTime()) + " minutes ago");
System.out.println(TimeUnit.MILLISECONDS.toHours(now.getTime() - past.getTime()) + " hours ago");
System.out.println(TimeUnit.MILLISECONDS.toDays(now.getTime() - past.getTime()) + " days ago");
It will give you difference in minute and hours also from current date, you can use your own date format and own date objects
You can use the following code to get a date difference in number of days.
public void calendarDaysBetween(Calendar metFromdate, Calendar etTodate)
{
long diff = (metFromdate.getTimeInMillis() - metTodate.getTimeInMillis());
long no_of_days1 = Math.abs(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
no_of_days.setText(no_of_days1+"");
}
First declear Calendar object
public Calendar metFromdate= Calendar.getInstance();
public Calendar metTodate== Calendar.getInstance();
Change first DatePicker values
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metFromdate.set(Calendar.YEAR, year);
metFromdate.set(Calendar.MONTH, month);
metFromdate.set(Calendar.DAY_OF_MONTH, day);
}
change second DatePicker values
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
metTodate.set(Calendar.YEAR, year);
metTodate.set(Calendar.MONTH, month);
metTodate.set(Calendar.DAY_OF_MONTH, day);
}
change button click
no_of_days.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calendarDaysBetween(metFromdate, metTodate);
}
});
you used it
Calendar date1 = Calendar.getInstance(); Calendar date2 =
Calendar.getInstance();
date1.clear();
date1.set(datePicker1.getYear(),datePicker1.getMonth(),datePicker1.getDayOfMonth());
date2.clear();
date2.set(datePicker2.getYear(),datePicker2.getMonth(),datePicker2.getDayOfMonth());
long diff = date2.getTimeInMillis() - date1.getTimeInMillis();
float dayCount = (float) diff / (24 * 60 * 60 * 1000);
textView.setText(Long.toString(diff) + " " + (int) dayCount);
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
........
........
compile 'joda-time:joda-time:2.2'
}
and
public String getTimeAgo(long ago){
DateTime timeAgo = new DateTime(ago*1000L);
DateTime now = new DateTime();
Period period = new Period(timeAgo, now);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.appendYears().appendSuffix(" Years, ")
.appendMonths().appendSuffix(" Months, ")
.appendWeeks().appendSuffix(" Weeks, ")
.appendDays().appendSuffix(" Days, ")
.appendHours().appendSuffix(" Hours, ")
.appendMinutes().appendSuffix(" Minutes, ")
.appendSeconds().appendSuffix(" Seconds ago ")
.printZeroNever()
.toFormatter();
String elapsed = formatter.print(period);
return elapsed;
}
and
yourTextView.setText(getTimeAgo(long-your_past_date_millis));
I have two Datepickers dialog for ToDate[start] and FromDate[end].
I am setting both datePicker max date is current date by using setMaxDate() function.
My issue is that when
1) I m change the date of ToDatePicker I wont set setMaxDate() of FromDatePicker.
2) I m change the date of FromDatePicker I wont set setMinDate() of ToDatePicker.
In My code this work 1st time but next time when I m changing both or single datepicker it not set setMaxDate(), setMinDate() function
here is my code
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
Calendar newCalendar = Calendar.getInstance();
//Setting first None value to updatedDate To and from
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedToDate, Constant.NoValue);
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedFromDate, Constant.NoValue);
datePickerDiaFrom=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar upDateFrom = Calendar.getInstance();
upDateFrom.set(year, monthOfYear, dayOfMonth);
txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));
// System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());
//setting minimum limit to date Picker 'To'
if (dateFormatter.format(upDateFrom.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate()))) {
upDateFrom.set(Calendar.HOUR_OF_DAY, upDateFrom.getMinimum(Calendar.HOUR_OF_DAY));
upDateFrom.set(Calendar.MINUTE, upDateFrom.getMinimum(Calendar.MINUTE));
upDateFrom.set(Calendar.SECOND, upDateFrom.getMinimum(Calendar.SECOND));
upDateFrom.set(Calendar.MILLISECOND, upDateFrom.getMinimum(Calendar.MILLISECOND));
datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
System.out.println("setting To DatePicker current date as minDate : "
+dateFormatter.format(NetworkUtil.getTodayDate()));
}else{
datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
System.out.println("setting To DatePicker minDate : "
+dateFormatter.format(upDateFrom.getTime()));
}
//datePickerDiaTo.getDatePicker().setMinDate(upDateFrom.getTimeInMillis());
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedFromDate, dateFormatter.format(upDateFrom.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDiaTo=new DatePickerDialog(MgntNCActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Calendar upDateTo = Calendar.getInstance();
upDateTo.set(year, monthOfYear, dayOfMonth);
txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));
// System.out.println("Date 1 )GTM : "+upDateTo.getTime().toGMTString());
//setting maximum limit to date Picker 'From'
if (dateFormatter.format(upDateTo.getTime()).equals(dateFormatter.format(NetworkUtil.getTodayDate()))) {
upDateTo.set(Calendar.HOUR_OF_DAY, upDateTo.getMaximum(Calendar.HOUR_OF_DAY));
upDateTo.set(Calendar.MINUTE, upDateTo.getMaximum(Calendar.MINUTE));
upDateTo.set(Calendar.SECOND, upDateTo.getMaximum(Calendar.SECOND));
upDateTo.set(Calendar.MILLISECOND, upDateTo.getMaximum(Calendar.MILLISECOND));
datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
System.out.println("setting From DatePicker current date as maxDate : "
+dateFormatter.format(NetworkUtil.getTodayDate()));
}else{
datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
System.out.println("setting From DatePicker maxDate : "
+dateFormatter.format(upDateTo.getTime()));
}
//datePickerDiaFrom.getDatePicker().setMaxDate(upDateTo.getTimeInMillis());
Util.SaveToSharedPref(MgntNCActivity.this,
Constant.ShaPreMgntNcFilter,
Constant.ShaPreMgntNcFilterUpdatedToDate, dateFormatter.format(upDateTo.getTime()));
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
//setting max limit to date Picker
datePickerDiaTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
datePickerDiaFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
Any one please suggest me solution of this ...
Thanks...
finally I found solution :)
I m writing datePickerDialogs 'datePickerDiaFrom' , 'datePickerDiaTo' are class variables that's why when I m changing minDate or maxDate at that time it not replace previous date .
My solution is ,
each time when I click on textview to open new dialogPicker and set range to it.
here is my code :
static long MaximumDate=0,MinimunDate=0;
dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
View.OnClickListener showDatePicker = new View.OnClickListener() {
#Override
public void onClick(View v) {
final View vv = v;
Calendar newCalendar=Calendar.getInstance();
switch (vv.getId()) {
case R.id.txtFromUpdatedDate_MgntNC:
DatePickerDialog dialogFrom=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar upDateFrom = Calendar.getInstance();
upDateFrom.set(year, monthOfYear, dayOfMonth);
txtFromUpdatedDate.setText(dateFormatter.format(upDateFrom.getTime()));
MinimunDate=upDateFrom.getTimeInMillis();
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
if (MaximumDate!=0) {
dialogFrom.getDatePicker().setMaxDate(MaximumDate);
}else{
dialogFrom.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
}
dialogFrom.show();
dialogFrom.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
MinimunDate=0;
txtFromUpdatedDate.setText(Constant.NoValue);
}
});
break;
case R.id.txtToUpdatedDate_MgntNC:
DatePickerDialog dialogTo=new DatePickerDialog(MgntNCActivity.this, new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
Calendar upDateTo = Calendar.getInstance();
upDateTo.set(year, monthOfYear, dayOfMonth);
txtToUpdatedDate.setText(dateFormatter.format(upDateTo.getTime()));
MaximumDate=upDateTo.getTimeInMillis();
}
}, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
dialogTo.getDatePicker().setMaxDate(NetworkUtil.getTodayDate().getTime());
if (MinimunDate!=0) {
dialogTo.getDatePicker().setMinDate(MinimunDate);
}else{
//dialog1.getDatePicker().setMinDate(NetworkUtil.getTodayDate().getTime());
}
dialogTo.show();
dialogTo.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface arg0) {
MaximumDate=0;
}
});
break;
default:
break;
}
}
};
txtFromUpdatedDate.setOnClickListener(showDatePicker);
txtToUpdatedDate.setOnClickListener(showDatePicker);