Package com.ibm.icu.util

Examples of com.ibm.icu.util.GregorianCalendar


        dowTest(false);
        dowTest(true);
    }

    void dowTest(boolean lenient) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.set(1997, Calendar.AUGUST, 12); // Wednesday
        // cal.getTime(); // Force update
        cal.setLenient(lenient);
        cal.set(1996, Calendar.DECEMBER, 1); // Set the date to be December 1,
                                             // 1996
        int dow = cal.get(Calendar.DAY_OF_WEEK);
        int min = cal.getMinimum(Calendar.DAY_OF_WEEK);
        int max = cal.getMaximum(Calendar.DAY_OF_WEEK);
        logln(cal.getTime().toString());
        if (min != Calendar.SUNDAY || max != Calendar.SATURDAY)
            errln("FAIL: Min/max bad");
        if (dow < min || dow > max)
            errln("FAIL: Day of week " + dow + " out of range");
        if (dow != Calendar.SUNDAY)
View Full Code Here


        if (!cal.getTime().equals(tempcal.getTime()))
            errln("Fail");
    }

    public void Test4073929() {
        GregorianCalendar foo1 = new GregorianCalendar(1997, 8, 27);
        foo1.add(Calendar.DAY_OF_MONTH, +1);
        int testyear = foo1.get(Calendar.YEAR);
        int testmonth = foo1.get(Calendar.MONTH);
        int testday = foo1.get(Calendar.DAY_OF_MONTH);
        if (testyear != 1997 ||
            testmonth != 8 ||
            testday != 28)
            errln("Fail: Calendar not initialized");
    }
View Full Code Here

    public void Test4083167() {
        TimeZone saveZone = TimeZone.getDefault();
        try {
            TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
            Date firstDate = new Date();
            Calendar cal = new GregorianCalendar();
            cal.setTime(firstDate);
            long firstMillisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
                cal.get(Calendar.MINUTE) * 60000L +
                cal.get(Calendar.SECOND) * 1000L +
                cal.get(Calendar.MILLISECOND);
           
            logln("Current time: " + firstDate.toString());

            for (int validity=0; validity<30; validity++) {
                Date lastDate = new Date(firstDate.getTime() +
                                         (long)validity*1000*24*60*60);
                cal.setTime(lastDate);
                long millisInDay = cal.get(Calendar.HOUR_OF_DAY) * 3600000L +
                    cal.get(Calendar.MINUTE) * 60000L +
                    cal.get(Calendar.SECOND) * 1000L +
                    cal.get(Calendar.MILLISECOND);
                if (firstMillisInDay != millisInDay)
                    errln("Day has shifted " + lastDate);
            }
        }
        finally {
View Full Code Here

        }
        logln("*** THE RESULTS OF THIS TEST MUST BE VERIFIED MANUALLY ***");
    }

    public void Test4092362() {
        GregorianCalendar cal1 = new GregorianCalendar(1997, 10, 11, 10, 20, 40);
        /*
         * cal1.set( Calendar.YEAR, 1997 ); cal1.set( Calendar.MONTH, 10 );
         * cal1.set( Calendar.DATE, 11 ); cal1.set( Calendar.HOUR, 10 );
         * cal1.set( Calendar.MINUTE, 20 ); cal1.set( Calendar.SECOND, 40 );
         */

        logln( " Cal1 = " + cal1.getTime().getTime() );
        logln( " Cal1 time in ms = " + cal1.get(Calendar.MILLISECOND) );
        for( int k = 0; k < 100 ; k++ ) {
            System.out.print("");
        }

        GregorianCalendar cal2 = new GregorianCalendar(1997, 10, 11, 10, 20, 40);
        /*
         * cal2.set( Calendar.YEAR, 1997 ); cal2.set( Calendar.MONTH, 10 );
         * cal2.set( Calendar.DATE, 11 ); cal2.set( Calendar.HOUR, 10 );
         * cal2.set( Calendar.MINUTE, 20 ); cal2.set( Calendar.SECOND, 40 );
         */

        logln( " Cal2 = " + cal2.getTime().getTime() );
        logln( " Cal2 time in ms = " + cal2.get(Calendar.MILLISECOND) );
        if( !cal1.equals( cal2 ) )
            errln("Fail: Milliseconds randomized");
    }
View Full Code Here

        if( !cal1.equals( cal2 ) )
            errln("Fail: Milliseconds randomized");
    }

    public void Test4095407() {
        GregorianCalendar a = new GregorianCalendar(1997,Calendar.NOVEMBER, 13);
        int dow = a.get(Calendar.DAY_OF_WEEK);
        if (dow != Calendar.THURSDAY)
            errln("Fail: Want THURSDAY Got " + dow);
    }
