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

The following examples show how to use android.icu.text.SimpleDateFormat#applyPattern() . 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: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4148168() {
        Date d = new Date(1002705212906L);
        String[] ISOPattern = {
            "''yyyy-MM-dd-hh.mm.ss.S''", "''yyyy-MM-dd-hh.mm.ss.SS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSS''", "''yyyy-MM-dd-hh.mm.ss.SSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSS''", "''yyyy-MM-dd-hh.mm.ss.SSSSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSSSS''", "''yyyy-MM-dd-hh.mm.ss.SSS000''"};
        SimpleDateFormat aSimpleDF = (SimpleDateFormat)DateFormat.getDateTimeInstance();

        for(int i = 0; i<ISOPattern.length; i++) {
            aSimpleDF.applyPattern( ISOPattern[i] );
            logln( "Pattern = " + aSimpleDF.toPattern());
            logln( "Format = " + aSimpleDF.format(d));
        }
}
 
Example 2
Source File: DateFormatRegressionTestJ.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void Test4253490() {
    Date d = new Date(1002705212231L);

    String[] ISOPattern = {
            "''yyyy-MM-dd-hh.mm.ss.S''", 
            "''yyyy-MM-dd-hh.mm.ss.SS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSSS''", 
            "''yyyy-MM-dd-hh.mm.ss.SSSSSSS''"}; 

    SimpleDateFormat aSimpleDF = (SimpleDateFormat) DateFormat.getDateTimeInstance();
    for (int i = 0; i < ISOPattern.length; i++) {
        aSimpleDF.applyPattern(ISOPattern[i]);
        logln("Pattern = " + aSimpleDF.toPattern());
        logln("Format = " + aSimpleDF.format(d));
    }
}
 
Example 3
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 4
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @bug 4104522
 * CANNOT REPRODUCE
 * According to the bug report, this test should throw a
 * StringIndexOutOfBoundsException during the second parse.  However,
 * this is not seen.
 */
@Test
public void Test4104522() {
    SimpleDateFormat sdf = new SimpleDateFormat();
    String pattern = "'time' hh:mm";
    sdf.applyPattern(pattern);
    logln("pattern: \"" + pattern + "\"");
    // works correctly
    ParsePosition pp = new ParsePosition(0);
    String text = "time ";
    Date dt = sdf.parse(text, pp);
    logln(" text: \"" + text + "\"" + " date: " + dt);
    // works wrong
    pp.setIndex(0);
    text = "time";
    dt = sdf.parse(text, pp);
    logln(" text: \"" + text + "\"" + " date: " + dt);    
}
 
Example 5
Source File: JapaneseTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
public void TestJapaneseYear3282() {
    Calendar c = Calendar.getInstance(ULocale.ENGLISH);
    c.set(2003,Calendar.SEPTEMBER,25);
    JapaneseCalendar jcal = new JapaneseCalendar();
    //jcal.setTime(new Date(1187906308151L));  alternate value
    jcal.setTime(c.getTime());
    logln("Now is: " + jcal.getTime());
    c.setTime(jcal.getTime());
    int nowYear = c.get(Calendar.YEAR);
    logln("Now year: "+nowYear);
    SimpleDateFormat jdf = (SimpleDateFormat) SimpleDateFormat.getDateInstance(jcal,
            SimpleDateFormat.DEFAULT, Locale.getDefault());
    jdf.applyPattern("G yy/MM/dd");
    String text = jdf.format(jcal.getTime());
    logln("Now is: " + text + " (in Japan)");
    try {
        Date date = jdf.parse(text);
        logln("But is this not the date?: " + date);
        c.setTime(date);
        int thenYear = c.get(Calendar.YEAR);
        logln("Then year: "+thenYear);
        if(thenYear != nowYear) {
            errln("Nowyear "+nowYear +" is not thenyear "+thenYear);
        } else {
            logln("Nowyear "+nowYear +" == thenyear "+thenYear);
        }
    } catch (java.text.ParseException ex) {
        ex.printStackTrace();
    }
}
 
