Java Code Examples for android.icu.util.Calendar#getTime()

The following examples show how to use android.icu.util.Calendar#getTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @bug 4151631
 * SimpleDateFormat incorrect handling of 2 single quotes in format()
 */
@Test
public void Test4151631() {
    String pattern = 
        "'TO_DATE('''dd'-'MM'-'yyyy HH:mm:ss''' , ''DD-MM-YYYY HH:MI:SS'')'"; 
    logln("pattern=" + pattern);
    SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.US);
    StringBuffer result = new StringBuffer("");
    FieldPosition pos = new FieldPosition(0);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1998, Calendar.JUNE, 30, 13, 30, 0);
    Date d = cal.getTime();
    result = format.format(d, result, pos); 
    if (!result.toString().equals("TO_DATE('30-06-1998 13:30:00' , 'DD-MM-YYYY HH:MI:SS')")) {
        errln("Fail: result=" + result);
    } else {
        logln("Pass: result=" + result);
    }
}
 
Example 2
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4358730() {
    SimpleDateFormat sdf = new SimpleDateFormat();
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(2001,11,10);
    Date today = cal.getTime();

    sdf.applyPattern("MM d y");
    logln(sdf.format(today));
    sdf.applyPattern("MM d yy");
    logln(sdf.format(today));

    sdf.applyPattern("MM d yyy");
    logln(sdf.format(today));

    sdf.applyPattern("MM d yyyy");
    logln(sdf.format(today));

    sdf.applyPattern("MM d yyyyy");
    logln(sdf.format(today));
}
 
Example 3
Source File: CopticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestYear() {
    // Gregorian Calendar
    Calendar gCal= new GregorianCalendar();
    Date gToday=gCal.getTime();
    gCal.add(GregorianCalendar.MONTH,2);
    Date gFuture=gCal.getTime();
    DateFormat gDF = DateFormat.getDateInstance(gCal,DateFormat.FULL);
    logln("gregorian calendar: " + gDF.format(gToday) +
          " + 2 months = " + gDF.format(gFuture));

    // Coptic Calendar
    CopticCalendar cCal= new CopticCalendar();
    Date cToday=cCal.getTime();
    cCal.add(CopticCalendar.MONTH,2);
    Date cFuture=cCal.getTime();
    DateFormat cDF = DateFormat.getDateInstance(cCal,DateFormat.FULL);
    logln("coptic calendar: " + cDF.format(cToday) +
          " + 2 months = " + cDF.format(cFuture));

    // EthiopicCalendar
    EthiopicCalendar eCal= new EthiopicCalendar();
    Date eToday=eCal.getTime();
    eCal.add(EthiopicCalendar.MONTH,2); // add 2 months
    eCal.setAmeteAlemEra(false);
    Date eFuture=eCal.getTime();
    DateFormat eDF = DateFormat.getDateInstance(eCal,DateFormat.FULL);
    logln("ethiopic calendar: " + eDF.format(eToday) +
          " + 2 months = " + eDF.format(eFuture));
}
 
Example 4
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestT5555() throws Exception
{
    Calendar cal = Calendar.getInstance();
    
    // Set date to Wednesday, February 21, 2007
    cal.set(2007, Calendar.FEBRUARY, 21);

    try {
        // Advance month by three years
        cal.add(Calendar.MONTH, 36);
        
        // Move to last Wednesday of month.
        cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, -1);
        
        cal.getTime();
    } catch (Exception e) {
        errln("Got an exception calling getTime().");
    }
    
    int yy, mm, dd, ee;
    
    yy = cal.get(Calendar.YEAR);
    mm = cal.get(Calendar.MONTH);
    dd = cal.get(Calendar.DATE);
    ee = cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
    
    if (yy != 2010 || mm != Calendar.FEBRUARY || dd != 24 || ee != Calendar.WEDNESDAY) {
        errln("Got date " + yy + "/" + (mm + 1) + "/" + dd + ", expected 2010/2/24");
    }
}
 
Example 5
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @bug 4104136
 */
