checking the calendar but doesn't work - android

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

Related

Call AlertDialog on a specific date each year

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) { ...}

Issue in getting difference Date

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
}

comparing date and time in date picker

I have a date picker dialog, using which user can select a date. Conditions are :
Date must not be greater than today (system date)
Date must not be older than 3 months
I have done the first bit but I am not sure how to compare two dates to check if it is older than 3 months.
in the code : checkInYear, checkInMonth and checkInDay is what user selected and year, month, day is the system date.
Could you please suggest how to compare two dates to check if it is greater than 3 months.
Your help is much appreciated.
#Override
public void onDateChanged(DatePicker view, int selectedYear,
int selectedMonthOfYear, int selectedDayOfMonth) {
setCurrentDateOnView();
int checkInYear = selectedYear;
int checkInMonth = selectedMonthOfYear;
int checkInDay = selectedDayOfMonth;
if (checkInYear > year || checkInYear < year) {
view.updateDate(year, month, day);
}
if (checkInMonth > month && checkInYear == year) {
view.updateDate(year, month, day);
}
if (checkInDay > day && checkInYear == year
&& checkInMonth == month) {
view.updateDate(year, month, day);
}
}
};
Thank you very much
Just use according to your need.
String sDate = "05-10-2012"; // suppose you create this type of date as string then
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
Date date = sdf.parse(sDate);
Calendar c = Calendar.getInstance();
c.getTime().compareTo(date);
it depending on your string or how you can get? you can get all individually from datepicker then directly set in calendar instance
Calendar my = Calendar.getInstance();
my.set(year, month, day);
now compare
my.compareTo(Calendar.getInstance());
and for less then 3 months you can use something like ..
Date referenceDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(referenceDate);
c.add(Calendar.MONTH, -3);
return c.getTime();

How can i get the String name of day on DatePicker

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.

Setting minimum year in Calendar

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

Categories

Resources