How to restrict date picker from accepting current and future dates in android i am using google api...any idea..?
Since API level 11 there is a method for that:
DatePicker.setMaxDate(long maxDate)
If it has to work in previous versions, use this method:
public void init(int year, int monthOfYear, int dayOfMonth, DatePicker.OnDateChangedListener onDateChangedListener)
You could pass your own OnDateChangedListener which "resets" invalid dates to the newest valid one:
DatePicker picker = ...
int year = ...
int monthOfYear = ...
int dayOfMonth = ...
picker.init(year, monthOfYear, dayOfMonth, new DatePicker.OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// check if current date is OK
boolean dateOk = ...
if (!dateOk) {
// correct the date, but be sure the corrected date is OK
// => otherwise you might get endless recursion
year = ...
monthOfYear = ...
dayOfMonth = ...
// update the date widget with the corrected date values
view.updateDate(year, monthOfYear, dayOfMonth);
}
}
});
Related
I have a date picker dialog, using which user can select a date. Conditions are :
Date must not be greater than today (system date)
Date must not be older than 3 months
I have done the first bit but I am not sure how to compare two dates to check if it is older than 3 months.
in the code : checkInYear, checkInMonth and checkInDay is what user selected and year, month, day is the system date.
Could you please suggest how to compare two dates to check if it is greater than 3 months.
Your help is much appreciated.
#Override
public void onDateChanged(DatePicker view, int selectedYear,
int selectedMonthOfYear, int selectedDayOfMonth) {
setCurrentDateOnView();
int checkInYear = selectedYear;
int checkInMonth = selectedMonthOfYear;
int checkInDay = selectedDayOfMonth;
if (checkInYear > year || checkInYear < year) {
view.updateDate(year, month, day);
}
if (checkInMonth > month && checkInYear == year) {
view.updateDate(year, month, day);
}
if (checkInDay > day && checkInYear == year
&& checkInMonth == month) {
view.updateDate(year, month, day);
}
}
};
Thank you very much
Just use according to your need.
String sDate = "05-10-2012"; // suppose you create this type of date as string then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(sDate);
Calendar c = Calendar.getInstance();
c.getTime().compareTo(date);
it depending on your string or how you can get? you can get all individually from datepicker then directly set in calendar instance
Calendar my = Calendar.getInstance();
my.set(year, month, day);
now compare
my.compareTo(Calendar.getInstance());
and for less then 3 months you can use something like ..
Date referenceDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(referenceDate);
c.add(Calendar.MONTH, -3);
return c.getTime();
I have a serious problem here, I am building an app that will work on Arabic devices, and I need to send dates to the server, I am using Android DatePickerDialog to get the date, but the date always sent with Arabic characters, and when i try to display it again it gives me Unparsable date exception
I have tried the following solutions but no results
mDateTime = Calendar.getInstance(Locale.US).getTime();
mDateFormater.setTimeZone(TimeZone.getTimeZone("GMT"));
but non of them worked for me
any help please.
My date picker dialog code is like following
public static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
private TextView mUserView;
private Date mAffectedDate;
private SimpleDateFormat mDateFormater;
private Date mInitialDate;
public TextView getUserView() {
return mUserView;
}
public void setUserView(TextView userView) {
this.mUserView = userView;
}
public Date getAffectedDate() {
return mAffectedDate;
}
public void setAffectedDate(Date mAffectedDate) {
this.mAffectedDate = mAffectedDate;
}
public SimpleDateFormat getDateFormater() {
return mDateFormater;
}
public void setDateFormater(SimpleDateFormat mDateFormater) {
this.mDateFormater = mDateFormater;
}
public Date getInitialDate() {
return mInitialDate;
}
public void setInitialDate(Date mInitialDate) {
this.mInitialDate = mInitialDate;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance(Locale.US);
c.setTime(mInitialDate);
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) {
Date date = new Date();
date.setYear(year - 1900);
date.setMonth(month);
date.setDate(day);
mAffectedDate.setYear(year - 1900);
mAffectedDate.setMonth(month);
mAffectedDate.setDate(day);
mUserView.setText(mDateFormater.format(date));
}
}
You can use:
Calendar.set(int year, int month, int day, int hourOfDay, int minute, int second)
Sets the year, month, day of the month, hour of day, minute, and second fields. Other fields are not
changed; call clear first if this is not desired. The month value is 0-based, so it may be clearer to use a constant like JANUARY.
Like this:
Calendar cal = Calendar.getInstance(Locale.US);
public void onDateSet(DatePicker view, int year, int month, int day) {
cal.set(year, month, day, 0, 0, 0);
// get time in milliseconds
Long timeInmilliseconds = cal.getTimeInMillis();
// print time
Log.v("log", "Date "+ new Date(cal.getTimeInMillis()));
}
Also see this:
A common mistake is to implicitly use the default locale when producing output meant to be machine-readable. This tends to work on the developers test devices (especially because so many developers use en_US), but fails when run on a device whose user is in a more complex locale.
For example, if you are formatting integers some locales will use non-ASCII decimal digits. As another example, if you are formatting floating-point numbers some locales will use ',' as the decimal point and '.' for digit grouping. That is correct for human-readable output, but likely to cause problems if presented to another computer (parseDouble(String) cannnot parse such a number, for example).
I am using the example from the documentation.
From the method
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
I want the string "Mon, Sep 14, 2014". How do I get it? Ideally I really want "Mon, Sep 14, 2014 10:30PM" but I can't find a combined date and time picker.
Here ya go date time combined.
This is no meant to be mean just that you said you were having trouble finding one.
how to find it : http://bit.ly/1doSxEf
Which one I use:
https://github.com/bendemboski/DateSlider
// try this way
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
Time chosenDate = new Time();
chosenDate.set(dayOfMonth, monthOfYear, year);
long dt = chosenDate.toMillis(true);
CharSequence strDate = DateFormat.format("KKK MMM dd,yyyy", dt);
// mow use strDate..
}
I'm trying to get the actual month from the Calendar using the following:
Calendar c = Calendar.getInstance();
String time = String.valueOf(c.get(Calendar.MONTH));
According the system settings "Settings --> Date & Time" actual month is 10 while get(Calendar.MONTH) returns 09.
Keep in mind that months values start from 0, so October is actually month number 9.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#MONTH
Calendar.MONTH returns month which is zero based that is why it is giving 1 less than actual month
Add 1 to get correct value
String time = String.valueOf(c.get(Calendar.MONTH)+1);
Calendar.MONTH
returns
0 for 1st month (jan)
1 for 2nd month (feb)
.
.
11 for 12th month (dec)
Docs
So change your code to
String time = String.valueOf(c.get(Calendar.MONTH)+1);// added 1
Calendar.MONTH value starts from 0 to 11 not 1 to 12.
You may check the value of Calendar.JANUARY is 0 not 1.
Refer: http://developer.android.com/reference/java/util/Calendar.html#JANUARY
I suggest trying
#Override
public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
Toast.makeText(getApplicationContext(), month+1 + "/" + day + "/" + year, Toast.LENGTH_SHORT).show();
calDate = month+1 + "/" + day + "/" + year; }
});
use this
public void DateDialog(){
DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{
int Month;
Month=monthOfYear+1;
fromDate.setText(year + "-" + Month + "-" + dayOfMonth);
}};
DatePickerDialog dpDialog=new DatePickerDialog(getActivity(), listener, year, month, day);
dpDialog.getDatePicker().setMinDate(mcalendar.getTimeInMillis());
dpDialog.show();
}
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
dateET.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
}
I want to avoid user setting date past to current date.
So, is there a way i can get the long timestamp (may be 12:00 A.M of the date set), so that i can compare it with the current timestamp and if it is less, the date will be set to default(current date + 3 days).
Thank You
You have to do it yourself:
Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);
calendar.getTimeInMillis();