@Test
public void Test4104136() {
    SimpleDateFormat sdf = new SimpleDateFormat();
    String pattern = "'time' hh:mm";
    sdf.applyPattern(pattern);
    logln("pattern: \"" + pattern + "\"");
    String strings[] = {"time 10:30", "time 10:x", "time 10x"};
    ParsePosition ppos[] = {new ParsePosition(10), new ParsePosition(0), new ParsePosition(0)};
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(1970, Calendar.JANUARY, 1, 10, 30);
    Date dates[] = {cal.getTime(), new Date(-1), new Date(-1)};
    for (int i = 0; i < 3; i++) {
        String text = strings[i];
        ParsePosition finish = ppos[i];
        Date exp = dates[i];
        ParsePosition pos = new ParsePosition(0);
        Date d = sdf.parse(text, pos);
        logln(" text: \"" + text + "\"");
        logln(" index: %d" + pos.getIndex());
        logln(" result: " + d);
        if (pos.getIndex() != finish.getIndex())
            errln("Fail: Expected pos " + finish.getIndex());
        if (!((d == null && exp.equals(new Date(-1))) || (d.equals(exp))))
            errln( "Fail: Expected result " + exp);
    }
}
 
Example 6
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Calendar does not update field values when setTimeZone is called.
 */
@Test
public void Test4177484() {
    TimeZone PST = TimeZone.getTimeZone("PST");
    TimeZone EST = TimeZone.getTimeZone("EST");

    Calendar cal = Calendar.getInstance(PST, Locale.US);
    cal.clear();
    cal.set(1999, 3, 21, 15, 5, 0); // Arbitrary
    int h1 = cal.get(Calendar.HOUR_OF_DAY);
    cal.setTimeZone(EST);
    int h2 = cal.get(Calendar.HOUR_OF_DAY);
    if (h1 == h2) {
        errln("FAIL: Fields not updated after setTimeZone");
    }

    // getTime() must NOT change when time zone is changed.
    // getTime() returns zone-independent time in ms.
    cal.clear();
    cal.setTimeZone(PST);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    Date pst10 = cal.getTime();
    cal.setTimeZone(EST);
    Date est10 = cal.getTime();
    if (!pst10.equals(est10)) {
        errln("FAIL: setTimeZone changed time");
    }
}
 
Example 7
Source File: IBMCalendarTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure that when adding a day, we actually wind up in a
 * different day.  The DST adjustments we use to keep the hour
 * constant across DST changes can backfire and change the day.
 */
@Test
public void TestTimeZoneTransitionAdd() {
    Locale locale = Locale.US; // could also be CHINA
    SimpleDateFormat dateFormat =
        new SimpleDateFormat("MM/dd/yyyy HH:mm z", locale);

    String tz[] = TimeZone.getAvailableIDs();

    for (int z=0; z<tz.length; ++z) {
        TimeZone t = TimeZone.getTimeZone(tz[z]);
        dateFormat.setTimeZone(t);

        Calendar cal = Calendar.getInstance(t, locale);
        cal.clear();
        // Scan the year 2003, overlapping the edges of the year
        cal.set(Calendar.YEAR, 2002);
        cal.set(Calendar.MONTH, Calendar.DECEMBER);
        cal.set(Calendar.DAY_OF_MONTH, 25);

        for (int i=0; i<365+10; ++i) {
            Date yesterday = cal.getTime();
            int yesterday_day = cal.get(Calendar.DAY_OF_MONTH);
            cal.add(Calendar.DAY_OF_MONTH, 1);
            if (yesterday_day == cal.get(Calendar.DAY_OF_MONTH)) {
                errln(tz[z] + " " +
                      dateFormat.format(yesterday) + " +1d= " +
                      dateFormat.format(cal.getTime()));
            }
        }
    }
}
 
Example 8
Source File: IBMCalendarTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test behavior of fieldDifference around leap years.  Also test a large
 * field difference to check binary search.
 */
