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.
Related
This question already has answers here:
DatePicker shows wrong value of month
(7 answers)
Closed 7 months ago.
For example, if I select December (the 12th month), then it returns 12−1=11.
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
String dDate = dayOfMonth + "/" + month + "/" + year;
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (7));
I will answer your question one by one:
1. Android Date Picker show the wrong value of month
From Calendar.MONTH documentation
Field number for get and set indicating the month. This is a
calendar-specific value. The first month of the year in the Gregorian
and Julian calendars is JANUARY which is 0; the last depends on the
number of months in a year.
So the first month (January) is 0 and the last month (December) is 11. That explains why when you select December, the date picker returns 11.
2. Disable past and date not working for appointment
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis() + (7));
I guess you want to set max date is 7 days after today, but the code you wrote is set max date is 7 milliseconds from today, that why you see the date picker disable all dates except today.
Change your code to:
datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());
calendar.add(Calendar.DAY_OF_MONTH, 7); // Add 7 days after today
datePickerDialog.getDatePicker().setMaxDate(calendar.getTimeInMillis());
I'm confused about how a Gregorian Calendar associates days with numbers. For examples, I instantiate an object
GregorianCalendar cal = new GregorianCalendar();
Toast.makeText(context, "Day: " + cal.DAY_OF_WEEK, Toast.LENGTH_LONG).show();
My toast messages keeps dispalying "Day: 7"
Today is Friday, assuming that Sunday = 0, shouldn't the text display "Day: 5"?
It does work when I do the following:
int current day = cal.get(Calendar.DAY_OF_WEEK)
Can someone explain why? Thank you.
The reason that you're getting a 7 with cal.DAY_OF_WEEK, is that you're actually asking for the value of the constant named DAY_OF_WEEK and the value of that field is 7. See here. In other words, cal.DAY_OF_WEEK is really equivalent to Calendar.DAY_OF_WEEK.
You get the correct answer with cal.get(Calendar.DAY_OF_WEEK), because you're then asking for the value of cal's DAY_OF_WEEK field.
For an easteregg in my Android app, I have to compare the current date with a stored date - and I only need to know if it's the right month.
I know that System.currentTimeMillis() is the fastest way to get the current time but now I need to get the current month from that. I avoided String comparison for it's known flaws.
My awful implementation works but it really doesn't look correct and efficient:
if (Integer.parseInt((String) DateFormat.format("MM",System.currentTimeMillis()))==12) //xmas "easteregg"
xmasBool=true;
Is there any more elegant solution for this?
Here's a better solution:
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date()); // Date's default constructor gives you current time
xmasBool = calendar.get(Calendar.MONTH) == Calendar.DECEMBER;
Calendar c = Calendar.getInstance();
int month = c.get(Calendar.MONTH);
And now you can compare the variable month with your stored month.
You can compare a day or a month or both, the whole date by formatting java.Util.Date using SimpleDateFormat.
Eg.
new SimpleDateFormat("dd").format(new java.util.Date())
gives you the "day" value. Similarly "MM" will give you the month. Use combination of those as per your requirement.
Store it in the same format and you have a common standard for comparison.
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.
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?