View Full Code Here

    public void Test4096231() {
        TimeZone GMT = TimeZone.getTimeZone("GMT");
        TimeZone PST = TimeZone.getTimeZone("PST");
        int sec = 0, min = 0, hr = 0, day = 1, month = 10, year = 1997;
                           
        Calendar cal1 = new GregorianCalendar(PST);
        cal1.setTime(new Date(880698639000L));
        int p;
        logln("PST 1 is: " + (p=cal1.get(Calendar.HOUR_OF_DAY)));
        cal1.setTimeZone(GMT);
        // Issue 1: Changing the timezone doesn't change the
        //          represented time.
        int h1,h2;
        logln("GMT 1 is: " + (h1=cal1.get(Calendar.HOUR_OF_DAY)));
        cal1.setTime(new Date(880698639000L));
        logln("GMT 2 is: " + (h2=cal1.get(Calendar.HOUR_OF_DAY)));
        // Note: This test had a bug in it. It wanted h1!=h2, when
        // what was meant was h1!=p. Fixed this concurrent with fix
        // to 4177484.
        if (p == h1 || h1 != h2)
            errln("Fail: Hour same in different zones");

        Calendar cal2 = new GregorianCalendar(GMT);
        Calendar cal3 = new GregorianCalendar(PST);
        cal2.set(Calendar.MILLISECOND, 0);
        cal3.set(Calendar.MILLISECOND, 0);

        cal2.set(cal1.get(Calendar.YEAR),
                 cal1.get(Calendar.MONTH),
                 cal1.get(Calendar.DAY_OF_MONTH),
                 cal1.get(Calendar.HOUR_OF_DAY),
                 cal1.get(Calendar.MINUTE),
                 cal1.get(Calendar.SECOND));

        long t1,t2,t3,t4;
        logln("RGMT 1 is: " + (t1=cal2.getTime().getTime()));
        cal3.set(year, month, day, hr, min, sec);
        logln("RPST 1 is: " + (t2=cal3.getTime().getTime()));
        cal3.setTimeZone(GMT);
        logln("RGMT 2 is: " + (t3=cal3.getTime().getTime()));
        cal3.set(cal1.get(Calendar.YEAR),
                 cal1.get(Calendar.MONTH),
                 cal1.get(Calendar.DAY_OF_MONTH),
                 cal1.get(Calendar.HOUR_OF_DAY),
                 cal1.get(Calendar.MINUTE),
                 cal1.get(Calendar.SECOND));
        // Issue 2: Calendar continues to use the timezone in its
        //          constructor for set() conversions, regardless
        //          of calls to setTimeZone()
        logln("RGMT 3 is: " + (t4=cal3.getTime().getTime()));
        if (t1 == t2 ||
            t1 != t4 ||
            t2 != t3)
            errln("Fail: Calendar zone behavior faulty");
    }
View Full Code Here

    public void Test4096539() {
        int[] y = {31,28,31,30,31,30,31,31,30,31,30,31};

        for (int x=0;x<12;x++) {
            GregorianCalendar gc = new
                GregorianCalendar(1997,x,y[x]);
            int m1,m2;
            log((m1=gc.get(Calendar.MONTH)+1)+"/"+
                             gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR)+
                             " + 1mo = ");

            gc.add(Calendar.MONTH, 1);
            logln((m2=gc.get(Calendar.MONTH)+1)+"/"+
                               gc.get(Calendar.DATE)+"/"+gc.get(Calendar.YEAR)
                               );
            int m = (m1 % 12) + 1;
            if (m2 != m)
                errln("Fail: Want " + m + " Got " + m2);
        }
View Full Code Here

        }
       
    }

    public void Test4100311() {
        GregorianCalendar cal = (GregorianCalendar)Calendar.getInstance();
        cal.set(Calendar.YEAR, 1997);
        cal.set(Calendar.DAY_OF_YEAR, 1);
        Date d = cal.getTime();             // Should be Jan 1
        logln(d.toString());
        if (cal.get(Calendar.DAY_OF_YEAR) != 1)
            errln("Fail: DAY_OF_YEAR not set");
    }
