Java Code Examples for android.icu.text.SimpleDateFormat#setCalendar()

The following examples show how to use android.icu.text.SimpleDateFormat#setCalendar() . 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: EthiopicTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestEraStart() {
    SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd, yyyy GG");
    fmt.setCalendar(new EthiopicCalendar());

    EthiopicCalendar cal = new EthiopicCalendar(1, 0, 1);
    assertEquals("Ethiopic Date", "Wed Jan 01, 0001 AD", fmt.format(cal));

    cal.set(Calendar.ERA, 0);
    cal.set(Calendar.YEAR, 5500);
    assertEquals("Ethiopic Date", "Tue Jan 01, 5500 BC", fmt.format(cal));

    // The gregorian calendar gets off by two days when
    // the date gets low, unless the gregorian changeover is set to 
    // very early.  The funny thing is, it's ok for dates in the year
    // 283, but not in the year 7, and it claims to be ok until the year 4.
    // should track down when the dates start to differ...
    
    GregorianCalendar gc = new GregorianCalendar();
    gc.setGregorianChange(new Date(Long.MIN_VALUE)); // act like proleptic Gregorian
    gc.setTime(cal.getTime());
    fmt.setCalendar(new GregorianCalendar());
    assertEquals("Gregorian Date", "Tue Aug 28, 0007 AD", fmt.format(gc));
}
 
Example 2
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4061476() {
    SimpleDateFormat fmt = new SimpleDateFormat("ddMMMyy", Locale.UK);
    Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), 
                                                 Locale.UK);
    fmt.setCalendar(cal);
    try
        {
            Date date = fmt.parse("29MAY97");
            cal.setTime(date);
        }
    catch (Exception e) {
        System.out.print("");
    }
    cal.set(Calendar.HOUR_OF_DAY, 13);
    logln("Hour: "+cal.get(Calendar.HOUR_OF_DAY));
    cal.add(Calendar.HOUR_OF_DAY, 6);
    logln("Hour: "+cal.get(Calendar.HOUR_OF_DAY));
    if (cal.get(Calendar.HOUR_OF_DAY) != 19)
        errln("Fail: Want 19 Got " + cal.get(Calendar.HOUR_OF_DAY));
}
 
Example 3
Source File: CopticTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestEraStart() {
    SimpleDateFormat fmt = new SimpleDateFormat("EEE MMM dd, yyyy GG");
    SimpleDateFormat copticFmt = new SimpleDateFormat("EEE MMM dd, yyyy GG");
    copticFmt.setCalendar(new CopticCalendar());

    CopticCalendar cal = new CopticCalendar(1, 0, 1);
    assertEquals("Coptic Date", "Fri Jan 01, 0001 AD", copticFmt.format(cal));
    assertEquals("Gregorian Date", "Fri Aug 29, 0284 AD", fmt.format(cal.getTime()));

    cal.set(Calendar.ERA, 0);
    cal.set(Calendar.YEAR, 1);
    assertEquals("Coptic Date", "Thu Jan 01, 0001 BC", copticFmt.format(cal));
    assertEquals("Gregorian Date", "Thu Aug 30, 0283 AD", fmt.format(cal.getTime()));
}
 
Example 4
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Test to see if DateFormat understands zone equivalency groups.  It
 * might seem that this should be a DateFormat test, but it's really a
 * TimeZone test -- the changes to DateFormat are minor.
 *
 * We use two known, zones that are equivalent, where one zone has
 * localized name data, and the other doesn't, in some locale.
 */
@Test
public void TestJ449() {
    // not used String str;

    // Modify the following three as necessary.  The two IDs must
    // specify two zones in the same equivalency group.  One must have
    // locale data in 'loc'; the other must not.
    String idWithLocaleData = "America/Los_Angeles";
    String idWithoutLocaleData = "PST"; // "US/Pacific";
    Locale loc = new Locale("en", "", "");

    TimeZone zoneWith = TimeZone.getTimeZone(idWithLocaleData);
    TimeZone zoneWithout = TimeZone.getTimeZone(idWithoutLocaleData);
    // Make sure we got valid zones
    if (!(zoneWith.getID().equals(idWithLocaleData) &&
          zoneWithout.getID().equals(idWithoutLocaleData))) {
        warnln("Fail: Unable to create zones");
    } else {
        GregorianCalendar calWith = new GregorianCalendar(zoneWith);
        GregorianCalendar calWithout = new GregorianCalendar(zoneWithout);
        SimpleDateFormat fmt =
            new SimpleDateFormat("MMM d yyyy hh:mm a zzz", loc);
        Date date = new Date(0L);
        fmt.setCalendar(calWith);
        String strWith = fmt.format(date);
        fmt.setCalendar(calWithout);
        String strWithout = fmt.format(date);
        if (strWith.equals(strWithout)) {
            logln("Ok: " + idWithLocaleData + " -> " +
                  strWith + "; " + idWithoutLocaleData + " -> " +
                  strWithout);
        } else {
            errln("FAIL: " + idWithLocaleData + " -> " +
                  strWith + "; " + idWithoutLocaleData + " -> " +
                  strWithout);
        }
    }
}
 
