Android Date & TimePicker Condition - android

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)

Related

How to set date limit in date picker

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.

Setting a specific date in datePickerdialog

I have setMaxDate in datepickerdialog. Its working well, but the minimum value in year segment is 1980. How can i set that value to, say 1900.
my code is :
DatePickerDialog dp=new DatePickerDialog(getParent(),datesetListener,day,month,year);
dp.getDatePicker().setMaxDate(System.currentTimeinMillis());
You can get the underlying DatePicker from a DatePickerDialog (by simply calling getDatePicker()) and set its bounds using:
setMinDate(long minDate)
setMaxDate(long maxDate)
Where the argument is the usual number of milliseconds since January 1, 1970 00:00:00 in the default time zone. You'll still have to calculate these values of course, but that should be trivial to do with the Calendar class: just pass current date and add or substract x years from that.
Referenced from here and here
Sample code
Date min = new Date(2013-1900, 4, 21);
DatePicker p = getDatePicker();
p.setMinDate(min.getTime());
Try this dp.setMinDate(new Date(1900,1,1));

Convert date into time I am getting wrong value in android

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()

Converting milliseconds to Date object

I am having following code to convert milliseconds to Android Date object.
Date dateObj = new Date(milli);
But problem is that my milliseconds value is having GMT value added in it before i pass it to Date class, add when i print this date object i can see that date object is again adding GMT value in the milliseconds value and because of that my date is displayed as wrong.
So how can i generate Date object with out considering GMT value in it.
For example my milliseconds are 1385569800000 which is getting printed as below:
Wed, 27 Nov 2013 22:00:00 --> +5.30
But the current value of this time stamp without adding GMT is:
Wed, 27 Nov 2013 16:30:00
*UPDAE*
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
So basically i want to create Date object which has proper date time value in it with out adding extra GMT time added in it.
A Date is always in UTC. No need to change that.
When printing the date value, use SimpleDateFormat and call setTimeZone() on it before formatting the output string.
It is not just about printing the date in right format and with right date time.
But i want to use that date object to schedule TimeTask.
TimerTask is just a task and not its scheduling. Timer accepts a Date object for scheduling. The Date is in UTC there as well.
try my code if you a
long currentTime = System.currentTimeMillis();
TimeZone tz = TimeZone.getDefault();
Calendar cal = GregorianCalendar.getInstance(tz);
int offsetInMillis = tz.getOffset(cal.getTimeInMillis());
currentTime -= offsetInMillis;
Date date = new Date(currentTime);
it is work for me
You can try with joda-time API.
Joda-Time provides a quality replacement for the Java date and time classes. The design allows for multiple calendar systems, while still providing a simple API. The 'default' calendar is the ISO8601 standard which is used by XML. The Gregorian, Julian, Buddhist, Coptic, Ethiopic and Islamic systems are also included, and we welcome further additions. Supporting classes include time zone, duration, format and parsing.
http://joda-time.sourceforge.net/key_instant.html
A Date object simply represents a moment in time. Imagine you're on the phone to someone on a different continent, and you say "3...2...1...NOW!". That "NOW" is the same moment for both of you, even though for one person it's 9am and for the other it's 4pm.
You're creating a Date representing the moment 1385569800000 milliseconds after the Java epoch (the beginning of 1970, GMT). That is your "NOW", and it's fixed and unchanging. What it looks like converted into text, however, depends on which timezone you want to display it for. Java defaults to using GMT, which would be right if you were in Britain during the winter, but for (I'm guessing) India you want it in a different time zone. Laalto's answer shows you how to do that.
here is the code,that worked like charm for me:
public static String getDate(long milliSeconds, String dateFormat)
{
// Create a DateFormatter object for displaying date in specified format.
DateFormat formatter = new SimpleDateFormat(dateFormat);
// Create a calendar object that will convert the date and time value in milliseconds to date.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
return formatter.format(calendar.getTime());
}

Calendar dayOfMonth off by 1

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?

Categories

Resources