How to compare date and year? - android

i am implementing one graph related app.
In that when application start i am getting present month,present year from calende and display on the screen with graph. like April 2012 and graph
ok that is fine.
In my screen i have two buttons like preview,next.
when user clicks preview ,i want to change April 2012 to March 2012,Fib 2012 ..........
And when user clicks next button,i want to change April 2012 to May 2012.....
How i will do these type of comparigens.
If any one know please help me.
Thanks in advance

Use Calendar Instance to set time.. When user presses next increment month by one and set it to Calendar and get Date from it.. and The opposite for previous.. But using default DatePicker is a lot better Option..

For this purpose you can use the add(int, int) method on a Calendar instance.
mCalendar.add(Calendar.Month, 1) // next month
mCalendar.add(Calendar.Month, -1) // previous month

Calendar mCalendar = Calendar.getInstance();
int cmonth = mCalendar.get(Calendar.MONTH);
int cyear = mCalendar.get(Calendar.YEAR);
//when you click next in click event call this below three lines
mCalendar.add(Calendar.MONTH, 1); // next month
cmonth = mCalendar.get(Calendar.MONTH);
cyear = mCalendar.get(Calendar.YEAR);
//when you click previous in click event call this below tthree lines
mCalendar.add(Calendar.MONTH, -1); // previous month
cmonth = mCalendar.get(Calendar.MONTH);
cyear = mCalendar.get(Calendar.YEAR);

Related

How to allow the user to choose date only before 10 years from today from datepicker in android?

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

Calendar in Android app to select month and year

I want to have a calendar that would allow the user to only choose month and year. Are there any 3rd party widgets that would allow me to do that? I know about date slider (http://i.stack.imgur.com/BDFls.png) that allows me to create a dialog box where user would choose month and year, but is there a widget that is similar to what I posted below? Or would I have to create this myself?
I would recommend CalDroid. It has customization options to allow you to create the desired effect.
Edit: Read the documentation on the project's GitHub:
Caldroid fragment includes 4 main parts:
1) Month title view: show the month and year (e.g MARCH, 2013)
2) Navigation arrows: to navigate to next month or previous month
3) Weekday gridview: contains only 1 row and 7 columns. To display "SUN, MON, TUE, WED, THU, FRI, SAT"
4) An infinite view pager that allow user to swipe left/right to change month. This library is taken from https://github.com/antonyt/InfiniteViewPager
Number (1) seems to be what you need.
Personally, for this reasonably simple use case, I'd not bother with some 3rd-party thing you can't easily debug/maintain. I'd use two ViewFlippers -- one for month and one for day. ViewFlipper supports animation, so you can get some nice looking physics to it when the user spins to a month/day.
yes, there is, Calendar Times Square provides to you a calendar to display and then a way to choose from it some date:
first, import the calendar into your project.
then, display it the following way:
//get the current date:
Calendar firstYear = Calendar.getInstance();
Calendar lastYear = Calendar.getInstance();
firstYear.add(Calendar.YEAR, -10);
lastYear.add(Calendar.YEAR, 10);
// to declare the calendar with the current date and make it scrollable for a past year and an upcoming year
final CalendarPickerView calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
calendar.init(firstYear.getTime() , lastYear.getTime())
.withSelectedDate(Calendar.getInstance().getTime());
then, when want to save the selected date:
Date new_date = calendar.getSelectedDate();
Calendar calendar = Calendar.getInstance();
calendar.setTime(new_date);
// to make sure it saved to the system
if (ShellInterface.isSuAvailable()) {
ShellInterface.runCommand("chmod 666 /dev/alarm");
SystemClock.setCurrentTimeMillis(calendar.getTimeInMillis());
ShellInterface.runCommand("chmod 664 /dev/alarm");
}

Setting android datepicker min date to specific date

I want the user to be able to pick start date and end date. The minimum of end date must be equal to start date. For example the user selects August 1 2014 as a start date. When he clicks on the return date field these values are passed to date_picker_activity as intent extras. In the date_picker_activity I check for these extras and currently it just sets the selected date as the displayed date:
if(getIntent().getExtras()!=null)
{
int year = getIntent().getExtras().getInt("SELECTED_YEAR");
int month = getIntent().getExtras().getInt("SELECTED_MONTH");
int day = getIntent().getExtras().getInt("SELECTED_DAY");
date_picker.updateDate(year, month, day);
}
but the user is still able to pick dates prior to selected start date.
I tried to do something like that inside that if statement:
Time time = new Time();
time.set(day, month, year);
date_picker.setMinDate(time.toMillis(true)-1000);
but that didn't work.
How can I set the min date with the passed values?
After discussing on chat, we managed to understand that this code:
date_picker.setMinDate(System.currentTimeMillis() - 1000);
was being called anyway inside the if statement, setting the minimum date to the current Date.
Once Igal moved that code to an else statement, all is working ok.

