I am currently struggling with Java.Util.Calendar
I have a method that creates a Calendar with random values for year, month and day.
public Calendar getRandomGeburtstag() {
int year = 1999 - (int) (Math.random() * 80);
int month = (int) (Math.random() * 12) + 1;
int monthLength = 28;
int day = (int) (Math.random() * monthLength) + 1;
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, month, day);
return calendar;
}
I store this calendar within a "Person" class, which has a simple getter method.
Then I request that calendar on two separate occasions.
The object isn't altered in the meantime.
Printing the calendar gives two different outputs :
java.util.GregorianCalendar[areAllFieldsSet=false,ERA=?,WEEK_OF_YEAR=?,WEEK_OF_MONTH=?,DAY_OF_YEAR=?,DAY_OF_WEEK=?,DAY_OF_WEEK_IN_MONTH=?,AM_PM=?,HOUR=?,HOUR_OF_DAY=?,MINUTE=?,SECOND=?,MILLISECOND=?,ZONE_OFFSET=?,DST_OFFSET=?]
And
java.util.GregorianCalendar[areAllFieldsSet=true,ERA=1,WEEK_OF_YEAR=52,WEEK_OF_MONTH=5,DAY_OF_YEAR=362,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=4,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=3600000,DST_OFFSET=0]
(I omitted the fields that stayed the same, they were all set in both printouts)
As you can see, all fields which were previously not set got a default or calculated value somewhere in between the two request.
The objected isn't used in between the requests so it's probably the JVM.
The problem is, that the field ZONE_OFFSET
is used by a method I don't have access to. The desired output is only printed if ZONE_OFFSET=?
is given.
Is there any way I can stop the automatic allocation of these values?