@Test
public void TestLeapFieldDifference() {
    Calendar cal = Calendar.getInstance();
    cal.set(2004, Calendar.FEBRUARY, 29);
    Date date2004 = cal.getTime();
    cal.set(2000, Calendar.FEBRUARY, 29);
    Date date2000 = cal.getTime();
    int y = cal.fieldDifference(date2004, Calendar.YEAR);
    int d = cal.fieldDifference(date2004, Calendar.DAY_OF_YEAR);
    if (d == 0) {
        logln("Ok: 2004/Feb/29 - 2000/Feb/29 = " + y + " years, " + d + " days");
    } else {
        errln("FAIL: 2004/Feb/29 - 2000/Feb/29 = " + y + " years, " + d + " days");
    }
    cal.setTime(date2004);
    y = cal.fieldDifference(date2000, Calendar.YEAR);
    d = cal.fieldDifference(date2000, Calendar.DAY_OF_YEAR);
    if (d == 0) {
        logln("Ok: 2000/Feb/29 - 2004/Feb/29 = " + y + " years, " + d + " days");
    } else {
        errln("FAIL: 2000/Feb/29 - 2004/Feb/29 = " + y + " years, " + d + " days");
    }
    // Test large difference
    cal.set(2001, Calendar.APRIL, 5); // 2452005
    Date ayl = cal.getTime();
    cal.set(1964, Calendar.SEPTEMBER, 7); // 2438646
    Date asl = cal.getTime();
    d = cal.fieldDifference(ayl, Calendar.DAY_OF_MONTH);
    cal.setTime(ayl);
    int d2 = cal.fieldDifference(asl, Calendar.DAY_OF_MONTH);
    if (d == -d2 && d == 13359) {
        logln("Ok: large field difference symmetrical " + d);
    } else {
        logln("FAIL: large field difference incorrect " + d + ", " + d2 +
              ", expect +/- 13359");
    }
}
 
Example 9
Source File: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * @return Returns the number of weeks between the current <code>date</code>
 *         and the <code>mMinDate</code>.
 */
private int getWeeksSinceMinDate(Calendar date) {
    if (date.before(mMinDate)) {
        throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
                + " does not precede toDate: " + date.getTime());
    }
    long endTimeMillis = date.getTimeInMillis()
            + date.getTimeZone().getOffset(date.getTimeInMillis());
    long startTimeMillis = mMinDate.getTimeInMillis()
            + mMinDate.getTimeZone().getOffset(mMinDate.getTimeInMillis());
    long dayOffsetMillis = (mMinDate.get(Calendar.DAY_OF_WEEK) - mFirstDayOfWeek)
            * MILLIS_IN_DAY;
    return (int) ((endTimeMillis - startTimeMillis + dayOffsetMillis) / MILLIS_IN_WEEK);
}
 
Example 10
Source File: ChineseTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestInitWithCurrentTime() {
    // jb4555
    // if the chinese calendar current millis isn't called, the default year is wrong.
    // this test is assuming the 'year' is the current cycle
    // so when we cross a cycle boundary, the target will need to change
    // that shouldn't be for awhile yet... 

    ChineseCalendar cc = new ChineseCalendar();
    cc.set(Calendar.YEAR, 22);
    cc.set(Calendar.MONTH, 0);
     // need to set leap month flag off, otherwise, the test case always fails when
     // current time is in a leap month
    cc.set(Calendar.IS_LEAP_MONTH, 0);
    cc.set(Calendar.DATE, 19);
    cc.set(Calendar.HOUR_OF_DAY, 0);
    cc.set(Calendar.MINUTE, 0);
    cc.set(Calendar.SECOND, 0);
    cc.set(Calendar.MILLISECOND, 0);

    cc.add(Calendar.DATE, 1);
 
    Calendar cal = new GregorianCalendar(2005, Calendar.FEBRUARY, 28);
    Date target = cal.getTime();
    Date result = cc.getTime();

    assertEquals("chinese and gregorian date should match", target, result);
}
 