Example 5
Source File: TimeZoneRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
boolean checkCalendar314(GregorianCalendar testCal, TimeZone testTZ) {
    // GregorianCalendar testCal = (GregorianCalendar)aCal.clone();

    final int ONE_DAY = 24*60*60*1000;

    int tzOffset, tzRawOffset;
    Float tzOffsetFloat,tzRawOffsetFloat;
    // Here is where the user made an error.  They were passing in the value of
    // the MILLSECOND field; you need to pass in the millis in the day in STANDARD
    // time.
    int millis = testCal.get(Calendar.MILLISECOND) +
        1000 * (testCal.get(Calendar.SECOND) +
                60 * (testCal.get(Calendar.MINUTE) +
                      60 * (testCal.get(Calendar.HOUR_OF_DAY)))) -
        testCal.get(Calendar.DST_OFFSET);

    /* Fix up millis to be in range.  ASSUME THAT WE ARE NOT AT THE
     * BEGINNING OR END OF A MONTH.  We must add this code because
     * getOffset() has been changed to be more strict about the parameters
     * it receives -- it turns out that this test was passing in illegal
     * values. */
    int date = testCal.get(Calendar.DATE);
    int dow  = testCal.get(Calendar.DAY_OF_WEEK);
    while (millis < 0) {
        millis += ONE_DAY;
        --date;
        dow = Calendar.SUNDAY + ((dow - Calendar.SUNDAY + 6) % 7);
    }
    while (millis >= ONE_DAY) {
        millis -= ONE_DAY;
        ++date;
        dow = Calendar.SUNDAY + ((dow - Calendar.SUNDAY + 1) % 7);
    }

    tzOffset = testTZ.getOffset(testCal.get(Calendar.ERA),
                                testCal.get(Calendar.YEAR),
                                testCal.get(Calendar.MONTH),
                                date,
                                dow,
                                millis);
    tzRawOffset = testTZ.getRawOffset();
    tzOffsetFloat = new Float((float)tzOffset/(float)3600000);
    tzRawOffsetFloat = new Float((float)tzRawOffset/(float)3600000);

    Date testDate = testCal.getTime();

    boolean inDaylightTime = testTZ.inDaylightTime(testDate);
    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    sdf.setCalendar(testCal);
    String inDaylightTimeString;

    boolean passed;

    if (inDaylightTime)
    {
        inDaylightTimeString = " DST ";
        passed = (tzOffset == (tzRawOffset + 3600000));
    }
    else
    {
        inDaylightTimeString = "     ";
        passed = (tzOffset == tzRawOffset);
    }

    String output = testTZ.getID() + " " + sdf.format(testDate) +
        " Offset(" + tzOffsetFloat + ")" +
        " RawOffset(" + tzRawOffsetFloat + ")" +
        " " + millis/(float)3600000 + " " +
        inDaylightTimeString;

    if (passed)
        output += "     ";
    else
        output += "ERROR";

    if (passed) logln(output); else errln(output);
    return passed;
}
 
