In my application i have to show dates 5 years back dates from current date like example today is 4th July 2016 but in my date picker it will show as 4th July 2011 and user can not choose date after 4th July 2011 but can choose before 4th July 2011 and backwards.
I have seen some examples like Click here
DatePickerDialog mDate = new DatePickerDialog(DatePicker.this, date, year, month, day);
maxDate = Calendar.getInstance();
maxDate.add(Calendar.YEAR, -5);
mDate.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
mDate.show();
I have tried this and its working perfectly from January 1900 to July 4 2011 but one thing i want that is when i click the date in calendar it shows January 1900 all i want is when i click it it should show from July 2011 and then backward dates.
Solution-1
You can just set :
long years4 = 126144000; //4 years in seconds
long years_millis4 = years4*1000; //4 years in milliseconds, which is required format in date picker
dateDialog.getDatePicker().setMaxDate((new Date().getTime())-years_millis4);
where dateDialog is a
new DatePickerDialog()
and the param type to set for MaxDate and MinDate is a long
Hope this works for your case.
Apart from this you can add listeners to your date picker using which you can prompt user in case he chooses any date other than the required ones.
Solution 2
You can implement your custom date picker using spinners and custom dates as the list in the spinners. Apply proper conversions while getting the input.
Make sure that java.util.Date.getTime() method returns how many milliseconds have passed since January 1, 1970, 00:00:00 GMT.
Related
I am stuck at point where I need to ask for birthdate from user as input. I need to put restriction that user should not be able to add any date before 10 year).
I think you mean users can't add any date earlier than 10 year before right? Or your users are mostly kids aged 10 to 0?!
Since your limit date is based on current date, you have to set limit programmatically using setMinDate(long date) and setMaxDate(long date). As you can see those method works with date in millisecond so you have to get dare in millis first:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -10); //Goes 10 Year Back in time ^^
long upperLimit = calendar.getTimeInMillis(); //Get date in millisecond (epoch)
, and then set the limit using above method:
datePicker.setMaxDate(upperLimit);
You could do this:
DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
datePicker.setMinDate(dateTenYearsAgo);
More info: https://stackoverflow.com/a/18353944/4235666
try with this code in datePicker dialog:
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -10);
long tenYearBack = c.getTimeInMillis();
datePickerDialog.getDatePicker().setMinDate(tenYearBack);
i want to be set month from the year in the datePicker.
my date picker is start from today date. i want to set that any one can select only future date.
example : if today date is 05-09-2016 than year start from 2016 and go on.
month start from 09 ans show only 9,10,11,12 and if i select year 2017 then month show 1-to-12.
You can specify minDate as today by adding minDate: 0 to the options
$("input.DateFrom").datepicker({
minDate: 0,
...
});
Demo:
how to disable the past date and time in android date-picker and time-picker so that user cannot select the date which has already passed and same with the time ?? please suggest me, It should be valid for future date only.
public void setMinDate (long minDate)
Added in API level 11 Sets the minimal date supported by this
NumberPicker in milliseconds since January 1, 1970 00:00:00 in
getDefault() time zone.
Parameters
minDate The minimal supported date.
Source: http://developer.android.com/reference/android/widget/DatePicker.html#setMinDate(long)
I am converting date to time, and I have a problem with minimum date and maximum date.
The minimum date time value is higher than the maximum date time value. I don't know how it is happening. Please help me understand why it is happened.
here i pass the values coming from the date picker when i select the march month 31st date 2014 year.
Date date, minPdate, maxPdate;
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
try {
date = sdf.parse(someDate);
minPdate = sdf.parse("31.02.2014");
maxPdate = sdf.parse("01.03.2014");
lmin = minPdate.getTime();
lmax = maxPdate.getTime();
Log.v("tag","min date "+lmin);
Log.v("tag","max date "+lmax);
} catch (ParseException e) {
e.printStackTrace();
}
Minimum date is March-31-2014 and Maximum date is April-01-2014.
And my result is
min date 1393804800000
max date 1393632000000
You have a wrong date in minDate:
31.02.2014 is invalid date.
try with 28.02.2014.
It seems the long value for 31st feb is treated as 3rd march.
So the long value of 1st march is less than 3rd march.
Java calendar/date APIs are generally considered (by me at least) horrible. As said, 31st feb is being treated as 3rd of march.
However, since you're using Android's DatePicker API, and you seem to want the milliseconds since epoch, you should probably use datePicker.getCalendarView().getDate(). However it might be possible that this is not available and you should just accept the fact that sometimes in java date related APIs month indexing starts with a 0 and sometimes in other classes it starts with 1.
Apparently DatePicker is modeled after Java's Calendar API, which uses 0-based indexing for months. So really you should be using a Calendar object to handle the data.
Calendar cal = new Calendar();
cal.set(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
Log.d("tag", "time in millis " + cal.getTimeInMillis());
if you want a Date you can just use cal.getTime()
When trying to get a string for the current date using
DateFormat.getDateInstance().format(calendar.getTime())
it keeps returning the wrong day. For example, it is saying today, July 25th., is July 26th. Also when I use it to sat a date picker, I get the day value by using
dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
When the date picker is set, it also shows the day ahead by 1.
To get the calendar I'm using
Calendar calendar = Calendar.getInstance();
Is there something I'm missing?
Thanks
I would imagine this is because you havent set the timezone to your timezone, and rather than the day being off randomly, the time zone you are in is diferent than GMT (Greenwich Median? Time). Try looking at this example How to handle calendar TimeZones using Java?