Java Calendar behave differently - java

In the following snippet of code, I try to get the date of first day of current week, in a week system that starts from Saturday:
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.SATURDAY);
cal.set(Calendar.DAY_OF_WEEK,cal.getFirstDayOfWeek());
when I run this code here, the computed date is correctly previous Saturday (today is Tuesday by the way), but when I use this code in my Android program, date is set to next Saturday! Any idea why?

Try Calendar with the timezone or locale arguments. That way you should be able to make sure there isn't a different value for different locales / timezone.
Like this:
Calendar.getInstance(Locale.CANADA);

Related

Get day from date return differences result?

If date param is 2015-08-08, in my country (timezone = +7) the day of the month will be 8 – exactly what i want. But in my partner's country (timezone = -8) the day of the month is 7.
Can anyone tell me why?
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int day = cal.get(Calendar.DAY_OF_MONTH);
Calendar#getInstance is using the default timezone, which is by default, your own. Any changes that you see are reflected based on your local timezone.
If you want to change that, you can pass in -Duser.timezone as a parameter to your program to ensure that it starts in a specific timezone.

How to get the day of week for a given date when the week begins on a Monday?

The Calendar API is confusing me. What I'm trying to do seems simple, but doesn't really appear to work for me.
I want to set the first day of the week to be Monday, rather than the default Sunday (I know it differs by Locale, but I want to enforce always Monday). On top of that, for a given date, I want to retrieve the day of the week it represents (e.g., 1 for Monday, 7 for Sunday).
Is there any way to do this with the default Calendar API? I've tried something like:
Calendar cal = Calendar.getInstance();
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.setTime(<some Date object that's a monday>);
System.out.println(cal.get(Calendar.DAY_OF_WEEK));
It gives me a 2, instead of a 1. I suspect it's because it's giving me the value of Calendar.MONDAY, but I'm not entirely sure. Based on that suspicion, the following does work:
Calendar cal = Calendar.getInstance();
cal.setTime(<some Date object that's a monday>);
System.out.println(
(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) ?
7 : cal.get(Calendar.DAY_OF_WEEK) - 1);
... but I'd rather not have that if-statement. Is there a way to do what I want with the straight-up Calendar API?
Looking at the Calendar source it would appear that setFirstDayOfWeek really only impacts the WEEK_OF_MONTH and WEEK_OF_YEAR calculations. Regardless of what day your week starts on, MONDAY is still MONDAY, and in Calendar MONDAY has a value of 2.
Blockquote
The first day of the week is derived from the current locale. If you don't set the locale of the calendar (Calendar.getInstance(Locale), or new GregorianCalendar(Locale)), it will use the system's default. The system's default can be overridden by a JVM parameter:
public static void main(String[] args) {
Calendar c = new GregorianCalendar();
System.out.println(Locale.getDefault() + ": " + c.getFirstDayOfWeek());
}
This should show a different output with different JVM parameters for language/country:
-Duser.language=en -Duser.country=US -> en_US: 1 (Sunday)
-Duser.language=en -Duser.country=GB -> en_GB: 2 (Monday)
Don't forget that this could change other behavio(u)r too.
Blockquote
Detail can be found at following link
How to specify firstDayOfWeek for java.util.Calendar using a JVM argument

SWT DateTime strange behaviour

I'm using a SWT DateTime widget and got a very strange behaviour. If I choose any date out of the month "February", "April" or "June" the displayed date is the date I choosed but with month "January". In my datebase the right date is saved, only the displayed date in my editor is wrong. Other months are working fine. The following example is how I set my "Date"-Object within my "DateTime"-Object.
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
dateTime.setYear(calendar.get(Calendar.YEAR));
dateTime.setMonth(calendar.get(Calendar.MONTH));
dateTime.setDay(calendar.get(Calendar.DAY_OF_MONTH));
I can't explain this behaviour. For me it seems like an error within the DateTime widget. Any tips how to avoid this?
Solved the problem now. The solution is described in this bugzilla-thread: bugs.eclipse.org/bugs/show_bug.cgi?id=190254. The trick is to set the day_of_month before setting the month in my DateTime-Widget

java Calender/formatter provides wrong date

Somehow I am being reported a issue, in which following code provides date in future.
The timezone used is GMT+01:00.
The numberOfDays is non negative integer.
The intention of this code is reduce the number of days from current date.
SimpleDateFormat formatter = new SimpleDateFormat("dd MMM yy",Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -numberOfDays);
Date date = calendar.getTime();
String dateStr= formatter.format(date);
System.out.println("Date : "+dateStr);
I am not able to reproduce this on my machine.
Does the Locale affect the TimeZone?
I tried to correlate to Why does a new SimpleDateFormat object contain calendar with the wrong year?, and Strange problem with timezone, calendar and SimpleDateFormat but in vain.
Please help me understand and rectify this issue.
Well, two possibilities I can think of off the top of my head:
The system date on the client machine is incorrect, so the calendar starts with a date in the future
If numberOfDays is negative, it will obviously push the date into the future
The Locale isn't directly related to the time zone - they're independent, although obviously a machine with a French locale is likely to be in a French time zone etc.
Personally I would avoid using Date/Calendar entirely and use Joda Time as a much nicer date and time API, but that wouldn't help with either of the ideas I gave above...

Java: Is this a correct way to get the current time as a Calendar object in a particular time zone?

I know there are other similar questions to this, but I came up with my own way of getting the current time in a specific time zone, so I just wanted to confirm if it is correct or not, or there are gotchas I didn't take care of.
Calendar cal = Calendar.getInstance();
// Assuming we want to get the current time in GMT.
TimeZone tz = TimeZone.getTimeZone("GMT");
cal.setTimeInMillis(calendar.getTimeInMillis()
+ tz.getOffset(calendar.getTimeInMillis())
- TimeZone.getDefault().getOffset(calendar.getTimeInMillis()));
// Calendar should now be in GMT.
Is the above correct at all? I did my own test and it seemed to be working as expected, but just wanted to confirm it again with the experts in Stack Overflow.
If you simply do a Calendar.getInstance with the TimeZone argument, the calendar's internal state for the get() methods will return you the field with the time for that timezone. For example:
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
// if i run this at 9 EST this will print 2
System.out.println(cal.get(Calendar.HOUR));
If you just need the local time for display purposes, you can set the TimeZone on your format object. For example:
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(sdf.format(new Date()));
Like Macarse said though, Joda time is where it's at if you need to do anything more complex. The Java date APIs are a nightmare.
I'd prefer using joda-time instead of that.
Check this link.

Categories