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
}
Related
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) { ...}
In my app, I want to show the list of appointments that a User has in this week. The arguments to be passed to the API are fromDate and toDate in milliseconds (long). I need to know how can I get the date in milliseconds of the last day of the week (Consider the week starts from Sunday and ends on Saturday).
// get today and clear time of day
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0); // ! clear would not reset the hour of day !
cal.clear(Calendar.MINUTE);
cal.clear(Calendar.SECOND);
cal.clear(Calendar.MILLISECOND);
// get start of this week in milliseconds
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
System.out.println("Start of this week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());
// start of the next week
cal.add(Calendar.WEEK_OF_YEAR, 1);
System.out.println("Start of the next week: " + cal.getTime());
System.out.println("... in milliseconds: " + cal.getTimeInMillis());
for the last day of week add 6 there !!
This can be achieved by adding first day of week as Sunday and then add 6 days to it.
// Get current time. You can reset hours/ minutes/ seconds it with default values which you want.
Calendar c = new GregorianCalendar(Locale.getDefault());
// Set first day of week
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
// Add 6 into that to get Saurday
c.add(Calendar.DAY_OF_WEEK, 6);
// Get millisenconds for Saturday
long millies = c.getTimeInMillis();
Please check the below code to get the upcoming saturday in milliseconds.
public class NextSaturdayMain
{
public static void main(String[] args)
{
System.out.println(getSaturday(new Date()));
}
public static long getSaturday(Date today)
{
Calendar cal = Calendar.getInstance();
cal.setTime(today);
int dow = cal.get(Calendar.DAY_OF_WEEK);
while (dow != Calendar.SATURDAY) {
int date = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
if (date == getMonthLastDate(month, year)) {
if (month == Calendar.DECEMBER) {
month = Calendar.JANUARY;
cal.set(Calendar.YEAR, year + 1);
} else {
month++;
}
cal.set(Calendar.MONTH, month);
date = 1;
} else {
date++;
}
cal.set(Calendar.DATE, date);
dow = cal.get(Calendar.DAY_OF_WEEK);
}
System.out.println(cal.getTime());
return cal.getTimeInMillis();
}
private static int getMonthLastDate(int month, int year)
{
switch (month)
{
case Calendar.JANUARY:
case Calendar.MARCH:
case Calendar.MAY:
case Calendar.JULY:
case Calendar.AUGUST:
case Calendar.OCTOBER:
case Calendar.DECEMBER:
return 31;
case Calendar.APRIL:
case Calendar.JUNE:
case Calendar.SEPTEMBER:
case Calendar.NOVEMBER:
return 30;
default: // Calendar.FEBRUARY
return year % 4 == 0 ? 29 : 28;
}
}
}
I have a custom dialog with a datepicker and a time picker in it. The user sets the Date which all works fine. The date picker is the hidden and the time picker is shown. I am currently setting the time on the timepicker manually to 8 am.
I now want to convert the user set time in the time picker to a long which I am able to do however its showing me the current time on the phone in the logcat and not the actual set time... Thanks!
button_continue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (timeset == false) {
Calendar calendar = Calendar.getInstance();
calendar.set(datePickerDiet.getYear(), datePickerDiet.getMonth(), datePickerDiet.getDayOfMonth());
long startTime = calendar.getTimeInMillis();
System.out.println(startTime);
// save to shared pref
ProfilePrerences.getInstance().setLongValue(DietActivity.this, ProfilePrerences.KEY_START_DIET_DAY, startTime);
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dateString = formatter.format(new Date(startTime));
System.out.println(dateString);
datePickerDiet.setVisibility(View.GONE);
time_breakfast.setVisibility(View.VISIBLE);
dialog_txt.setText("At what time do you have breakfast?");
time_breakfast.setCurrentHour(8);
time_breakfast.setCurrentMinute(0);
time_breakfast.clearFocus();
timeset = true;
}
else if (timeset == true) {
// time_breakfast.setVisibility(View.VISIBLE);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(time_breakfast.getCurrentHour(), time_breakfast.getCurrentMinute(), 0);
long breakfasttime = calendar2.getTimeInMillis();
System.out.println(breakfasttime);
SimpleDateFormat formatter2 = new SimpleDateFormat("hh:mm");
String dateString2 = formatter2.format(new Date(breakfasttime));
System.out.println(dateString2);
// startdietdialog.cancel();
ProfilePrerences.getInstance().setLongValue(DietActivity.this, ProfilePrerences.KEY_BREAKFAST_TIME, breakfasttime);
timeset = false;
}
}
});
This line is causing you the problem:
calendar2.set(time_breakfast.getCurrentHour(), time_breakfast.getCurrentMinute(), 0);
This is setting the year, month, day on calendar2 - not the hour, minute, second you intended.
Probably the easiest solution is call the direct methods - setHour, setMinute, etc., on calendar2.
Two things: you're printing the current date (new Date):
String dateString2 = formatter2.format(new Date(breakfasttime));
System.out.println(dateString2);
You have to print calendar2 time:
String dateString2 = formatter2.format(calendar2.getTime());
System.out.println(dateString2);
The other is, Greg Ennis said, you're setting calendar2 time incorrectly: there is not such method to set only the hour, minutes and seconds. You should set year, month and day also or call set(Calendar.HOUR, h), set(Calendar.MINUTE, m), etc separately
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.
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");
}