Calendar is not working correctly

1

My conversion from timestamp to calendar was apparently successful, since the data presented appears correct except for 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, endTimeMode = 0, endTimeMode = 0, endDayOfWeek = 1, endDayOfWeek = 1, startDayOfWeek = 1, startTimeMode = 0, endTimeMode = 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, it 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: I have tried in milliseconds (% with%).

    
asked by anonymous 31.01.2017 / 02:37

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 the output from it:

  

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 = startDimeObject = 1, startTimeMode = 0, endTimeMode = 0, endTimeMode = 0, endDayOfWeek = 1, endDayOfWeek = 1, YEAR = 2017, MONTH = 1, WEEK_OF_MARY = 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 = 0, MILLISECOND = 584, ZONE_OFFSET = -10800000, DST_OFFSET = 3600000]

In the meantime, one detail has hit me, its exit appears time = -125784706799416 and my exit is time = 1486032000584 . Looking closer, yours is ERA = 0 and mine with ERA = 1 . Going on javadoc, I see BC is 0 and AD is 1 . In other words, yours is in BC and mine in AD . Here's what BC according to javadoc :

  

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

I'll just emphasize this part:

  

before Christ

That is, you put February 2, 2017 before Christ !

The solution is to add this:

data.set(Calendar.ERA, GregorianCalendar.AD);
    
31.01.2017 / 03:04