I have code to get the start date of week with current day as Sunday. But I want to start my week from Saturday-Friday.
How can I achieve this through android?
This should return date of last saturday and set it as a start of the date.
Calendar cal = Calendar.getInstance();
int i = cal.get(Calendar.DAY_OF_WEEK) - cal.getFirstDayOfWeek();
cal.add(Calendar.DATE, -i - 7);
cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
cal.setFirstDayOfWeek(Calendar.Saturday);
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.Saturday);
i think its help you.
Related
I want to show a specific month to user using android CalendarView. Please help me to implement this functionality. I wrote following code but it is not working.
CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
Calendar calendar1 = Calendar.getInstance();
calendar1.set(2016, 6, 1);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(2016, 6, 30);
calendarView.setMinDate(calendar1.DATE);
calendarView.setMaxDate(calendar2.DATE);
Following output is coming when i am trying to run the app using above code.
Hint: What is the the value of Calendar.DATE? Documentation says 5, and so when you call this method, you are saying "5 milliseconds past Jan 01, 1970."
setMinDate(long minDate)
Sets the minimal date supported by this CalendarView in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
Basically, that is a static field, and probably not the value that you want.
Perhaps you want getTimeInMillis() which returns the long for the value that you set the date at?
calendarView.setMinDate(calendar1.getTimeInMillis());
calendarView.setMaxDate(calendar2.getTimeInMillis());
As From my experience i always used Calender.set(Calendar.YEAR, year) method to set year and Calender.set(Calendar.MONTH, month) method to set month and Calender.set(Calendar.DAY,day) method to set day . So i am suggesting you to do same ,
String date = "1/6/2016";
String parts[] = date.split("/");
int day = Integer.parseInt(parts[0]);
int month = Integer.parseInt(parts[1]);
int year = Integer.parseInt(parts[2]);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);
long milliTime = calendar.getTimeInMillis();
And now set the selected date in the calendar view by doing
mCalendarView.setDate (milliTime, true, true);
Say for example I have two calendar object with the format of "MM/dd/yyyy". How do I subtract them and should return the number of days? For example, "9/7/2014" - "9/1/2014" and returns 7.
Calendar calendar = Calendar.getInstance();
Calendar today = Calendar.getInstance();
You may try like this:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH,7);
calendar.set(Calendar.MONTH,8);
calendar.set(Calendar.YEAR, 2014);
Calendar today = Calendar.getInstance();
long difference = today.getTimeInMillis() - calendar.getTimeInMillis();
int days = (int) (difference/ (1000*60*60*24));
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.
ANDROID:
Wanted to know whether is there any API or workaround to know that DAY X + N DAYS ahead or behind was the day that will be / was of next month / previous month.
public static Date getCurrentDatePlusDays(int days) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(Calendar.DAY_OF_YEAR, days);
Date newDate = calendar.getTime();
return newDate;
}
then check the month or year.
I have a Date object. Now I want to add days to that Date object.
So how that can be done? Actually using Calendar object that can be done I know.
But in my case, I haven't used a calendar objects. Instead only used date object.
For Example, suppose I have a date object
Date dtStartDate=o.getStartDate();
int x=28;
Now what I want to do is to add 28 to this date object, means if the dtStartDate is 1 July 2011
then after adding 28, dtStartDate will be 29 July 2011.
Please suggest it to me.
Thanks in advance
You can add Day using below
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, 1);
Here 1 is number of Day you can add.
OR
Date dtStartDate=o.getStartDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Calendar c = Calendar.getInstance();
c.setTime(dtStartDate);
c.add(Calendar.DATE, 3); // number of days to add
String dt = sdf.format(c.getTime()); // dt is now the new date
Toast.makeText(this, "" + dt, 5000).show();
May be your problem solved.
You could add the equivilent number of milliseconds to the time retrieved from Date, e.g.:
long millis = dtStartDate.getTime();
millis = millis + x*24*60*60*1000;
Date dtEndDate = new Date();
dtEndDate.setTime(millis);
You can easily do this in two simple ways my friend. First one is:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 1);
and the second one is:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR_OF_DAY, 24);
I think you would like to find this thing. Though there are so many persons are there who choose the first method.
Thanks.