Example 6
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 7
Source File: JapaneseTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void Test5345parse() {
    // Test parse with incomplete information
    DateFormat fmt2= DateFormat.getDateInstance(); //DateFormat.LONG, Locale.US);
    JapaneseCalendar c = new JapaneseCalendar(TimeZone.getDefault(), new ULocale("en_US"));
    SimpleDateFormat fmt = (SimpleDateFormat)c.getDateTimeFormat(1,1,new ULocale("en_US@calendar=japanese"));
    fmt.applyPattern("G y");
    logln("fmt's locale = " + fmt.getLocale(ULocale.ACTUAL_LOCALE));
    //SimpleDateFormat fmt = new SimpleDateFormat("G y", new Locale("en_US@calendar=japanese"));
    long aDateLong = -3197117222000L; // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
    if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
        // Java time zone implementation does not support LMTs
        aDateLong = -3197116800000L; // 1868-09-08 00:00 Pacific Time (GMT-08:00)
    }
    Date aDate = new Date(aDateLong);
    logln("aDate: " + aDate.toString() +", from " + aDateLong);
    String str;
    str = fmt2.format(aDate);
    logln("Test Date: " + str);
    str = fmt.format(aDate);
    logln("as Japanese Calendar: " + str);
    String expected = "Meiji 1";
    if(!str.equals(expected)) {
        errln("FAIL: Expected " + expected + " but got " + str);
    }
    Date otherDate;
    try {
        otherDate = fmt.parse(expected);
        if(!otherDate.equals(aDate)) { 
            String str3;
//            ParsePosition pp;
            Date dd = fmt.parse(expected);
            str3 = fmt.format(otherDate);
            long oLong = otherDate.getTime();
            long aLong = otherDate.getTime();
            
            errln("FAIL: Parse incorrect of " + expected + ":  wanted " + aDate + " ("+aLong+"), but got " +  " " +
                otherDate + " ("+oLong+") = " + str3 + " not " + dd.toString() );


        } else {
            logln("Parsed OK: " + expected);
        }
    } catch(java.text.ParseException pe) {
        errln("FAIL: ParseException: " + pe.toString());
        pe.printStackTrace();
    }
}
 
Example 8
Source File: DateFormatRegressionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void Test_GEec() {
    class PatternAndResult {
        private String pattern;
        private String result;
        PatternAndResult(String pat, String res) {
            pattern = pat;
            result = res;
        }
        public String getPattern()  { return pattern; }
        public String getResult()  { return result; }
    }
    final PatternAndResult[] tests = {
        new PatternAndResult( "dd MMM yyyy GGG",   "02 Jul 2008 AD" ),
        new PatternAndResult( "dd MMM yyyy GGGGG", "02 Jul 2008 A" ),
        new PatternAndResult( "e dd MMM yyyy",     "4 02 Jul 2008" ),
        new PatternAndResult( "ee dd MMM yyyy",    "04 02 Jul 2008" ),
        new PatternAndResult( "c dd MMM yyyy",     "4 02 Jul 2008" ),
        new PatternAndResult( "cc dd MMM yyyy",    "4 02 Jul 2008" ),
        new PatternAndResult( "eee dd MMM yyyy",   "Wed 02 Jul 2008" ),
        new PatternAndResult( "EEE dd MMM yyyy",   "Wed 02 Jul 2008" ),
        new PatternAndResult( "EE dd MMM yyyy",    "Wed 02 Jul 2008" ),
        new PatternAndResult( "eeee dd MMM yyyy",  "Wednesday 02 Jul 2008" ),
        new PatternAndResult( "eeeee dd MMM yyyy", "W 02 Jul 2008" ),
        new PatternAndResult( "e ww YYYY",         "4 27 2008" ),
        new PatternAndResult( "c ww YYYY",         "4 27 2008" ),
    };
    ULocale loc = ULocale.ENGLISH;
    TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    Calendar cal = new GregorianCalendar(tz, loc);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MMM-dd", loc);
    for ( int i = 0; i < tests.length; i++ ) {
        PatternAndResult item = tests[i];
        dateFormat.applyPattern( item.getPattern() );
        cal.set(2008, 6, 2, 5, 0); // 2008 July 02 5 AM PDT
        StringBuffer buf = new StringBuffer(32);
        FieldPosition fp = new FieldPosition(DateFormat.YEAR_FIELD);
        dateFormat.format(cal, buf, fp);
        if ( buf.toString().compareTo(item.getResult()) != 0 ) {
            errln("for pattern " + item.getPattern() + ", expected " + item.getResult() + ", got " + buf );
        }
        ParsePosition pos = new ParsePosition(0);
        dateFormat.parse( item.getResult(), cal, pos);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DATE);
        if ( year != 2008 || month != 6 || day != 2 ) {
            errln("use pattern " + item.getPattern() + " to parse " + item.getResult() +
                    ", expected y2008 m6 d2, got " + year + " " + month + " " + day );
        }
    }
}
 
Example 9
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 10
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());
          }
      }
}