Example 11
Source File: DangiTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestInitWithCurrentTime() {
    // If the chinese calendar current millis isn't called, the default year is wrong.
    // this test is assuming the 'year' is the current cycle
    // so when we cross a cycle boundary, the target will need to change
    // that shouldn't be for awhile yet... 

    Calendar cc = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    cc.set(Calendar.EXTENDED_YEAR, 4338);
    cc.set(Calendar.MONTH, 0);
     // need to set leap month flag off, otherwise, the test case always fails when
     // current time is in a leap month
    cc.set(Calendar.IS_LEAP_MONTH, 0);
    cc.set(Calendar.DATE, 19);
    cc.set(Calendar.HOUR_OF_DAY, 0);
    cc.set(Calendar.MINUTE, 0);
    cc.set(Calendar.SECOND, 0);
    cc.set(Calendar.MILLISECOND, 0);

    cc.add(Calendar.DATE, 1);
 
    Calendar cal = new GregorianCalendar(2005, Calendar.FEBRUARY, 28);
    Date target = cal.getTime();
    Date result = cc.getTime();

    assertEquals("chinese and gregorian date should match", target, result);
}
 
Example 12
Source File: CompatibilityTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestSecondsZero121() {
    Calendar        cal = new GregorianCalendar();
    // Initialize with current date/time
    cal.setTime(new Date());
    // Round down to minute
    cal.set(Calendar.SECOND, 0);
    Date    d = cal.getTime();
    String s = d.toString();
    if (s.indexOf(":00 ") < 0) errln("Expected to see :00 in " + s);
}
 
Example 13
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Check that the given year/month/dom/hour maps to and from the
 * given epochHours.  This verifies the functioning of the
 * calendar and time zone in conjunction with one another,
 * including the calendar time->fields and fields->time and
 * the time zone getOffset method.
 *
 * @param epochHours hours after Jan 1 1970 0:00 GMT.
 */
void verifyMapping(Calendar cal, int year, int month, int dom, int hour,
                   double epochHours) {
    double H = 3600000.0;
    cal.clear();
    cal.set(year, month, dom, hour, 0, 0);
    Date d = cal.getTime();
    double e = d.getTime() / H;
    Date ed = new Date((long)(epochHours * H));
    if (e == epochHours) {
        logln("Ok: " + year + "/" + (month+1) + "/" + dom + " " + hour + ":00 => " +
              e + " (" + ed + ")");
    } else {
        errln("FAIL: " + year + "/" + (month+1) + "/" + dom + " " + hour + ":00 => " +
              e + " (" + new Date((long)(e * H)) + ")" +
              ", expected " + epochHours + " (" + ed + ")");
    }
    cal.setTime(ed);
    if (cal.get(Calendar.YEAR) == year &&
        cal.get(Calendar.MONTH) == month &&
        cal.get(Calendar.DATE) == dom &&
        cal.get(Calendar.MILLISECONDS_IN_DAY) == hour * 3600000) {
        logln("Ok: " + epochHours + " (" + ed + ") => " +
              cal.get(Calendar.YEAR) + "/" +
              (cal.get(Calendar.MONTH)+1) + "/" +
              cal.get(Calendar.DATE) + " " +
              cal.get(Calendar.MILLISECONDS_IN_DAY)/H);
    } else {
        errln("FAIL: " + epochHours + " (" + ed + ") => " +
              cal.get(Calendar.YEAR) + "/" +
              (cal.get(Calendar.MONTH)+1) + "/" +
              cal.get(Calendar.DATE) + " " +
              cal.get(Calendar.MILLISECONDS_IN_DAY)/H +
              ", expected " + year + "/" + (month+1) + "/" + dom +
              " " + hour);
    }
}
 
Example 14
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void Test4250359() {
    Locale.setDefault(Locale.US);
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(101 + 1900, 9, 9, 17, 53);
    Date d = cal.getTime();
    DateFormat tf = DateFormat.getTimeInstance(DateFormat.SHORT);
    String act_result = tf.format(d);
    String exp_result = "5:53 PM";
    
    if(!act_result.equals(exp_result)){
        errln("The result is not expected");
    }
}
 