Example 6
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
  public void TestT10239() {
      
      class TestDateFormatItem {
          public String parseString;
          public String pattern;
          public String expectedResult;   // null indicates expected error
          // Simple constructor
          public TestDateFormatItem(String parString, String patt, String expResult) {
              pattern = patt;
              parseString = parString;
              expectedResult = expResult;
          }
      };
      
      final TestDateFormatItem[] items = {
      //                     parse String                 pattern                 expected result
      new TestDateFormatItem("1 Oct 13 2013",             "e MMM dd yyyy",        "1 Oct 13 2013"),
      new TestDateFormatItem("02 Oct 14 2013",            "ee MMM dd yyyy",       "02 Oct 14 2013"),
      new TestDateFormatItem("Tue Oct 15 2013",           "eee MMM dd yyyy",      "Tue Oct 15 2013"),
      new TestDateFormatItem("Wednesday  Oct 16 2013",    "eeee MMM dd yyyy",     "Wednesday Oct 16 2013"),
      new TestDateFormatItem("Th Oct 17 2013",            "eeeeee MMM dd yyyy",   "Th Oct 17 2013"),
      new TestDateFormatItem("Fr Oct 18 2013",            "EEEEEE MMM dd yyyy",   "Fr Oct 18 2013"),
      new TestDateFormatItem("S Oct 19 2013",             "eeeee MMM dd yyyy",    "S Oct 19 2013"),
      new TestDateFormatItem("S Oct 20 2013",             "EEEEE MMM dd yyyy",    "S Oct 20 2013"),
      };

      StringBuffer result = new StringBuffer();
      Date d = new Date();
      Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US); 
      SimpleDateFormat sdfmt = new SimpleDateFormat();
      ParsePosition p = new ParsePosition(0);
      for (TestDateFormatItem item: items) {
          cal.clear();
          sdfmt.setCalendar(cal);
          sdfmt.applyPattern(item.pattern);
          result.setLength(0);
          p.setIndex(0);
          p.setErrorIndex(-1);
          d = sdfmt.parse(item.parseString, p);
          if(item.expectedResult == null) {
              if(p.getErrorIndex() != -1)
                  continue;
              else
                  errln("error: unexpected parse success..."+item.parseString + " should have failed");
          }
          if(p.getErrorIndex() != -1) {
              errln("error: parse error for string " +item.parseString + " against pattern " + item.pattern + " -- idx["+p.getIndex()+"] errIdx["+p.getErrorIndex()+"]");
              continue;
          }
          cal.setTime(d);
          result = sdfmt.format(cal, result, new FieldPosition(0));
          if(!result.toString().equalsIgnoreCase(item.expectedResult)) {
              errln("error: unexpected format result. expected - " + item.expectedResult + "  but result was - " + result);
          } else {
              logln("formatted results match! - " + result.toString());
          }
      }
}
 
Example 7
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
  public void TestT10619() {
      
      class TestDateFormatLeniencyItem {
          public boolean leniency;
          public String parseString;
          public String pattern;
          public String expectedResult;   // null indicates expected error
           // Simple constructor
          public TestDateFormatLeniencyItem(boolean len, String parString, String patt, String expResult) {
              leniency = len;
              pattern = patt;
              parseString = parString;
              expectedResult = expResult;
          }
      };

      final TestDateFormatLeniencyItem[] items = {
          //                             leniency    parse String       pattern                 expected result
          new TestDateFormatLeniencyItem(true,       "2008-Jan 02",     "yyyy-LLL. dd",         "2008-Jan. 02"),
          new TestDateFormatLeniencyItem(false,      "2008-Jan 03",     "yyyy-LLL. dd",         null),
          new TestDateFormatLeniencyItem(true,       "2008-Jan--04",    "yyyy-MMM' -- 'dd",     "2008-Jan -- 04"),
          new TestDateFormatLeniencyItem(false,      "2008-Jan--05",    "yyyy-MMM' -- 'dd",     null),
          new TestDateFormatLeniencyItem(true,       "2008-12-31",      "yyyy-mm-dd",           "2008-12-31"),
          new TestDateFormatLeniencyItem(false,      "6 Jan 05 2008",   "eee MMM dd yyyy",      null),
          new TestDateFormatLeniencyItem(true,       "6 Jan 05 2008",   "eee MMM dd yyyy",      "Sat Jan 05 2008"),
      };

      StringBuffer result = new StringBuffer();
      Date d = new Date();
      Calendar cal = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US); 
      SimpleDateFormat sdfmt = new SimpleDateFormat();
      ParsePosition p = new ParsePosition(0);
      for (TestDateFormatLeniencyItem item: items) {
          cal.clear();
          sdfmt.setCalendar(cal);
          sdfmt.applyPattern(item.pattern);
          sdfmt.setLenient(item.leniency);
          result.setLength(0);
          p.setIndex(0);
          p.setErrorIndex(-1);
          d = sdfmt.parse(item.parseString, p);
          if(item.expectedResult == null) {
              if(p.getErrorIndex() != -1)
                  continue;
              else
                  errln("error: unexpected parse success..."+item.parseString + " w/ lenient="+item.leniency+" should have failed");
          }
          if(p.getErrorIndex() != -1) {
              errln("error: parse error for string " +item.parseString + " -- idx["+p.getIndex()+"] errIdx["+p.getErrorIndex()+"]");
              continue;
          }
          cal.setTime(d);
          result = sdfmt.format(cal, result, new FieldPosition(0));
          if(!result.toString().equalsIgnoreCase(item.expectedResult)) {
              errln("error: unexpected format result. expected - " + item.expectedResult + "  but result was - " + result);
          } else {
              logln("formatted results match! - " + result.toString());
          }
      }
}