I try to call AlertDialog on a certain date every year when launching an app that day that is defined but something fails and I ask for your help. Note, for example, on November 11 of each year, it is necessary to run AlertDialog all day when launching the app, and when November 12 start, AlertDialog will not be displayed until November 11 of the next year. Thanks in advance.
Calendar start = Calendar.getInstance();
start.set(Calendar.YEAR, Calendar.NOVEMBER, 11);
Date today = start.getTime();
if(start.equals(today)){
showStartDialog();
}
You can try
Calendar start = Calendar.getInstance();
Date today = start.getTime();
start.set(calendar.get(Calendar.YEAR), Calendar.NOVEMBER, 11)
if(start.getTime().equals(today)){
showStartDialog();
}
Or you can just
Calendar start = Calendar.getInstance();
int month = start.get(Calendar.MONTH)
int dayOfMonth = start.get(Calendar.DAY_OF_MONTH)
if (month == Calendar.November && dayOfMonth == 11) { ...}
Related
I only need Today and yesterday date and when other date is selected it should give error....I am getting every thing ok but there is small issue
Here is my code:
if (flag == 0) {
if (getday(myCalendar.getTime()) == 1 || getday(myCalendar.getTime()) == 0) {
setvisitdate();
}
Toast.makeText(getApplicationContext(),
"Selected date is not valid...", Toast.LENGTH_SHORT)
.show();
flag = 0;
new DatePickerDialog(FvrActivity.this, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
here is method of getday:
private int getday(Date selected_date) {
Calendar selected_cal = Calendar.getInstance();
selected_cal .setTime(selected_date);
Calendar c = Calendar.getInstance();
c.getTime();
long diffInMillisec = c.getTimeInMillis() - selected_cal.getTimeInMillis();
long diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMillisec);
return (int) diffInDays;
}
Suppose today's date is 21/11/2016...I just want to select 21 date and 20 and not 22 but here i can select 22 also..Thank you in advance for solving.
Use this logic:
Calendar selected_cal = Calendar.getInstance();
selected_cal.setTime(selected_date);
//For next day
Calendar nextDay = Calendar.getInstance();
nextDay.add(Calendar.DATE, 1);//Adding one day
nextDay.set(Calendar.HOUR_OF_DAY, 0);//Setting calendar for start hour of the day
nextDay.set(Calendar.MINUTE, 0);//Setting calendar for start minute of the day
nextDay.set(Calendar.SECOND, 0);//Setting calendar for start second of the day
//For previous days
Calendar previousDay = Calendar.getInstance();
previousDay.add(Calendar.DATE, -2);//Adding one day
previousDay.set(Calendar.HOUR_OF_DAY, 23);//Setting calendar for start hour of the day
previousDay.set(Calendar.MINUTE, 59);//Setting calendar for start minute of the day
previousDay.set(Calendar.SECOND, 59);//Setting calendar for start second of the day
if(selected_cal.getTimeInMillis()>=nextDay.getTimeInMillis()
|| selected_cal.getTimeInMillis()<previousDay.getTimeInMillis()){
//Show error
}else{
//Do what you want to do
}
I'm developing a simple app that the user pick some date on DatePicker and return the current day name in some Toast message.
but when I'm trying to do that it has given me a wrong date.
my code:
final int day = datepicker.getDayOfMonth();
datepicker.getDrawableState();
int mounth = datepicker.getMonth();
mounth+=1;
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, day);
String formatDay = new SimpleDateFormat("E").format(cal.getTime());
Toast.makeText(MainActivity.this, formatDay, 2000).show();
You're setting the day of month when you create the Calendar, but you aren't setting the month or year. You need to getMonth() and getYear() of the DatePicker and set those on the Calendar also. Do GregorianCalendar cal = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 0, 0) instead of Calendar.getInstance(). With getInstance() you are getting a Calendar for the current time, then just changing the day.
This question already has answers here:
Android datepicker min max date before api level 11
(7 answers)
Closed 9 years ago.
I want to set date between 1990 and present date in date picker dialog in versions below honey comb,it is possible for versions above honey comb using functions setMinDate() and setMaxDate(),I tried the following code and it is possible to set either mindate or max date for lower versions,,the code is
calendar = new GregorianCalendar();
mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
mDatePickerField.setAccessible(true);
DatePicker datePicker = (DatePicker) mDatePickerField.get(dialog);
final int maxYear = calendar.get(Calendar.YEAR);
final int maxMonth = calendar.get(Calendar.MONTH);
final int maxDay = calendar.get(Calendar.DAY_OF_MONTH);
datePicker.init(maxYear, maxMonth, maxDay,
new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year,
int month, int day) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, day);
if (newDate.after(calendar)) {
view.init(maxYear, maxMonth, maxDay, this);
}
}
});
Log.w(TAG, "API Level < 11 so not restricting date range...");
Please give me some new code or change the existing code for datepicker supporting both mindate and maxdate,,,,,thanks
I think you should use update instead of init.
replace this line
view.init(maxYear, maxMonth, maxDay, this);
with
view.updateDate(maxYear, maxMonth, maxDay);
I try to check the calendar when evey first day of each month will auto send data but the problem code doesn't work.
Calendar c = Calendar.getInstance();
if (Calendar.DAY_OF_MONTH == 25){
sendMessage("L100000000");}
I think you need this
Calendar cal = Calendar.getInstance();
int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
if (dayOfMonth == 25){
sendMessage("L100000000");
}
I want to use a calendar in android. Within the calendar, I want to set the minimum year of 1998; T the user should not able to enter a year before that. How can I solve this problem? By default it goes before 1998.
I am using following code:
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
mDatePickerDialog = new DatePickerDialog(WinningNumberPowerBall.this, mDateSetListener, cyear, cmonth,cday);
mDatePickerDialog.show();
By using this my calendar year starts from 1900 but I want to start the year at 1998. How can I do this?
I think you can just do:
...
Calendar minCal = new GregorianCalendar(1998, Calendar.JANUARY, 1);
mDatePickerDialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
mDatePickerDialog.show();