FileNotFoundException at new GregorianCalendar() in 1.4.2_02

The following exception is thrown in windows platform of j2sdk 1.4.2_02 (also in 03) when I create a new GregorianCalendar
Thread [main] (Suspended (exception FileNotFoundException))
     FileInputStream.open(String) line: not available [native method] [local variables unavailable]
     FileInputStream.<init>(File) line: 106
     ZoneInfoFile$1.run() line: 910
     AccessController.doPrivileged(PrivilegedExceptionAction) line: not available [native method]
     ZoneInfoFile.readZoneInfoFile(String) line: 904
     ZoneInfoFile.createZoneInfo(String) line: 520
     ZoneInfoFile.getZoneInfo(String) line: 499
     TimeZone.parseCustomTimeZone(String) line: 633
     TimeZone.getTimeZone(String, boolean) line: 450
     TimeZone.getDefault() line: 522
     GregorianCalendar.<init>() line: 336
     MyTest.<clinit>() line: 28The source is
public class MyTest {
     private static Calendar cal = new GregorianCalendar();
     Does anyone know of any cure?
Thanks a lot.
Gary.

I don't think you can do new GregorianCalendar, but you need to use Calendar.getInstance().
Read more at:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Calendar.html
Here is some:
Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. (A Date object represents a specific instant in time with millisecond precision. See Date for information about the Date class.)
Subclasses of Calendar interpret a Date according to the rules of a specific calendar system. The platform provides one concrete subclass of Calendar: GregorianCalendar. Future subclasses could represent the various types of lunar calendars in use in many parts of the world.
Like other locale-sensitive classes, Calendar provides a class method, getInstance, for getting a generally useful object of this type. Calendar's getInstance method returns a Calendar object whose time fields have been initialized with the current date and time:
Calendar rightNow = Calendar.getInstance();
A Calendar object can produce all the time field values needed to implement the date-time formatting for a particular language and calendar style (for example, Japanese-Gregorian, Japanese-Traditional).
Gil

Similar Messages

  • I made a new Date object