View Full Code Here

    public void Test4103271() {
        SimpleDateFormat sdf = new SimpleDateFormat();
        int numYears=40, startYear=1997, numDays=15;
        String output, testDesc;
        GregorianCalendar testCal = (GregorianCalendar)Calendar.getInstance();
        testCal.clear();
        sdf.setCalendar(testCal);
        sdf.applyPattern("d MMM yyyy");
        boolean fail = false;
        for (int firstDay=1; firstDay<=2; firstDay++) {
            for (int minDays=1; minDays<=7; minDays++) {
                testCal.setMinimalDaysInFirstWeek(minDays);
                testCal.setFirstDayOfWeek(firstDay);
                testDesc = ("Test" + String.valueOf(firstDay) + String.valueOf(minDays));
                logln(testDesc + " => 1st day of week=" +
                                   String.valueOf(firstDay) +
                                   ", minimum days in first week=" +
                                   String.valueOf(minDays));
                for (int j=startYear; j<=startYear+numYears; j++) {
                    testCal.set(j,11,25);
                    for(int i=0; i<numDays; i++) {
                        testCal.add(Calendar.DATE,1);
                        String calWOY;
                        int actWOY = testCal.get(Calendar.WEEK_OF_YEAR);
                        if (actWOY < 1 || actWOY > 53) {
                            Date d = testCal.getTime();
                            calWOY = String.valueOf(actWOY);
                            output = testDesc + " - " + sdf.format(d) + "\t";
                            output = output + "\t" + calWOY;
                            logln(output);
                            fail = true;
                        }
                    }
                }
            }
        }

        int[] DATA = {
            3, 52, 52, 52, 52, 52, 52, 52,
                1111111,
                2222222,
            4, 52, 52, 52, 52, 52, 52, 52,
               53, 53, 53, 53, 53, 53, 53,
                1111111,
        };
        testCal.setFirstDayOfWeek(Calendar.SUNDAY);
        for (int j=0; j<DATA.length; j+=22) {
            logln("Minimal days in first week = " + DATA[j] +
                               "  Week starts on Sunday");
            testCal.setMinimalDaysInFirstWeek(DATA[j]);
            testCal.set(1997, Calendar.DECEMBER, 21);
            for (int i=0; i<21; ++i) {
                int woy = testCal.get(Calendar.WEEK_OF_YEAR);
                log(testCal.getTime() + " " + woy);
                if (woy != DATA[j + 1 + i]) {
                    log(" ERROR");
                    fail = true;
                }
                //logln();
               
                // Now compute the time from the fields, and make sure we
                // get the same answer back. This is a round-trip test.
                Date save = testCal.getTime();
                testCal.clear();
                testCal.set(Calendar.YEAR, DATA[j+1+i] < 25 ? 1998 : 1997);
                testCal.set(Calendar.WEEK_OF_YEAR, DATA[j+1+i]);
                testCal.set(Calendar.DAY_OF_WEEK, (i%7) + Calendar.SUNDAY);
                if (!testCal.getTime().equals(save)) {
                    logln("  Parse failed: " + testCal.getTime());
                    fail= true;
                }

                testCal.setTime(save);
                testCal.add(Calendar.DAY_OF_MONTH, 1);
            }
        }

        Date d[] = new Date[8];
        java.util.Calendar tempcal = java.util.Calendar.getInstance();
        tempcal.clear();
        tempcal.set(1997, Calendar.DECEMBER, 28);
        d[0] = tempcal.getTime();
        tempcal.set(1998, Calendar.JANUARY, 10);
        d[1] = tempcal.getTime();
        tempcal.set(1998, Calendar.DECEMBER, 31);
        d[2] = tempcal.getTime();
        tempcal.set(1999, Calendar.JANUARY, 1);
        d[3] = tempcal.getTime();
        // Test field disambiguation with a few special hard-coded cases.
        // This shouldn't fail if the above cases aren't failing.
        Object[] DISAM = {
            new Integer(1998), new Integer(1), new Integer(Calendar.SUNDAY),
                d[0],
            new Integer(1998), new Integer(2), new Integer(Calendar.SATURDAY),
                d[1],
            new Integer(1998), new Integer(53), new Integer(Calendar.THURSDAY),
                d[2],
            new Integer(1998), new Integer(53), new Integer(Calendar.FRIDAY),
                d[3],
        };
        testCal.setMinimalDaysInFirstWeek(3);
        testCal.setFirstDayOfWeek(Calendar.SUNDAY);
        for (int i=0; i<DISAM.length; i+=4) {
            int y = ((Integer)DISAM[i]).intValue();
            int woy = ((Integer)DISAM[i+1]).intValue();
            int dow = ((Integer)DISAM[i+2]).intValue();
            Date exp = (Date)DISAM[i+3];
            testCal.clear();
            testCal.set(Calendar.YEAR, y);
            testCal.set(Calendar.WEEK_OF_YEAR, woy);
            testCal.set(Calendar.DAY_OF_WEEK, dow);
            log(y + "-W" + woy +
                             "-DOW" + dow + " expect:" + exp +
                             " got:" + testCal.getTime());
            if (!testCal.getTime().equals(exp)) {
                log("  FAIL");
                fail = true;
            }
            //logln();
        }

        // Now try adding and rolling
        Object ADD = new Object();
        Object ROLL = new Object();
        tempcal.set(1998, Calendar.DECEMBER, 25);
        d[0] = tempcal.getTime();
        tempcal.set(1999, Calendar.JANUARY, 1);
        d[1] = tempcal.getTime();
        tempcal.set(1997, Calendar.DECEMBER, 28);
        d[2] = tempcal.getTime();
        tempcal.set(1998, Calendar.JANUARY, 4);
        d[3] = tempcal.getTime();
        tempcal.set(1998, Calendar.DECEMBER, 27);
        d[4] = tempcal.getTime();
        tempcal.set(1997, Calendar.DECEMBER, 28);
        d[5] = tempcal.getTime();
        tempcal.set(1999, Calendar.JANUARY, 2);
        d[6] = tempcal.getTime();
        tempcal.set(1998, Calendar.JANUARY, 3);
        d[7] = tempcal.getTime();
       
        Object[] ADDROLL = {
            ADD, new Integer(1), d[0], d[1],
            ADD, new Integer(1), d[2], d[3],
            ROLL, new Integer(1), d[4], d[5],
            ROLL, new Integer(1), d[6], d[7],
        };
        testCal.setMinimalDaysInFirstWeek(3);
        testCal.setFirstDayOfWeek(Calendar.SUNDAY);
        for (int i=0; i<ADDROLL.length; i+=4) {
            int amount = ((Integer)ADDROLL[i+1]).intValue();
            Date before = (Date)ADDROLL[i+2];
            Date after = (Date)ADDROLL[i+3];

            testCal.setTime(before);
            if (ADDROLL[i] == ADD) testCal.add(Calendar.WEEK_OF_YEAR, amount);
            else testCal.roll(Calendar.WEEK_OF_YEAR, amount);
            log((ADDROLL[i]==ADD?"add(WOY,":"roll(WOY,") +
                             amount + ") " + before + " => " +
                             testCal.getTime());
            if (!after.equals(testCal.getTime())) {
                logln("  exp:" + after + "  FAIL");
                fail = true;
            }
            else logln(" ok");

            testCal.setTime(after);
            if (ADDROLL[i] == ADD) testCal.add(Calendar.WEEK_OF_YEAR, -amount);
            else testCal.roll(Calendar.WEEK_OF_YEAR, -amount);
            log((ADDROLL[i]==ADD?"add(WOY,":"roll(WOY,") +
                             (-amount) + ") " + after + " => " +
                             testCal.getTime());
            if (!before.equals(testCal.getTime())) {
                logln("  exp:" + before + "  FAIL");
                fail = true;
            }
            else logln(" ok");
        }
View Full Code Here

        // Use a proleptic Gregorian calendar for 0001AD and later by setting
        // the Gregorian change date before 0001AD with a value
        // that is safely before that date by any measure, i.e.,
        // more than 719164 days before 1970.
        long before0001AD = -1000000 * javaDays;
        GregorianCalendar cal = new GregorianCalendar(utc, Locale.ENGLISH);
        cal.setGregorianChange(new Date(before0001AD));

        MessageFormat fmt = new MessageFormat("{0, date, full} {0, time, full} = {1}");
        Object arguments[] = {cal, null};
       
        System.out.println("Epoch offsets:");
       
        // January 1, 0001 00:00:00 is the universal epoch date...
        cal.set(1, Calendar.JANUARY, 1, 0, 0, 0);

        long universalEpoch = cal.getTimeInMillis();
       
        for (int i = 0; i < epochDates.length; i += 1) {
            int[] date = epochDates[i];
           
            cal.set(date[0], date[1], date[2]);
           
            long millis = cal.getTimeInMillis();
           
            arguments[1] = Long.toString((millis - universalEpoch) * milliseconds);
           
            System.out.println(fmt.format(arguments));
         }
View Full Code Here

TOP

Related Classes of com.ibm.icu.util.GregorianCalendar

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.