SWT DateTime strange behaviour - java

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

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.

Java Calendar behave differently

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

Calendar.getTime().getYear() gives back 0113 instead of 2013

I'm making a little game. When the game is starting for the first time i saves the time since 01.01.1970 in seconds in the SharedPreferences.
Now i want to give this date out on my screen in this form: DD.MM.YYYYY
I used the Calendar function but it give back 02.04.0113 so, there are missing 1900 Years.
Here is my Code:
private void initBornTXT() {
SharedPreferences pref = getSharedPreferences("LIFE", 0);
long born = pref.getLong("BIRTHDAY", 0);
Calendar c = Calendar.getInstance();
c.setTimeInMillis(0);
c.add(Calendar.SECOND, (int)born);
int year = c.getTime().getYear();
int month = c.getTime().getMonth();
int day = c.getTime().getDay();
String string_born = String.format("%02d.%02d.%04d", day, month, year);
TextView born_txt = (TextView)findViewById(R.id.textViewBorn);
born_txt.setText(string_born);
}
What coud be wrong?
Nothing's wrong. You've just not looked at the documentation for the method you're calling, Date.getYear():
Returns a value that is the result of subtracting 1900 from the year that contains or begins with the instant in time represented by this Date object, as interpreted in the local time zone.
Note that you should have received a warning that you're using a deprecated API: don't just ignore those warnings.
Also, do yourself a favour and don't do the formatting yourself: use SimpleDateFormat instead. (Or ideally, use Joda Time instead...) That way you can avoid the month being wrong, too... you may not have noticed that you're a month off due to months being 0-based, which is common to both Calendar and Date.
That's normal, documented behavior. See JavaDoc for Date#getYear().
A better way to get the year would be:
c.get(Calendar.YEAR)
You're using getTime, which returns a date object. Dates are based on 0=1900. So this is the expected output. Use a SimpleDateFormat instead.
Kudos for creating a Y2K bug though :)

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

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...

Categories