There are some countries which first day of week begin on Sunday, instead other countries like mine begin on Monday. Is there any way to know which calendar is used in the country of the user.
I'm developing an application in Android with a calendar and I don't know how to solve this problem.
Set your country timeZone in Calendar class.
Calendar cal = Calendar.getInstance();
TimeZone tz = TimeZone.getTimeZone("GMT");
cal.setTimeZone(tz);
or
you can add the offset by if using UTC date or time.
Related
I want to show calendarView that will set the minimum date as today for booking purposes. But in my code it will show the whole month and here I can book the previous date. So, I need a solution that will show dates available from today and can not select previous days.
How can I do this?
My code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DATE,Calendar.getInstance().getActualMinimum(Calendar.DATE));
long date = calendar.getTime().getTime();
calendar_view.setMinDate(date);
Here is a solution. You need to set current date to the calendar minimum date.
Calendar calendar = Calendar.getInstance();
mCalendarView.setMinDate(calendar.getTimeInMillis());
I want to get which day of year is it.
a year have 365 days . I want to get which day base on the data , i have .
for example :
my date is : 2017/01/03 ---> base on this data I found that it means day 2.
You can use Calendar class to get your desired output:
Calendar cal = Calendar.getInstance(); // get calendar object instance
cal.setTime("set your time here"); // set your desired time here, it can be as Date object, if you've millis then use setTimeInMillis();
cal.get(Calendar.DAY_OF_YEAR); // get day of year like this
I am stuck at point where I need to ask for birthdate from user as input. I need to put restriction that user should not be able to add any date before 10 year).
I think you mean users can't add any date earlier than 10 year before right? Or your users are mostly kids aged 10 to 0?!
Since your limit date is based on current date, you have to set limit programmatically using setMinDate(long date) and setMaxDate(long date). As you can see those method works with date in millisecond so you have to get dare in millis first:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -10); //Goes 10 Year Back in time ^^
long upperLimit = calendar.getTimeInMillis(); //Get date in millisecond (epoch)
, and then set the limit using above method:
datePicker.setMaxDate(upperLimit);
You could do this:
DatePicker datePicker = (DatePicker) findViewById(R.id.event_date);
datePicker.setMinDate(dateTenYearsAgo);
More info: https://stackoverflow.com/a/18353944/4235666
try with this code in datePicker dialog:
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -10);
long tenYearBack = c.getTimeInMillis();
datePickerDialog.getDatePicker().setMinDate(tenYearBack);
How can I construct a Calendar object using getInstance(TimeZone) to use the device's TimeZone?
According to this issue, just using:
Calendar calendar = Calendar.getInstance();
will give you an instance in the user's default time zone (as per their settings). You can determine the time zone from the calendar with:
TimeZone zone = calendar.getTimeZone();
Other posts have suggested that using TimeZone.getDefault() does not give this user-default time zone - I don't know about that personally, but it's another option to look into.
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?