Example 15
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * j32 {JDK Bug 4210209 4209272}
 * DateFormat cannot parse Feb 29 2000 when setLenient(false)
 */
@Test
public void Test4210209() {

    String pattern = "MMM d, yyyy";
    DateFormat fmt = new SimpleDateFormat(pattern, Locale.US);
    DateFormat disp = new SimpleDateFormat("MMM dd yyyy GG", Locale.US);

    Calendar calx = fmt.getCalendar();
    calx.setLenient(false);
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(2000, Calendar.FEBRUARY, 29);
    Date d = calendar.getTime();
    String s = fmt.format(d);
    logln(disp.format(d) + " f> " + pattern + " => \"" + s + "\"");
    ParsePosition pos = new ParsePosition(0);
    d = fmt.parse(s, pos);
    logln("\"" + s + "\" p> " + pattern + " => " +
          (d!=null?disp.format(d):"null"));
    logln("Parse pos = " + pos.getIndex() + ", error pos = " + pos.getErrorIndex());
    if (pos.getErrorIndex() != -1) {
        errln("FAIL: Error index should be -1");
    }

    // The underlying bug is in GregorianCalendar.  If the following lines
    // succeed, the bug is fixed.  If the bug isn't fixed, they will throw
    // an exception.
    GregorianCalendar cal = new GregorianCalendar();
    cal.clear();
    cal.setLenient(false);
    cal.set(2000, Calendar.FEBRUARY, 29); // This should work!
    d = cal.getTime();
    logln("Attempt to set Calendar to Feb 29 2000: " + disp.format(d));
}
 
Example 16
Source File: SimpleDateFormat.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void initializeDefaultCenturyStart(long baseTime) {
    defaultCenturyBase = baseTime;
    // clone to avoid messing up date stored in calendar object
    // when this method is called while parsing
    Calendar tmpCal = (Calendar)calendar.clone();
    tmpCal.setTimeInMillis(baseTime);
    tmpCal.add(Calendar.YEAR, -80);
    defaultCenturyStart = tmpCal.getTime();
    defaultCenturyStartYear = tmpCal.get(Calendar.YEAR);
}
 
Example 17
Source File: TimeZoneBoundaryTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Given a date, a TimeZone, and expected values for inDaylightTime,
 * useDaylightTime, zone and DST offset, verify that this is the case.
 */
void verifyDST(String tag, Calendar cal, TimeZone time_zone,
               boolean expUseDaylightTime, boolean expInDaylightTime,
               int expRawOffset, int expOffset)
{
    Date d = cal.getTime();

    logln("-- " + tag + ": " + d +
          " in zone " + time_zone.getID() + " (" +
          d.getTime()/3600000.0 + ")");

    if (time_zone.inDaylightTime(d) == expInDaylightTime)
        logln("PASS: inDaylightTime = " + time_zone.inDaylightTime(d));
    else
        errln("FAIL: inDaylightTime = " + time_zone.inDaylightTime(d));

    if (time_zone.useDaylightTime() == expUseDaylightTime)
        logln("PASS: useDaylightTime = " + time_zone.useDaylightTime());
    else
        errln("FAIL: useDaylightTime = " + time_zone.useDaylightTime());

    if (time_zone.getRawOffset() == expRawOffset)
        logln("PASS: getRawOffset() = " + expRawOffset/(double)ONE_HOUR);
    else
        errln("FAIL: getRawOffset() = " + time_zone.getRawOffset()/(double)ONE_HOUR +
              "; expected " + expRawOffset/(double)ONE_HOUR);

    //GregorianCalendar gc = new GregorianCalendar(time_zone);
    //gc.setTime(d);
    int offset = time_zone.getOffset(cal.get(Calendar.ERA), cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
                                     cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.DAY_OF_WEEK),
                                     ((cal.get(Calendar.HOUR_OF_DAY) * 60 +
                                       cal.get(Calendar.MINUTE)) * 60 +
                                      cal.get(Calendar.SECOND)) * 1000 +
                                     cal.get(Calendar.MILLISECOND));
    if (offset == expOffset)
        logln("PASS: getOffset() = " + offset/(double)ONE_HOUR);
    else {
        logln("era=" + cal.get(Calendar.ERA) +
              ", year=" + cal.get(Calendar.YEAR) +
              ", month=" + cal.get(Calendar.MONTH) +
              ", dom=" + cal.get(Calendar.DAY_OF_MONTH) +
              ", dow=" + cal.get(Calendar.DAY_OF_WEEK) +
              ", time-of-day=" + (((cal.get(Calendar.HOUR_OF_DAY) * 60 +
                           cal.get(Calendar.MINUTE)) * 60 +
                          cal.get(Calendar.SECOND)) * 1000 +
                         cal.get(Calendar.MILLISECOND)) / 3600000.0 +
                        " hours");
        errln("FAIL: getOffset() = " + offset/(double)ONE_HOUR +
              "; expected " + expOffset/(double)ONE_HOUR);
    }
}
 
