Search in sources :

Example 86 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class CalendarRegressionTest method TestT9968.

/**
 * Test case for ticket 9968
 * subparse fails to return an error indication when start pos is 0
 */
@Test
public void TestT9968() {
    SimpleDateFormat sdf0 = new SimpleDateFormat("-MMMM");
    ParsePosition pos0 = new ParsePosition(0);
    /* Date d0 = */
    sdf0.parse("-September", pos0);
    logln("sdf0: " + pos0.getErrorIndex() + "/" + pos0.getIndex());
    assertTrue("Fail: failed a good test", pos0.getErrorIndex() == -1);
    SimpleDateFormat sdf1 = new SimpleDateFormat("-MMMM");
    ParsePosition pos1 = new ParsePosition(0);
    /* Date d1 = */
    sdf1.parse("-????", pos1);
    logln("sdf1: " + pos1.getErrorIndex() + "/" + pos1.getIndex());
    assertTrue("Fail: failed to detect bad parse", pos1.getErrorIndex() == 1);
    SimpleDateFormat sdf2 = new SimpleDateFormat("MMMM");
    ParsePosition pos2 = new ParsePosition(0);
    /* Date d2 = */
    sdf2.parse("????", pos2);
    logln("sdf2: " + pos2.getErrorIndex() + "/" + pos2.getIndex());
    assertTrue("Fail: failed to detect bad parse", pos2.getErrorIndex() == 0);
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 87 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class EthiopicTest method TestEraStart.

// basic check to see that we print out eras ok
// eventually should modify to use locale strings and formatter appropriate to coptic calendar
@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();
    // act like proleptic Gregorian
    gc.setGregorianChange(new Date(Long.MIN_VALUE));
    gc.setTime(cal.getTime());
    fmt.setCalendar(new GregorianCalendar());
    assertEquals("Gregorian Date", "Tue Aug 28, 0007 AD", fmt.format(gc));
}
Also used : EthiopicCalendar(android.icu.util.EthiopicCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 88 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class IndianTest method TestYearEdge.

/**
 * Problem reported by Bruno Haible <bruno.haible@de.ibm.com>
 *  -- see ticket 8419 -- http://bugs.icu-project.org/trac/ticket/8419
 * Problem with months out of range 0-11
 */
@Test
public void TestYearEdge() {
    // Display dates in ISO 8601 format.
    DateFormat fmt = new SimpleDateFormat("YYYY-MM-dd", ULocale.US);
    // Instantiate an Indian calendar.
    ULocale locale = ULocale.US.setKeywordValue("calendar", "indian");
    Calendar cal = Calendar.getInstance(locale);
    // Try add() repeatedly.
    cal.setTimeInMillis(1295568000000L);
    if (!fmt.format(cal.getTime()).equals("2011-01-20")) {
        errln("Incorrect calendar value for year edge test");
    }
    cal.add(Calendar.MONTH, 1);
    if (!fmt.format(cal.getTime()).equals("2011-02-19")) {
        errln("Incorrect calendar value for year edge test");
    }
    cal.add(Calendar.MONTH, 1);
    if (!fmt.format(cal.getTime()).equals("2011-03-21")) {
        errln("Incorrect calendar value for year edge test");
    }
    cal.add(Calendar.MONTH, 1);
    if (!fmt.format(cal.getTime()).equals("2011-04-20")) {
        errln("Incorrect calendar value for year edge test");
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) IndianCalendar(android.icu.util.IndianCalendar) Calendar(android.icu.util.Calendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Test(org.junit.Test)

Example 89 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class CalendarRegressionTest method TestT9452.

/**
 * Test case for ticket 9452
 * Calendar addition fall onto the missing date - 2011-12-30 in Samoa
 */
@Test
public void TestT9452() {
    TimeZone samoaTZ = TimeZone.getTimeZone("Pacific/Apia");
    GregorianCalendar cal = new GregorianCalendar(samoaTZ);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
    sdf.setTimeZone(samoaTZ);
    // Set date to 2011-12-29 00:00
    cal.clear();
    cal.set(2011, Calendar.DECEMBER, 29, 0, 0, 0);
    Date d = cal.getTime();
    String dstr = sdf.format(d);
    logln("Initial date: " + dstr);
    // Add 1 day
    cal.add(Calendar.DATE, 1);
    d = cal.getTime();
    dstr = sdf.format(d);
    logln("+1 day: " + dstr);
    assertEquals("Add 1 day", "2011-12-31T00:00:00+14:00", dstr);
    // Subtract 1 day
    cal.add(Calendar.DATE, -1);
    d = cal.getTime();
    dstr = sdf.format(d);
    logln("-1 day: " + dstr);
    assertEquals("Subtract 1 day", "2011-12-29T00:00:00-10:00", dstr);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 90 with SimpleDateFormat

use of android.icu.text.SimpleDateFormat in project j2objc by google.

the class IslamicTest method TestIslamicTabularDates.

@Test
public void TestIslamicTabularDates() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    Date date = null;
    try {
        date = formatter.parse("1975-05-06");
    } catch (Exception e) {
        errln("unable to parse test date string - errMsg:" + e.getLocalizedMessage());
    }
    IslamicCalendar is_cal = new IslamicCalendar();
    is_cal.setCalculationType(CalculationType.ISLAMIC_CIVIL);
    is_cal.setTime(date);
    IslamicCalendar is_cal2 = new IslamicCalendar();
    is_cal2.setCalculationType(CalculationType.ISLAMIC_TBLA);
    is_cal2.setTime(date);
    int is_month = is_cal.get(Calendar.MONTH);
    int is_month2 = is_cal2.get(Calendar.MONTH);
    int is_year = is_cal.get(Calendar.YEAR);
    int is_year2 = is_cal2.get(Calendar.YEAR);
    if ((is_month != is_month2) || (is_year != is_year2))
        errln("unexpected difference between islamic and tbla month " + is_month + " : " + is_month2 + " and/or year " + is_year + " : " + is_year2);
    int is_day = is_cal.get(Calendar.DAY_OF_MONTH);
    int is_day2 = is_cal2.get(Calendar.DAY_OF_MONTH);
    if (is_day2 - is_day != 1)
        errln("unexpected difference between civil and tbla: " + is_day2 + " : " + is_day);
}
Also used : SimpleDateFormat(android.icu.text.SimpleDateFormat) Date(java.util.Date) IOException(java.io.IOException) IslamicCalendar(android.icu.util.IslamicCalendar) Test(org.junit.Test)

Aggregations

SimpleDateFormat (android.icu.text.SimpleDateFormat)153 Test (org.junit.Test)113 Date (java.util.Date)103 GregorianCalendar (android.icu.util.GregorianCalendar)59 Calendar (android.icu.util.Calendar)53 ParseException (java.text.ParseException)42 DateFormat (android.icu.text.DateFormat)41 JapaneseCalendar (android.icu.util.JapaneseCalendar)41 ULocale (android.icu.util.ULocale)40 IslamicCalendar (android.icu.util.IslamicCalendar)37 ParsePosition (java.text.ParsePosition)36 FieldPosition (java.text.FieldPosition)29 ChineseCalendar (android.icu.util.ChineseCalendar)27 TimeZone (android.icu.util.TimeZone)27 BuddhistCalendar (android.icu.util.BuddhistCalendar)25 ChineseDateFormat (android.icu.text.ChineseDateFormat)21 HebrewCalendar (android.icu.util.HebrewCalendar)21 IOException (java.io.IOException)19 SimpleTimeZone (android.icu.util.SimpleTimeZone)16 Locale (java.util.Locale)14