    See, here's the thing with dates in java, the way i see it. We used to have Date, but Date wasn't international so we got Calendar. But some things, like SimpleDateFormat, still expect java.util.Date. java.sql.Date is out there somewhere, too. None of them have direct support for date differences (i.e. dateA - dateB = 8 days, or whatever). Now you're probably reading this thinking "Java supports all of this" and you're right. Calendar has a getTime method that returns a Date, and i can just pass THAT to SimpleDateFormat. With a little math, getTimeInMillis can be used to find the difference in two Calendars. But i got to thinking, "There should only be one Date object i need to think about," and since i don't ever have enough to do at work i put one together. I don't know if any of you are going to care enough to use it, mostly i'm just looking for advice on features i should add to it, inefficiencies in the design, stuff that's hard to understand, or whatever.
    This is it:
    *This catastrophe brought to you by SrA Meyerin.
    *Proximo Satis pro administatio.
    package awesomedate;
    //The following are imported for the toFormattedString(), toUtilDate(), toSQLDate, and toCalendar() methods, respectively.
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.GregorianCalendar;
    import java.sql.*;
    import java.util.TimeZone;
    *This class is designed to be the ultimate date object. It stores dates accurate to the millisecond, has the ability to do date
    *arithmetic (adding/subtracting days/months/whatever), date comparison, and date formatting. Also it can, at will, be used as any
    *of the other date objects in the standard java API.
    public class AwesomeDate
         *Milliseconds from the epoch. This field is where everything starts.
        private long millis;
         * The current year. This is the only aspect of the current date that is stored in addition to the milliseconds. Everything else
         * is calculated as needed.
        private int year;
        //The following six variables are fairly self explanatory. I'll explain them anyway.
         *Milliseconds in one non-leap year. This can be passed to the "adjustDate()" method
         * but mostly it is used to make my code more readable.
        public final static long MILLIS_IN_YEAR = 31536000000L;
         *Milliseconds in one leap year. This can be passed to the "adjustDate()" method
         * but mostly it is used to make my code more readable.
        public static final long MILLIS_IN_LEAP_YEAR = 31622400000L;
         *Milliseconds in one day. This can be passed to the "adjustDate()" method
         * but mostly it is used to make my code more readable.
        public static final long MILLIS_IN_DAY = 86400000;
         *Milliseconds in one hour. This can be passed to the "adjustDate()" method
         * but mostly it is used to make my code more readable.
        public static final long MILLIS_IN_HOUR = 3600000;
         *Milliseconds in one minute. This can be passed to the "adjustDate()" method
         * but mostly it is used to make my code more readable.
        public static final long MILLIS_IN_MINUTE = 60000;
         *Milliseconds in one second. This can be passed to the "adjustDate()" method
         * but mostly it is used to make my code more readable.
        public static final long MILLIS_IN_SECOND = 1000;
         *The number of days in each month.
        private int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
         *The timezone offset. GMT is 0, and the default, however, CST (where i live), is -6.
        private int timeZoneOffset = -6;
         *As the name would indicate, this is the number of milliseconds beyond 00:00 of jan 1 of the current year.
        private long millisAfterYear;
         *Basic constructor. Defaults to the current date and time. Works on Greenwich Mean Time, so it may seem off. To avoid this, multiply
         *add your time zone offset * MILLIS_IN_HOUR to the time in milliseconds.
        public AwesomeDate()
            setTimeInMillis(System.currentTimeMillis());
         *Fairly simple constructor. Sets time to the the number of milliseconds passed past the epoch.
        public AwesomeDate(long millis)
            setTimeInMillis(millis);
         *This constructor sets the date to the year/month/day passed.
        public AwesomeDate(int year, int month, int day)
            setDate(year, month, day);
         *This constructor sets the date time to the year/month/day/minute/hour/second passed.
        public AwesomeDate(int year, int month, int day, int hour, int minute, int second)
            setDate(year, month, day, hour, minute, second);
         *When you have the number of milliseconds, the first thing you must calculate is year. Because the number of milliseconds in a
         *year may vary, this is also the most difficult to calculate. This is the method that does it, though.
        private void setYearAfter1970()
            //I don't want to alter the actual number of milliseconds, so i make this variable and perform calculations on it.
            long theseMillis = this.millis;
            //Start at 1970 (the epoch) and work from there.
            year = 1970;
            long nextYear = MILLIS_IN_YEAR;
            //In this loop i subtract the number of millis in whatever year. The condition is if i have enough millis to make it through
                //another year.
            while (theseMillis >= nextYear)
                if (isLeapYear(year))
                    theseMillis = theseMillis - MILLIS_IN_LEAP_YEAR;
                    nextYear = MILLIS_IN_YEAR;
                else
                    theseMillis = theseMillis - MILLIS_IN_YEAR;
                    if (isLeapYear(year + 1))
                        nextYear = MILLIS_IN_LEAP_YEAR;
                year++;
            millisAfterYear = theseMillis;
            this.year = year;
         *Calculating the year from before 1970 must be done differently. It's pretty much just inverted.
        private void setYearBefore1970()
            long theseMillis = this.millis;
            year = 1970;
            long nextYear = MILLIS_IN_YEAR;
            while (theseMillis <= 0)
                if (isLeapYear(year))
                    theseMillis = theseMillis + MILLIS_IN_LEAP_YEAR;
                    nextYear = MILLIS_IN_YEAR;
                else
                    theseMillis = theseMillis + MILLIS_IN_YEAR;
                    if (isLeapYear(year - 1))
                        nextYear = MILLIS_IN_LEAP_YEAR;
                year--;
            millisAfterYear = theseMillis * -1;
            this.year = year;
         *Just what it sounds like. Pass it the number of milliseconds past the epoch and it will calculate the date based on that.
        public void setTimeInMillis(long millis)
            this.millis = millis;
            if (millis >= 0)
                setYearAfter1970();
            else
                setYearBefore1970();
         *Returns the number of milliseconds from the epoch.
        public long getTimeInMillis()
            return this.millis;
         *Returns the current year.
        public int getYear()
            return this.year;
         *Sets the date to 00:00 of Jan 1 of the passed year.
        public void setYear(int year)
            this.year = year;
            long theseMillis = 0;
            if (year > 1970)
                //Kind of like setYearAfter1970 method, except it's adding instead of subtracting. So actually it's the opposite.
                for (int cntr = 1970; cntr < this.year; cntr++)
                    if (isLeapYear(cntr))
                        theseMillis = theseMillis + MILLIS_IN_LEAP_YEAR;
                    else
                        theseMillis = theseMillis + MILLIS_IN_YEAR;
            else
                for (int cntr = 1970; cntr > this.year; cntr--)
                    if (isLeapYear(cntr))
                        theseMillis = theseMillis - MILLIS_IN_LEAP_YEAR;
                    else
                        theseMillis = theseMillis - MILLIS_IN_YEAR;
            //On a leap year there are 29 days in February. If not, there's 28.
            if (isLeapYear(year))
                daysInMonth[1] = 29;
            else
                daysInMonth[1] = 28;
            //This variable helps me calculate denominations of time that are below year.
            millisAfterYear = 0;
            setTimeInMillis(theseMillis);
         *Sets the month to the one that you passed, 0 being January and 11 being December.
        public void setMonth(int month)
            if (month < 0 || month > 11)
                throw new ArrayIndexOutOfBoundsException(month);
            long theseMillis = getTimeInMillis();
            int days = 0;
            setYear(this.year);
            if (getTimeInMillis() > 0)
                theseMillis = theseMillis - getTimeInMillis();
            else
                theseMillis = theseMillis + getTimeInMillis();
            for (int cntr = 0; cntr < month; cntr++)
                days = days + daysInMonth[cntr];
            millisAfterYear = days * MILLIS_IN_DAY + 1;
            setTimeInMillis(getTimeInMillis() + millisAfterYear);
         *Returns the month stored in this object. With this object 0 represents January and 11 represents December.
        public int getMonth()
            long theseMillis = millisAfterYear;
            int cntr = 0;
            while (theseMillis > (MILLIS_IN_DAY * daysInMonth[cntr]))
                theseMillis = theseMillis - MILLIS_IN_DAY * daysInMonth[cntr];
                cntr++;
            return cntr;
         *Set the day of month to the one passed.
        public void setDayOfMonth(int day)
            if (day < 1 || day > daysInMonth[getMonth()])
                throw new ArrayIndexOutOfBoundsException(day);
            setMonth(getMonth());
            //Internally, this actually works starting at zero, however to anyone using this class it appears to start at 1. Therefore
                //i must subtract one here.
            long addMillis = MILLIS_IN_DAY * (day - 1);
            millisAfterYear = millisAfterYear + addMillis;
            setTimeInMillis(getTimeInMillis() + addMillis + 1);
         *Returns the day of month.
        public int getDayOfMonth()
            int days = (int)(millisAfterYear / MILLIS_IN_DAY);
            int cntr = 0;
            while (days >= daysInMonth[cntr])
                days = days - daysInMonth[cntr];
                cntr++;
            //Internally this class stores dates starting at zero, but i want it to look like it starts at 1.
            return days + 1;
         *Sets the time to the currently selected day at the passed hour on the hour. uses 24 hour clock.
        public void setHour(int hour)
            if (hour < 0 || hour > 23)
                throw new ArrayIndexOutOfBoundsException(hour);
            setDayOfMonth(getDayOfMonth());
            long addMillis = MILLIS_IN_HOUR * hour;
            millisAfterYear = millisAfterYear + addMillis;
            setTimeInMillis(getTimeInMillis() + addMillis);
         *Returns the hour (but not how many minutes past the hour).
        public int getHour()
            long millisAfterDay = millisAfterYear % MILLIS_IN_DAY;
            return (int)(millisAfterDay / MILLIS_IN_HOUR);
         *Set the minutes past the hour. Works 0-59.
        public void setMinute(int minute)
            if (minute < 0 || minute > 59)
                throw new ArrayIndexOutOfBoundsException(minute);
            setHour(getHour());
            long addMillis = MILLIS_IN_MINUTE * minute;
            millisAfterYear = millisAfterYear + addMillis;
            setTimeInMillis(getTimeInMillis() + addMillis);
         *Returns the minutes past the hour, 0-59.
        public int getMinute()
            long millisAfterHour = millisAfterYear % MILLIS_IN_HOUR;
            return (int)(millisAfterHour / MILLIS_IN_MINUTE);
         *Sets the seconds past the minute, 0-59.
        public void setSecond(int second)
            if (second < 0 || second > 59)
                throw new ArrayIndexOutOfBoundsException(second);
            setMinute(getMinute());
            long addMillis = MILLIS_IN_SECOND * second;
            millisAfterYear = millisAfterYear + addMillis;
            setTimeInMillis(getTimeInMillis() + addMillis);
         *Returns the seconds past the minute, 0-59.
        public int getSecond()
            long millisAfterMinute = millisAfterYear % MILLIS_IN_MINUTE;
            return (int)(millisAfterMinute / MILLIS_IN_SECOND);
         *The more commonly seen set method of other date objects. Sets the date/time to 00:00 of the year/month/day passed. Convenience method
         *for setDate(int year, int month, int day, int hour, int minute, int second).
        public void setDate(int year, int month, int day)
            setDate(year, month, day, 0 , 0 , 0);
         *Sets every date/time field.
        public void setDate(int year, int month, int day, int hour, int minute, int second)
            setYear(year);
            setMonth(month);
            setDayOfMonth(day);
            setHour(hour);
            setMinute(minute);
            setSecond(second);
         *Returns yes if the stored date is a leap year. A leap year is every year that is divisible by four unless it is divisible by 100
         *unless it is also divisible by 400.
        public boolean isLeapYear()
            return isLeapYear(this.year);
         *For internal use. Returns if the passed year meets the criteria for a leap year.
        private boolean isLeapYear(int year)
            boolean leapYear = false;
            if (year % 4 == 0)
                if (year % 100 != 0)
                    leapYear = true;
                else if (year % 400 == 0)
                    leapYear = true;
            return leapYear;
         *Returns the difference in milliseconds between the time stored in this object and the millis passed to this method.
        public long getDifferenceInMillis(long millis)
            return getTimeInMillis() - millis;
         *Returns the difference in milliseconds between this date and the date passed to this method.
        public long getDifferenceInMillis(AwesomeDate otherDate)
            return getDifferenceInMillis(otherDate.getTimeInMillis());
         *Designed to be a wrapper method for the getDifferenceInMillis method. This method changes millis into years/days/whatever. Pass
         *the number of milliseconds you have to convert in the first parameter. The second parameter should be the type of denomination you
         *want (year, day, whatever). Use the MILLIS_IN_* fields associated with this object. Also bear in mind when workin with years that
         *some years are leap years, so in extreme cases of differences of 365+ years you may gain/lose a year.
        public static int toGreaterDenom(long millis, long denom)
            return (int)(millis / denom);
         * The first argument is how many of whatever (days, months, years, etc.) to add (use negative numbers to subtract). For the second
         * argument pass one of the MILLIS_IN_* fields. Thus, to add two hours would be
         * <code>
         *      AwesomeDate.adjustDate(2, AwesomeDate.MILLIS_IN_HOUR);
         * </code>
        public void adjustDate(int amount, long typeMillis)
            setTimeInMillis(this.millis + amount * typeMillis);
         *Returns an object of type java.util.Date set to the date/time stored here.
        public java.util.Date toUtilDate()
            long offset = TimeZone.getDefault().getRawOffset();
            return new java.util.Date(getTimeInMillis() +  -1 * offset);
    //        return new java.util.Date(getTimeInMillis());
         *Returns an object of type GregorianCalendar set to the date/time stored here.
        public GregorianCalendar toGregorianCalendar()
            long offset = TimeZone.getDefault().getRawOffset();
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTimeInMillis(getTimeInMillis() - offset);
            return cal;
         *Returns an object of type java.sql.Date set to the date/time stored here.
        public java.sql.Date toSQLDate()
            long offset = TimeZone.getDefault().getRawOffset();
            return new java.sql.Date(getTimeInMillis() - offset);
        /** Format the date  using the string that you pass. Works just like the SimpleDateFormat object. Infact, it uses one! Heres
         *how it works:
         *Letter  Date or Time Component  Presentation  Examples  <br>
         *G  Era designator  Text  AD  <br>
         *y  Year  Year  1996; 96  <br>
         *M  Month in year  Month  July; Jul; 07<br> 
         *w  Week in year  Number  27  <br>
         *W  Week in month  Number  2  <br>
         *D  Day in year  Number  189  <br>
         *d  Day in month  Number  10  <br>
         *F  Day of week in month  Number  2<br> 
         *E  Day in week  Text  Tuesday; Tue  <br>
         *a  Am/pm marker  Text  PM  <br>
         *H  Hour in day (0-23)  Number  0<br> 
         *k  Hour in day (1-24)  Number  24  <br>
         *K  Hour in am/pm (0-11)  Number  0  <br>
         *h  Hour in am/pm (1-12)  Number  12  <br>
         *m  Minute in hour  Number  30  <br>
         *s  Second in minute  Number  55  <br>
         *S  Millisecond  Number  978  <br>
         *z  Time zone  General time zone  Pacific Standard Time; PST; GMT-08:00<br> 
         *Z  Time zone  RFC 822 time zone  -0800  <br>
        public String toFormattedString(String format)
            SimpleDateFormat formattage = new SimpleDateFormat(format);
            return formattage.format(toUtilDate());
         *I overrode the toString method. Now it tells everyone how awesome my AwesomeDate is.
        public String toString()
            String output = "John's Awesome Date!";
            output = output + " Millis: " + getTimeInMillis();
            output = output + " Year: " + getYear();
            output = output + " Month: " + getMonth();
            output = output + " Date: " + getDayOfMonth();
            output = output + " Time: ";
            if (getHour() < 10)
                output = output + "0" + getHour();
            else
                output = output + getHour();
            if (getMinute() < 10)
                output = output + "0" + getMinute();
            else
                output = output + getMinute();
            output = output + " Seconds: " + getSecond();
            return output;
        public static void main(String[] psvm)
            AwesomeDate blah = new AwesomeDate();
            GregorianCalendar blah1 = new GregorianCalendar();
            GregorianCalendar blah2 = new GregorianCalendar();
    }

    The reason Callendar is so complicated is that it is trying to solve a very complicated problem.
    You don't appear to support leap seconds (of which there can be upto 2 in any time interval)
    http://www.timeanddate.com/time/leapseconds.html
    Some of your code will not work for large dates very well (seems to iterate over the years). Try using your date class for astronomical or geological calculations. them big numbers will make your code slow at the moment.
    You don't seem to support timezones or transitory offsets (BST vs GMT). You most definately don't handle changes to timezone offsets that have occured in the past or historical daylight savings time offsets (they have not always been the same)
    The difference between two calendars is a "duration" a duration means nothing unless it is applied to a caledar/date (although you may be able to get away with it if the duration is always stored in millis)

  • Want to make Monday as the first day of the week in GregorianCalendar. how?

    hi
    I need to know what day is the first of the month is. for example the 1st of Nov 2004 is Moday and 1st of Dec 2004 is saturday.
    I am using the GregorianCalendar:
    1.  GregorianCalendar calendar = new GregorianCalendar(2004,11,1);    //set date to 1st Nov 2004
    2.  int firstDay = calendar.get(Calendar.DAY_OF_WEEK);now firstDay is 2. This is because the week starts from Sunday, so Monday is the 2nd day.
    But I am in Uk and my Uk Calendar shows monday as the first day (even in Windows 2000 Calendar).
    I would like the GregorianCalendar to have Monday as the first day of the week, so that the
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    returns 1 in the above case.
    I also tried adding
    calendar.setFirstDayOfWeek(Calendar.MONDAY); just between line 1 and line 2, but it did not help.
    This is because i don't want to manually subtract 1. If i manually subtract one, then the program might not work in other locale and timezones.
    Also if I subtract 1, then for 1 Feb 2004,
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    will return 1 as 1st Feb 2004 falls on Sunday. so if i subtract 1 it will be 0, so I have to do a extra checking for 0.
    Is there anyway to make the Calendar have Monday as the first day of the week??
    Tanveer

    hi
    I need to know what day is the first of the month is.Why does this matter? Since we know that 1 == Sunday and 2 == Monday... Why do you need Monday to be == 1? and couldn't you just subtract 1 if it's so important?
    for example the 1st of Nov 2004 is Moday and 1st of
    Dec 2004 is saturday.
    I am using the GregorianCalendar:
    1.  GregorianCalendar calendar = new
    GregorianCalendar(2004,11,1);    //set date to 1st Nov
    2004
    2.  int firstDay = calendar.get(Calendar.DAY_OF_WEEK);now firstDay is 2. This is because the week starts
    from Sunday, so Monday is the 2nd day.
    But I am in Uk and my Uk Calendar shows monday as the
    first day (even in Windows 2000 Calendar).
    I would like the GregorianCalendar to have Monday as
    the first day of the week, so that the
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    returns 1 in the above case.
    I also tried adding
    calendar.setFirstDayOfWeek(Calendar.MONDAY);[/cod
    ] just between line 1 and line 2, but it did not help.
    This is because i don't want to manually subtract 1.
    If i manually subtract one, then the program might not
    work in other locale and timezones.
    Also if I subtract 1, then for 1 Feb 2004,
    int firstDay = calendar.get(Calendar.DAY_OF_WEEK);
    will return 1 as 1st Feb 2004 falls on Sunday. so if i
    subtract 1 it will be 0, so I have to do a extra
    checking for 0.
    Is there anyway to make the Calendar have Monday as
    the first day of the week??
    Tanveer

  • Moving a GregorianCalendar to the first day of the week

    I'm using a GregorianCalendar class in order to get the time stamp for the nearest Sunday's date.
    here's what I'm trying to do:
    1. get a timeStamp and set it into a date
    Date startDate = new Date(startTime);2. create a new GregorianCalendar using the above date as the start time.
    GregorianCalendar calendar = new GregorianCalendar((startDate.getYear()+1900),startDate.getMonth(),startDate.getDate());3. the tricky part: make sure the calnedar's date is set to Sunday, if the current date is on any day other then Sunday, I will have to move it back to the nearest Sunday. I'm trying to do this by using:
    calendar.set(Calendar.DAY_OF_WEEK, 0);     for some reason this does not work...
    am I doing anything wrong here?

    thanks for that info but the problem is still there. here's some more input:
    //creating the date, the ts here is 1153858800000 which is the dated in 7/25/2006
    Date startDate = new Date(startTime);
    //creating the new calendar
    GregorianCalendar calendar = new GregorianCalendar((startDate.getYear()+1900),startDate.getMonth(),startDate.getDate());
    //setting the calendar to the first hour (not sure it necessary)
    calendar.set(Calendar.HOUR, 1);
    //setting the calendar to the first day of the week
    calendar.set(Calendar.DAY_OF_WEEK, 1);
    //this returns 1153778400000 which is still dated 7/25/2006 whic is a Tuesday I would expect it to returns a different date
    calendar.getTimeInMillis()     

  • Error with gregoriancalendar

    I'm having an error with the gregoriancalendar class.
    I have a class that gets an initial date froma database, and iterate until today.
    The problem is that its showing that today is day 262 and we are in day 232 of the year.
    Whats the problem????
    Any help will be appreciate.
    the code is bellow:
    String sql2 = "Select d02inter from cen02";
              try{
              PreparedStatement st4 = cnn3.prepareStatement(sql2);
              ResultSet rs2 = st4.executeQuery();
              dataatual = null;
              while (rs2.next()){
                   String dataint = rs2.getString("d02inter");
                   Integer ano_int = Integer.valueOf(dataint.substring(1,5));
                   Integer mes_int = Integer.valueOf(dataint.substring(5,7));
                   Integer dia_int = Integer.valueOf(dataint.substring(7));
                   Calendar calend_int = new GregorianCalendar(ano_int,mes_int,dia_int);
                   Calendar calend_hoje = new GregorianCalendar();
                   Calendar calendwork = (GregorianCalendar)calend_int.clone();
                   while((calendwork.compareTo(calend_hoje))<0){
                        Integer diadoano=calendwork.get(Calendar.DAY_OF_YEAR);
                        Integer ano = calendwork.get(Calendar.YEAR);
                        dataatual=(ano*1000)+diadoano;
                        if(calendwork.equals(calend_int)){
                             if(mp.containsKey(dataatual)){
                                  bmhBean bm = (bmhBean)mp.get(dataatual);
                                  bm.entrou++;
                                  mp.put(dataatual,bm);
                             }else{
                                  bmhBean bm = new bmhBean();
                                  bm.entrou++;
                                  mp.put(dataatual,bm);
                        if(mp.containsKey(dataatual)){
                             bmhBean bm = (bmhBean)mp.get(dataatual);
                             bm.inter++;
                             mp.put(dataatual,bm);
                        }else{
                             bmhBean bm = new bmhBean();
                             bm.inter++;
                             mp.put(dataatual,bm);
                        calendwork.add(Calendar.DATE,1);
              }

    Yes I agree with Wildcard82
    The GregorianCalendar javadoc defines the month 0 based (0 - 11)
    so you must be careful when creating a Gregorian calendar by substracting 1 to your month number (if 1 - 12), and if passing it back to an int of the type 1 - 12, to add a respective 1e on the
    get(GregorianCalendar.MONTH)method
    I recommend what i did to evade dealing with this, I'm used to save dates in the yyyymmdd format, and month being from 1-12, so I created two methods
    a) int2GregorianCalendar(int date), which takes that int type and returns the correct GregorianCalendar, with what was defined above.
    b) GregorianCalendar2int(GregorianCalendar date) which is the opposite.
    Hope it helps, cya.

  • Parsing datestring to GregorianCalendar on Solaris.

    Hi There!
    I?m parsing strings to GregorianCalendar object with the class SimpleDateFormat. The problem is that when I?m running on a Windows platform everything works out great but when I deploy it on a Solaris platform the parser parse it wrongly.
    Heres the code:
    java.util.TimeZone tz= null;
    String timezonesettings= null;
    String availableIds[]= null;
    String timezone= null;
    SimpleTimeZone zone= null;
    java.text.SimpleDateFormat sdf= null;
    int TZ_OFFSET= 1*60*60*1000;
    try{
    //get requested time zone from jsp page.
    timezone= request.getParameter("timezone");
    //pattern= "ww-yyyy"
    pattern= request.getParameter("pattern");
    //get requested datestring from client
    date= request.getParameter("date");
    loc= Integer.parseInt(request.getParameter("locale"));          
    zone = new SimpleTimeZone(TZ_OFFSET, timezone);
    zone.setStartRule(Calendar.MARCH,-1,Calendar.SUNDAY,2*60*60*1000);
    zone.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2*60*60*1000);
    sdf= new java.text.SimpleDateFormat(pattern,locale[loc]);
    sdf.setTimeZone(zone);
    sdf.setLenient(false);
    gregdate= new GregorianCalendar(zone,locale[loc]);
    gregdate.setTime(sdf.parse(date));
    }catch(Exception e){
    The thing is when i try to parse with the pattern "ww-yyyy" the parsing on a solaris platform always returns a the date with value 1 Jan. Why this??
    Best regards
    Niclas Rothman

    It is a known bug:
    http://developer.java.sun.com/developer/bugParade/bugs/4227284.html
    See their work around.

  • Problems with GregorianCalendar.add(Calendar.WEEK_OF_YEAR, 1)

    hi all
    i'm writing an application to calculate all the days of all the week of the year
    i got 2 buttons, one to forward to the next week, the other to go back to the last
    this is my code:
    GregorianCalendar now = new GregorianCalendar();
    DateFormat week=new SimpleDateFormat( "w" ); 
    private void b_proxActionPerformed(java.awt.event.ActionEvent evt)
    //the next week
            now.add(now.WEEK_OF_YEAR, 1);                      
            System.out.println("week "+week.format(now.getTime()));
    private void b_precActionPerformed(java.awt.event.ActionEvent evt) {
            now.add(now.WEEK_OF_YEAR, -1);    
             System.out.println("week "+week.format(now.getTime()));
    }the problem is that, when i want to skip to the next week i get:
    WEEK 25 (the current week)
    WEEK 26
    WEEK 28
    WEEK 30
    like i were add 2, but i added 1 to the week value!!!
    and when i want to return to the previous week (by pressing the second button), the week number is always the same!!
    anyone could help me?
    thanx
    sandro

    Whether it matters or not, just simply stating that it's correct.
    Here's my code that worked correctly ...
    Calendar time = new GregorianCalendar();
    SimpleDateFormat sdf = new SimpleDateFormat("w");
    for(int i=0; i < 10; i++)
        System.out.println("week in year: "+time.get(Calendar.WEEK_OF_YEAR));
        System.out.println("Week in year by sdf: "+sdf.format(time.getTime()));
        time.add(Calendar.WEEK_OF_YEAR, 1);
    }

  • Has anyone verified jdk1.3.1_19 has the new DST rule as Sun advertizes?

    Hi Everyone,
    Has anyone verified that jdk1.3.1_19 does not actually have the new Daylight saving time rule mandated by Congress starting 2007 although Sun claims that it does? I tried jdk1.3.1_18 and jdk1.3.1_19. They do not have the new DST rule. I tried jdk1.4.1_11 and it has the new DST rule. Could anyone enlighten me about jdk1.3.1_18(19) if you know about the DST issue with these two JVM's? Thanks.
    Alan Lei

    We have legacy code running on legacy platforms that cannot be just retired unfortunately. It would be great if we could simply eliminate it overnight. The following is the code that I used to gauge whether the JVM has the new DST rule.
    int year = 2007;
    int month =10;
    int dateOfMonth = 1;
    int hour = 1;
    int minute = 0;
    GregorianCalendar gc = new GregorianCalendar(year, month, dateOfMonth, hour, minute);
    Date date = gc.getTime();
    System.out.println("displayed name=" + timeZone.getDisplayName() +
    " dst?=" + timeZone.useDaylightTime() +
    " date=" + date.toString() +
    " in dst?=" + timeZone.inDaylightTime(date));
    Alan Lei

  • Is this a bug with GregorianCalendar.getTimeMiliseconds()?

    Hi,
    I have used GregorianCalendar.getTimeMiliseconds() method for many years, but now I have a problem.
    The code below returns the same value:
    System.out.println("DATE1=" + new GregorianCalendar(2009,1,29,8,0,1).getTimeInMillis());
    System.out.println("DATE2=" + new GregorianCalendar(2009,2,1,8,0,1).getTimeInMillis());The reply for both is: 1235908801000
    Then, I think this is a Java bug, right?
    My system is:
    Linux Fedora Core + JDK 1.6.5.
    The same bug occurs in another machine:
    Windows 2003 Server + JDK 1.6.10.

    Edilmar_Alves wrote:
    Then, I think this is a Java bug, right?Nope. Your understanding of the [GregorianCalendar constructor|http://java.sun.com/javase/6/docs/api/java/util/GregorianCalendar.html#GregorianCalendar(int,%20int,%20int,%20int,%20int,%20int)] is incorrect. ;o)
    Months in Java are zero-indexed. You should never use int literals for the core calendar API. Use constants instead, and you'll get results more in line with yor expectations. Example:
    Calendar c1 = new GregorianCalendar(2009,Calendar.JANUARY,29,8,0,1);
    Calendar c2 = new GregorianCalendar(2009,Calendar.FEBRUARY,1,8,0,1);
    System.out.println("DATE1=" + c1.getTimeInMillis());
    System.out.println("DATE2=" + c2.getTimeInMillis());~

  • GregorianCalendar Issue

    Hi,
    In the process of finding the difference between two dates, I am encountering a problem where one of the dates when converted to GregorianCalendar always prints a -ve value. Would you have any hints as to why would that would be?
         String fmt = "MM-dd-yyyy HH:mm:ss";
         SimpleDateFormat df = new SimpleDateFormat(fmt);
         Date expires = null;
    try
    expires = df.parse(endTime);
    catch (ParseException pe)
    pe.printStackTrace();
    GregorianCalendar gexpires = new GregorianCalendar();
    gexpires.setTime(expires);
    GregorianCalendar rightNow = new GregorianCalendar();
    long exp_millies = gexpires.getTimeInMillis();
    long now_millies = rightNow.getTimeInMillis();
    exp_millis is always -ve, even though I am parsing the future date from an xml string.
    Just wondering where I am going wrong!! Thanks
    M

    What's the string you're passing for the date?
    Zero is Jan. 1, 1970, 00:00:00.000 GMT. Any dates prior to that are negative.

  • GregorianCalendar: I seem to always be five (5) hours head in my value.

    The following just outputs the milliseconds of the current datetime in Central Standard Time (CST).
    What I am try to test is this line:
    calendar.add(Calendar.HOUR_OF_DAY, 0);When I try the following:
    package test;
    import java.util.Date;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.SimpleTimeZone;
    import java.util.TimeZone;
    public class GetCal {
         public static void main(String args[]){
              String[] ids = TimeZone.getAvailableIDs(-6 * 60 * 60 * 1000);
              if (ids.length == 0) {
                  System.exit(0);
              System.out.println("Current Time");
              SimpleTimeZone pdt = new SimpleTimeZone(-6 * 60 * 60 * 1000, ids[0]);
              // set up rules for daylight savings time
              pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
              pdt.setEndRule(Calendar.OCTOBER, -1, Calendar.SUNDAY, 2 * 60 * 60 * 1000);
              Calendar calendar = new GregorianCalendar(pdt);
              Date trialTime = new Date();
              calendar.setTime(trialTime);
              calendar.add(Calendar.HOUR_OF_DAY, 0);
              long nCalTime = calendar.getTimeInMillis();
              String nCalTimeValue = String.valueOf(nCalTime);
              System.out.println("Long " + nCalTime);
              System.out.println("String " + nCalTimeValue);
    }I would output something similar to:
    1149624386040
    I would take this value and run it in the following SQL to verify the datetime:
    VARIABLE millis NUMBER
    EXEC :millis := 1149624386040;
    ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH:MI:SS AM';
    SELECT TO_DATE('197001','YYYYMM') + (:millis / 1000 / 60 / 60 / 24) converted
    FROM DUAL;The result would be:
    06-JUN-2006 08:06:26 PM
    However, the actual time would be:
    06-JUN-2006 03:06:26 PM
    Thus, I am five hours ahead.
    Since I am adding zero (0) in:
    calendar.add(Calendar.HOUR_OF_DAY, 0);I thought I should get the correct time, however I always seem to be five hours ahead.
    I'd greatly appreciate any help or corrects.
    Thank you,
    --Todd                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    DrClap: I greatly appreciate your help with this.
    I ran the following:
    select DBTIMEZONE from dual;This returned "-06:00".
    In a nutshell, I am testing with the current date - but what I have is a JSF MyFaces Calendar component that represents a date and a JSF listbox with the values 0 - 23 as hours options.
    A user will pick a date and select an hour.
    I have converted the date to milliseconds, add the selected hour and the resulting value needs to be in milliseconds.
    This value will be passed to a method that will return a list of transactions for the user's picked date and selected hour. I have no control of the method - I am only consuming.
    I am testing with the current date, to make sure everything is converting to milliseconds correctly - which it's not since I seem to be five (5) hours ahead.
    Now that I know that the Oracle database server as a DBTIMEZONE value of "-06:00"; do I just create a static variable of (5 hours) 18000000 and just subtract that from the millisecond value that is made up of the picked date and selected hour?
    There has to be a better way?
    Thanks,
    --Todd                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • How to know the which week in GregorianCalendar API

    Dear all
    does someone know that way when i use GregorianCalendar this API to know the which week is
    for example in this month has 6 week and 2008/08/27 is week 5 , dose any idea i can know it in this program
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Date;
    import java.text.DateFormat;
    public class DateExample5 {
         public static void main(String[] args) {
              DateFormat dateFormat =
              DateFormat.getDateInstance(DateFormat.FULL);
              // Create our Gregorian Calendar.
              //GregorianCalendar cal = new GregorianCalendar(2008,05,10);
              GregorianCalendar cal = new GregorianCalendar();
              //cal.setTime(new Date());
              System.out.println("System Date: " +dateFormat.format(cal.getTime()));
              // Set the day of week to FRIDAY
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.SUNDAY);
              System.out.println("After Setting Day of Week to Friday: " + dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.MONDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.TUESDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.WEDNESDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.THURSDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.FRIDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.set(GregorianCalendar.DAY_OF_WEEK,GregorianCalendar.SATURDAY);
              System.out.println("After Setting Day of Week to Friday: " +dateFormat.format(cal.getTime()));
              cal.add(GregorianCalendar.DAY_OF_MONTH,7);
    }Edited by: roger5089 on Aug 26, 2008 9:25 AM

    import java.util.Calendar;
    import static java.util.Calendar.WEEK_OF_MONTH;
    public class DateExample5 {
        public static void main(String[] args) {
            Calendar cal = Calendar.getInstance();
            int weekOfMonth = cal.get(WEEK_OF_MONTH);
            System.out.println(weekOfMonth); //5
    }Never used Calendar.WEEK_OF_MONTH, but I had a butcher's hook and found this documentation:
    <quote>
    Field number for get and set indicating the week number within the current month. The first week of the month, as defined by getFirstDayOfWeek() and getMinimalDaysInFirstWeek(), has value 1. Subclasses define the value of WEEK_OF_MONTH for days before the first week of the month.
    </quote>

  • Help with GregorianCalendar and ADD.

    I want to subtract 1 month from a date. I use GregorianCalendar to do it this way. So if I have a date of say 28-feb-2003 and I have to subtract 1 month from it to get 31-jan-2003.
    GregorianCalendar aGC = new GregorianCalendar();
    aGC.setTime( "28-feb-2003");
    aGC.add(Calendar.MONTH,-1); <-- which gives 28-jan-2003 instead of 31-jan-2003.
    Similary subtracting 1 month from 30-SEP-2003 gives 30-aug-2003 instead of 31-aug-2003.

    I've tried this
    import java.util.*;
    public class CalendarTest {
         public static void main(String[] args) {
              Calendar cal = Calendar.getInstance(Locale.ITALY);
              cal.set(2003, Calendar.AUGUST, 28);
              System.out.println(cal.get(Calendar.DATE) + "."
                                       + cal.get(Calendar.MONTH) + "."
                                       + cal.get(Calendar.YEAR));
              cal.add(Calendar.MONTH, -1);
              System.out.println(cal.get(Calendar.DATE) + "."
                                       + cal.get(Calendar.MONTH) + "."
                                       + cal.get(Calendar.YEAR));
    }and it gives
    28.7.2003
    28.6.2003
    I hope this will help.
    Bye

  • How to use GregorianCalendar to get today's date?

    I am using the following code to get today's date? I'm curious whether there is a direct way for GregorianCalendar to get today's date?
    Thanks,
    java.util.Date today = new java.util.Date();
    Calendar cal_today = new GregorianCalendar();
    cal_today.setTime(today);
    System.out.println(" month = " + cal_today.get(Calendar.MONTH));
    System.out.println(" day = " + cal_today.get(Calendar.DAY_OF_MONTH));
    System.out.println(" year = " + cal_today.get(Calendar.YEAR));

    If you mean construct it with todays date and time, sure. Just call the contructor with no arguments. According to the API...Constructs a default GregorianCalendar using the current time in the default time zone with the default locale.

  • Issue with GregorianCalendar class

    Hi I'm having issues with the GregorianCalender class. I am trying to enable daylight savings time but it doesn't seem to be working properly for. As a test I have taken the difference of time (in milliseconds) between a date where daylight savings causes a shift in time (April 4, 2004 for example). The two sample dates I have chosen are April 5, 2004 and April 4, 2004, ideally the time difference should be 23 hrs (since one hour is lost due to daylight savings) but I am getting 24hrs. I have a sample program in C++ which gives me the correct answer so I know the result am I getting here is wrong. Anybody have any suggestions?
    String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
         if (ids.length == 0)
         System.exit(0);
         // create a Pacific Standard Time time zone
         SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
         // set up rules for daylight savings time
         pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 60 * 60 * 1000, true);
         pdt.setEndRule(Calendar.OCTOBER, 31, Calendar.SUNDAY, 60 * 60 * 1000, false);
         pdt.setDSTSavings( 60*60*1000 );
         GregorianCalendar cal = new GregorianCalendar(2004, 4, 4);
         GregorianCalendar cal1 = new GregorianCalendar(2007, 4, 5);
         cal1.setTimeZone ( pdt );
         cal.setTimeZone( pdt );
    //This values is incorrect
         long diff = cal1.getTimeInMillis() - cal.getTimeInMillis();     
    //I have also tried the following
    String[] ids = TimeZone.getAvailableIDs(-8 * 60 * 60 * 1000);
         if (ids.length == 0)
         System.exit(0);
         // create a Pacific Standard Time time zone
         SimpleTimeZone pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, ids[0]);
         // set up rules for daylight savings time
         pdt.setStartRule(Calendar.APRIL, 1, Calendar.SUNDAY, 60 * 60 * 1000, true);
         pdt.setEndRule(Calendar.OCTOBER, 31, Calendar.SUNDAY, 60 * 60 * 1000, false);
         pdt.setDSTSavings( 60*60*1000 );
         GregorianCalendar cal = new GregorianCalendar(pdt);
         GregorianCalendar cal1 = new GregorianCalendar(pdt);
         cal1.set(2004, 4, 5 );
         cal.set( 2004, 4, 4 );
    //This values is incorrect
         long diff = cal1.getTimeInMillis() - cal.getTimeInMillis();     
    Thanks

    There may be any of several issues:
    Daylight savings time moves from year to year. Maybe last year, it was not on the same week number.
    Daylight savings time officially occurs at 2am. Java date's default to midnight if explicitly initialized. So, try calculating from after 2-3 am, depending on which way time was shifted.- Saish
    "My karma ran over your dogma." - Anon

Maybe you are looking for

  • VPRS in COPA coresponding to value from goods receipt

    Hi, I'm having the following scenario: 1. Goods receipt from production for finish goods at standard price P1; 2. Calculation and change of standard price to P2; 3. Period end closing in CO - variances and settlement to COPA - variances are calculate

  • Mail problems - no content and worse

    I am getting some messages that have "no content". Another, even worse problem is also happening (see below). HELP! Email from YouTube Subject: Subscription Update - April 24, 2009 Content: show's youtube logo, pic and link to video Email from friend

  • My Nokia 5230 has not showing the Photos and video...

    My Nokia 5230 has not showing the Photos and videos(from Memory Card) in gallery. and Music library has not updating. Please send me the solution.

  • Doubt regarding BDC

    Hi all, I developed a BDC. Client had asked for a requirement  tat while uploading an excel for eg of 1000 records, and if ter is an error in the 600th record ,as of now it will throw error,but tey were telling BDC shud not stop ter,else it shud disc

  • JBO-26010: Invalid Entity Attribute Name

    Hi All, The requirement is to add a new field on the page but that field is not getting selected in the core EO, so I have extended the EO and than extended the VO based on that extend ed EO. After that I followed usual steps to import then personali