Example 18
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * @bug 4052408
 */
@Test
public void Test4052408() {

    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US); 
    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(97 + 1900, Calendar.MAY, 3, 8, 55);
    Date dt = cal.getTime();
    String str = fmt.format(dt);
    logln(str);
    
    if (!str.equals("5/3/97, 8:55 AM"))
        errln("Fail: Test broken; Want 5/3/97, 8:55 AM Got " + str);

    String expected[] = {
        "", //"ERA_FIELD",
        "97", //"YEAR_FIELD",
        "5", //"MONTH_FIELD",
        "3", //"DATE_FIELD",
        "", //"HOUR_OF_DAY1_FIELD",
        "", //"HOUR_OF_DAY0_FIELD",
        "55", //"MINUTE_FIELD",
        "", //"SECOND_FIELD",
        "", //"MILLISECOND_FIELD",
        "", //"DAY_OF_WEEK_FIELD",
        "", //"DAY_OF_YEAR_FIELD",
        "", //"DAY_OF_WEEK_IN_MONTH_FIELD",
        "", //"WEEK_OF_YEAR_FIELD",
        "", //"WEEK_OF_MONTH_FIELD",
        "AM", //"AM_PM_FIELD",
        "8", //"HOUR1_FIELD",
        "", //"HOUR0_FIELD",
        "" //"TIMEZONE_FIELD"
        };        
    String fieldNames[] = {
            "ERA_FIELD", 
            "YEAR_FIELD", 
            "MONTH_FIELD", 
            "DATE_FIELD", 
            "HOUR_OF_DAY1_FIELD", 
            "HOUR_OF_DAY0_FIELD", 
            "MINUTE_FIELD", 
            "SECOND_FIELD", 
            "MILLISECOND_FIELD", 
            "DAY_OF_WEEK_FIELD", 
            "DAY_OF_YEAR_FIELD", 
            "DAY_OF_WEEK_IN_MONTH_FIELD", 
            "WEEK_OF_YEAR_FIELD", 
            "WEEK_OF_MONTH_FIELD", 
            "AM_PM_FIELD", 
            "HOUR1_FIELD", 
            "HOUR0_FIELD", 
            "TIMEZONE_FIELD"}; 

    boolean pass = true;
    for (int i = 0; i <= 17; ++i) {
        FieldPosition pos = new FieldPosition(i);
        StringBuffer buf = new StringBuffer("");
        fmt.format(dt, buf, pos);
        //char[] dst = new char[pos.getEndIndex() - pos.getBeginIndex()];
        String dst = buf.substring(pos.getBeginIndex(), pos.getEndIndex());
        str = dst;
        log(i + ": " + fieldNames[i] + ", \"" + str + "\", "
                + pos.getBeginIndex() + ", " + pos.getEndIndex()); 
        String exp = expected[i];
        if ((exp.length() == 0 && str.length() == 0) || str.equals(exp))
            logln(" ok");
        else {
            logln(" expected " + exp);
            pass = false;
        }
    }
    if (!pass)
        errln("Fail: FieldPosition not set right by DateFormat");
}
 
