Calendar is not working properly

Asked

Viewed 380 times

1

My conversion from timestamp to Calendar was done, apparently successfully, as the data presented appeared correct except the day of the week.

data=(java.util.Gregoriancalendar) java.util.Gregoriancalendar[time=-125784706799416,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Sao_Paulo",offset=-10800000,dstSavings=3600000,useDaylight=true,transitions=129,lastRule=java.util.SimpleTimeZone[id=America/Sao_Paulo,offset=-10800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=9,startDay=15,startDayOfWeek=1,startTime=0,startTimeMode=0,endMode=3,endMonth=1,endDay=15,endDayOfWeek=1,endTime=0,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=0,YEAR=2017,MONTH=1,WEEK_OF_YEAR=6,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=33,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=40,SECOND=0,MILLISECOND=584,ZONE_OFFSET=-10800000,DST_OFFSET=0]

Note the following values:
YEAR: 2017 (OK)
MONTH: 1 (OK - Starts at 0, so we’re talking about February)
DAY_OF_YEAR: 33 (OK - since we have the 2nd of February)
DAY_OF_WEEK: 2 (WRONG, should be 5)

The following lines have been executed:

Calendar data = Calendar.getInstance();
data.set(0, 0, 0, 0, 0, 0);
data.setTime(arrAAM.get(i).getRegistroAulaAvulsa().getDataInicio());

NOTE: Remember that I tried in with milliseconds (setTimeInMillis).

  • 1

    Tip - Use the java.time.LocalDateTime. Work with java.util.Calendar is horrible. See more here: http://answall.com/q/177129/132

  • Thanks for the tip, but I am using Calendar throughout the system, and this caused me to be surprised, because apparently it is the only attribute that is incorrect.

  • 1

    Dude, try to make it easier for people to help you by not putting the picture of the mistake, but the written mistake. And then try to define the Legends Timezone.

1 answer

3


I tested the following program:

import java.util.Calendar;
import java.util.TimeZone;

public class TesteCalendar {
    public static void main(String[] args) {
        Calendar data = Calendar.getInstance();
        data.set(2017, Calendar.FEBRUARY, 2, 8, 40, 0);
        data.set(Calendar.MILLISECOND, 584);
        data.setTimeZone(TimeZone.getTimeZone("America/Sao_Paulo"));
        System.out.println(data.get(Calendar.DAY_OF_WEEK));
        System.out.println(data);
    }
}

Here’s his way out:

5 java.util.Gregoriancalendar[time=1486032000584,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/Sao_Paulo",offset=-10800000,dstSavings=3600000,useDaylight=true,transitions=129,lastRule=java.util.SimpleTimeZone[id=America/Sao_Paulo,offset=-10800000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=9,startDay=15,startDayOfWeek=1,startTime=0,startTimeMode=0,endMode=3,endMonth=1,endDay=15,endDayOfWeek=1,endTime=0,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=1,WEEK_OF_YEAR=5,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=33,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=8,HOUR_OF_DAY=8,MINUTE=40,SECOND=0,MILLISECOND=584,ZONE_OFFSET=-10800000,DST_OFFSET=3600000]

However, a detail attentive me, on his exit appears time=-125784706799416 and on my way out time=1486032000584. Looking closer, yours is with ERA=0 and mine with ERA=1. Going in the javadoc, I see that BC is 0 and AD is 1. Meaning yours is in BC and my in AD. Here’s what it is BC in accordance with the javadoc:

Value of the ERA field indicating the period before the common era (before Christ), also known as BCE. The Sequence of years at the Transition from BC to AD is ..., 2 BC, 1 BC, 1 AD, 2 AD,...

I’ll just emphasize that part:

before Christ

That is, you put 2 February of the year 2017 before Christ!

The solution then, is to add this:

data.set(Calendar.ERA, GregorianCalendar.AD);
  • With your answer it was possible to understand the root of the problem. So I’ll put it here, to help the community. By putting data.sep(0, 0, 0, 0, 0, 0);, the year is set for "0" going for before Christ, even overwriting the year for the current date, it does not change the BC.

  • @Lucas_kunze It is because there was no year zero. Before year 1 AD, it had year 1 BC. So, 0 is year 1 BC, -1 is year 2 BC, -2 is year 3 BC, etc.

  • Yes, I understood, the effect I wanted was to clear the fields, then overwrite it. I didn’t think it would cause change in AC/DC. As a matter of fact, I haven’t even remembered the existence of them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.