Setting the time for both DatePicker fields in a DatePickerDialog

I am trying to create a DatePickerDialog when a certain TextView is clicked, and then set the date picked to be that TextView. I have gotten this to work, but when the DatePickerDialog is shown, the dates for left DatePicker are correctly set to the current date, while the calendar on the right is set to November 2100. How can I access the field of calendar and set its date?
Here is a link to an image of what I am talking about, but is not from my application exactly.
http://i1023.photobucket.com/albums/af358/shaikhhamadali/typesofdialog_4_zps078711ac.png
So, I am talking about the calendar on the right that does not start on the current date. I would like to know how to access it? If I'm not mistaken, this calendar will only show on tablets, so is there a "safe" way to do this where running it on a phone would not cause any problems?
Here is the code from my DatePickerFragment innerclass onCreateDialog method
#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);
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), this, year, month, day);
datePickerDialog.setTitle("Enter date");
datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L * 86400000L);
return datePickerDialog;
}
Thank you for any help!
UPDATE:
Update: I have tried adding accessing the CalendarView through both, but the CalendarView still starts up at Nov 2100
datePickerDialog.getDatePicker().getCalendarView().setDate(Calendar.getInstance().getTimeInMillis() / 86400000L *86400000L);
datePickerDialog.getDatePicker().getCalendarView().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L 86400000L);
Solution
This long answer (to a question about practically the same bug) suggests a workaround for this issue.
Part of it quoted here:
formatDateRange() does not work past 2038.
Workaround #1
class DatePickerDialog1964 extends DatePickerDialog {
DatePickerDialog1964(Context c) {
super(c, null, 2013, 4, 21);
#SuppressWarnings("deprecation")
Date min = new Date(2013-1900, 4, 21);
DatePicker p = getDatePicker();
CalendarView cv = p.getCalendarView(); // should check for null
long cur = cv.getDate();
int d = cv.getFirstDayOfWeek();
p.setMinDate(min.getTime());
cv.setDate(cur + 1000L*60*60*24*40);
cv.setFirstDayOfWeek((d + 1) % 7);
cv.setDate(cur);
cv.setFirstDayOfWeek(d);
}
}
Workaround #2 I actually used
// Calendar view is a cascade of bugs.
// Work around that by explicitly disabling it.
datePicker.setCalendarViewShown(false);
My Deduction
By elimination, we can deduce that the line datePickerDialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis() / 86400000L * 86400000L); is causing the November 2100 error.
(UNIX time / 86400000L) * 86400000L --I guess it's supposed to take advantage of rounding to produce a 'full' day. Beware of it rounding to the same day if the current time is after noon, the previous day if before. It isn't chopping after the floating point.
About the updated part:
Even calling setMinDate then setDate isn't really solving anything -- The problem that caused the Nov 2100 error will persist, because the updated line's effect is executed by the original code.
Then I looked around, and I found the above linked answer.
I guess CalendarView is stuffed with bugs.
There appears to be another NumberPicker bug where the previous month and date and shown sometimes, even if not accessible. They disappear when we try to access them.
P.S: I know this is late, but since the OP didn't provide any solution, an analysis might be helpful to somebody.

Why is this generating 3913 as the year

I wrote some code that I am try to use as a comparison for a subscription so this piece of code was supposed to dump the current date. The month and day are correct but the year is off by about 1900 or so any ideas as to why
Calendar calendar=Calendar.getInstance();
Date validDate = new Date(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
complain(validDate.toString());
complain is a function I wrote to simply the dump the value of a string to the screen in an alert box
It shows this 3913-02-10
I need to get this fixed before I start doing comparisons so the quicker the better
OK I added some new code and not using the depracated Date any longer but now my month is of by 1 it shows 2013 1 10
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
complain(String.valueOf(year) + " " + String.valueOf(month) + " " + String.valueOf(day));
After further research I found that MONTH returns the correct value but I have to say this is VERY unintuitive I would think that it would coincide with normal understandings of the date format
This has January set to 0, February to 1, March to 2, etc. it is non intuitive.
Thanks to all
You are using a deprecated constructor that takes the year - 1900 as its first argument.
Depending on what you're gonna end up doing with the app, take this with a grain of salt:
Just subtract 1900 from the year and send that to your method or whatever you're doing.
Note: THIS IS A TERRIBLE PROGRAMMING PRACTICE and if this is anything other than self-education, FIND A BETTER WAY.

Categories

Resources