Example 19
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Test fieldDifference().
 */
@Test
public void TestJ438() throws Exception {
    int DATA[] = {
        2000, Calendar.JANUARY, 20,   2010, Calendar.JUNE, 15,
        2010, Calendar.JUNE, 15,      2000, Calendar.JANUARY, 20,
        1964, Calendar.SEPTEMBER, 7,  1999, Calendar.JUNE, 4,
        1999, Calendar.JUNE, 4,       1964, Calendar.SEPTEMBER, 7,
    };
    Calendar cal = Calendar.getInstance(Locale.US);
    for (int i=0; i<DATA.length; i+=6) {
        int y1 = DATA[i];
        int m1 = DATA[i+1];
        int d1 = DATA[i+2];
        int y2 = DATA[i+3];
        int m2 = DATA[i+4];
        int d2 = DATA[i+5];

        cal.clear();
        cal.set(y1, m1, d1);
        Date date1 = cal.getTime();
        cal.set(y2, m2, d2);
        Date date2 = cal.getTime();

        cal.setTime(date1);
        int dy = cal.fieldDifference(date2, Calendar.YEAR);
        int dm = cal.fieldDifference(date2, Calendar.MONTH);
        int dd = cal.fieldDifference(date2, Calendar.DATE);

        logln("" + date2 + " - " + date1 + " = " +
              dy + "y " + dm + "m " + dd + "d");

        cal.setTime(date1);
        cal.add(Calendar.YEAR, dy);
        cal.add(Calendar.MONTH, dm);
        cal.add(Calendar.DATE, dd);
        Date date22 = cal.getTime();
        if (!date2.equals(date22)) {
            errln("FAIL: " + date1 + " + " +
                  dy + "y " + dm + "m " + dd + "d = " +
                  date22 + ", exp " + date2);
        } else {
            logln("Ok: " + date1 + " + " +
                  dy + "y " + dm + "m " + dd + "d = " +
                  date22);
        }
    }
}
 
Example 20
Source File: IBMCalendarTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * Run a test of a quasi-Gregorian calendar.  This is a calendar
 * that behaves like a Gregorian but has different year/era mappings.
 * The int[] data array should have the format:
 *
 * { era, year, gregorianYear, month, dayOfMonth, ... }
 */
void quasiGregorianTest(Calendar cal, int[] data) {
    // As of JDK 1.4.1_01, using the Sun JDK GregorianCalendar as
    // a reference throws us off by one hour.  This is most likely
    // due to the JDK 1.4 incorporation of historical time zones.
    //java.util.Calendar grego = java.util.Calendar.getInstance();
    Calendar grego = Calendar.getInstance();
    for (int i=0; i<data.length; ) {
        int era = data[i++];
        int year = data[i++];
        int gregorianYear = data[i++];
        int month = data[i++];
        int dayOfMonth = data[i++];

        grego.clear();
        grego.set(gregorianYear, month, dayOfMonth);
        Date D = grego.getTime();

        cal.clear();
        cal.set(Calendar.ERA, era);
        cal.set(year, month, dayOfMonth);
        Date d = cal.getTime();
        if (d.equals(D)) {
            logln("OK: " + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
                  " => " + d);
        } else {
            errln("Fail: " + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
                  " => " + d + ", expected " + D);
        }

        cal.clear();
        cal.setTime(D);
        int e = cal.get(Calendar.ERA);
        int y = cal.get(Calendar.YEAR);
        if (y == year && e == era) {
            logln("OK: " + D + " => " + cal.get(Calendar.ERA) + ":" +
                  cal.get(Calendar.YEAR) + "/" +
                  (cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE));
        } else {
            logln("Fail: " + D + " => " + cal.get(Calendar.ERA) + ":" +
                  cal.get(Calendar.YEAR) + "/" +
                  (cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE) +
                  ", expected " + era + ":" + year + "/" + (month+1) + "/" +
                  dayOfMonth);
        }
    }
}