Search in sources :

Example 11 with DateFormat

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

the class AstroTest method TestSunriseTimes.

@Test
public void TestSunriseTimes() {
    // logln("Sunrise/Sunset times for San Jose, California, USA");
    // CalendarAstronomer astro = new CalendarAstronomer(-121.55, 37.20);
    // TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
    // We'll use a table generated by the UNSO website as our reference
    // From: http://aa.usno.navy.mil/
    // -Location: W079 25, N43 40
    // -Rise and Set for the Sun for 2001
    // -Zone:  4h West of Greenwich
    int[] USNO = { 6, 59, 19, 45, 6, 57, 19, 46, 6, 56, 19, 47, 6, 54, 19, 48, 6, 52, 19, 49, 6, 50, 19, 51, 6, 48, 19, 52, 6, 47, 19, 53, 6, 45, 19, 54, 6, 43, 19, 55, 6, 42, 19, 57, 6, 40, 19, 58, 6, 38, 19, 59, 6, 36, 20, 0, 6, 35, 20, 1, 6, 33, 20, 3, 6, 31, 20, 4, 6, 30, 20, 5, 6, 28, 20, 6, 6, 27, 20, 7, 6, 25, 20, 8, 6, 23, 20, 10, 6, 22, 20, 11, 6, 20, 20, 12, 6, 19, 20, 13, 6, 17, 20, 14, 6, 16, 20, 16, 6, 14, 20, 17, 6, 13, 20, 18, 6, 11, 20, 19 };
    logln("Sunrise/Sunset times for Toronto, Canada");
    CalendarAstronomer astro = new CalendarAstronomer(-(79 + 25 / 60), 43 + 40 / 60);
    // As of ICU4J 2.8 the ICU4J time zones implement pass-through
    // to the underlying JDK.  Because of variation in the
    // underlying JDKs, we have to use a fixed-offset
    // SimpleTimeZone to get consistent behavior between JDKs.
    // The offset we want is [-18000000, 3600000] (raw, dst).
    // [aliu 10/15/03]
    // TimeZone tz = TimeZone.getTimeZone("America/Montreal");
    TimeZone tz = new SimpleTimeZone(-18000000 + 3600000, "Montreal(FIXED)");
    GregorianCalendar cal = new GregorianCalendar(tz, Locale.US);
    GregorianCalendar cal2 = new GregorianCalendar(tz, Locale.US);
    cal.clear();
    cal.set(Calendar.YEAR, 2001);
    cal.set(Calendar.MONTH, Calendar.APRIL);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    // must be near local noon for getSunRiseSet to work
    cal.set(Calendar.HOUR_OF_DAY, 12);
    DateFormat df = DateFormat.getTimeInstance(cal, DateFormat.MEDIUM, Locale.US);
    DateFormat df2 = DateFormat.getDateTimeInstance(cal, DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US);
    DateFormat day = DateFormat.getDateInstance(cal, DateFormat.MEDIUM, Locale.US);
    for (int i = 0; i < 30; i++) {
        astro.setDate(cal.getTime());
        Date sunrise = new Date(astro.getSunRiseSet(true));
        Date sunset = new Date(astro.getSunRiseSet(false));
        cal2.setTime(cal.getTime());
        cal2.set(Calendar.SECOND, 0);
        cal2.set(Calendar.MILLISECOND, 0);
        cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 0]);
        cal2.set(Calendar.MINUTE, USNO[4 * i + 1]);
        Date exprise = cal2.getTime();
        cal2.set(Calendar.HOUR_OF_DAY, USNO[4 * i + 2]);
        cal2.set(Calendar.MINUTE, USNO[4 * i + 3]);
        Date expset = cal2.getTime();
        // Compute delta of what we got to the USNO data, in seconds
        int deltarise = Math.abs((int) (sunrise.getTime() - exprise.getTime()) / 1000);
        int deltaset = Math.abs((int) (sunset.getTime() - expset.getTime()) / 1000);
        // Allow a deviation of 0..MAX_DEV seconds
        // It would be nice to get down to 60 seconds, but at this
        // point that appears to be impossible without a redo of the
        // algorithm using something more advanced than Duffett-Smith.
        final int MAX_DEV = 180;
        if (deltarise > MAX_DEV || deltaset > MAX_DEV) {
            if (deltarise > MAX_DEV) {
                errln("FAIL: " + day.format(cal.getTime()) + ", Sunrise: " + df2.format(sunrise) + " (USNO " + df.format(exprise) + " d=" + deltarise + "s)");
            } else {
                logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")");
            }
            if (deltaset > MAX_DEV) {
                errln("FAIL: " + day.format(cal.getTime()) + ", Sunset: " + df2.format(sunset) + " (USNO " + df.format(expset) + " d=" + deltaset + "s)");
            } else {
                logln(day.format(cal.getTime()) + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
            }
        } else {
            logln(day.format(cal.getTime()) + ", Sunrise: " + df.format(sunrise) + " (USNO " + df.format(exprise) + ")" + ", Sunset: " + df.format(sunset) + " (USNO " + df.format(expset) + ")");
        }
        cal.add(Calendar.DATE, 1);
    }
// CalendarAstronomer a = new CalendarAstronomer(-(71+5/60), 42+37/60);
// cal.clear();
// cal.set(cal.YEAR, 1986);
// cal.set(cal.MONTH, cal.MARCH);
// cal.set(cal.DATE, 10);
// cal.set(cal.YEAR, 1988);
// cal.set(cal.MONTH, cal.JULY);
// cal.set(cal.DATE, 27);
// a.setDate(cal.getTime());
// long r = a.getSunRiseSet2(true);
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) SimpleTimeZone(android.icu.util.SimpleTimeZone) DateFormat(android.icu.text.DateFormat) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Date(java.util.Date) Test(org.junit.Test)

Example 12 with DateFormat

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

the class ChineseTest method TestResolution.

/**
 * Make sure IS_LEAP_MONTH participates in field resolution.
 */
@Test
public void TestResolution() {
    ChineseCalendar cal = new ChineseCalendar();
    DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT);
    // May 22 2001 = y4638 m4 d30 doy119
    // May 23 2001 = y4638 m4* d1 doy120
    final int THE_YEAR = 4638;
    final int END = -1;
    int[] DATA = { // If we set DAY_OF_YEAR only, that should be used
    Calendar.DAY_OF_YEAR, 1, END, // Expect 1-1
    1, // Expect 1-1
    0, // Expect 1-1
    1, // If we set MONTH only, that should be used
    Calendar.IS_LEAP_MONTH, 1, Calendar.DAY_OF_MONTH, 1, Calendar.MONTH, 3, END, // Expect 4*-1
    4, // Expect 4*-1
    1, // Expect 4*-1
    1, // Should ignore
    Calendar.MONTH, // Should ignore
    1, // Should ignore
    Calendar.IS_LEAP_MONTH, // Should ignore
    1, // Should ignore
    Calendar.DAY_OF_MONTH, // Should ignore
    1, Calendar.DAY_OF_YEAR, 121, END, // Expect 4*-2
    4, // Expect 4*-2
    1, // Expect 4*-2
    2, // If we set IS_LEAP_MONTH last, that should take precedence
    Calendar.MONTH, 3, Calendar.DAY_OF_MONTH, 1, // Should ignore
    Calendar.DAY_OF_YEAR, // Should ignore
    5, Calendar.IS_LEAP_MONTH, 1, END, // Expect 4*-1
    4, // Expect 4*-1
    1, // Expect 4*-1
    1 };
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < DATA.length; ) {
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
        buf.setLength(0);
        buf.append("EXTENDED_YEAR=" + THE_YEAR);
        while (DATA[i] != END) {
            cal.set(DATA[i++], DATA[i++]);
            buf.append(" " + fieldName(DATA[i - 2]) + "=" + DATA[i - 1]);
        }
        // Skip over END mark
        ++i;
        int expMonth = DATA[i++] - 1;
        int expIsLeapMonth = DATA[i++];
        int expDOM = DATA[i++];
        int month = cal.get(Calendar.MONTH);
        int isLeapMonth = cal.get(Calendar.IS_LEAP_MONTH);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        if (expMonth == month && expIsLeapMonth == isLeapMonth && dom == expDOM) {
            logln("OK: " + buf + " => " + fmt.format(cal.getTime()));
        } else {
            String s = fmt.format(cal.getTime());
            cal.clear();
            cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
            cal.set(Calendar.MONTH, expMonth);
            cal.set(Calendar.IS_LEAP_MONTH, expIsLeapMonth);
            cal.set(Calendar.DAY_OF_MONTH, expDOM);
            errln("Fail: " + buf + " => " + s + "=" + (month + 1) + "," + isLeapMonth + "," + dom + ", expected " + fmt.format(cal.getTime()) + "=" + (expMonth + 1) + "," + expIsLeapMonth + "," + expDOM);
        }
    }
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ChineseDateFormat(android.icu.text.ChineseDateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Example 13 with DateFormat

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

the class JapaneseTest method Test3860.

@Test
public void Test3860() {
    ULocale loc = new ULocale("ja_JP@calendar=japanese");
    Calendar cal = new JapaneseCalendar(loc);
    DateFormat enjformat = cal.getDateTimeFormat(0, 0, new ULocale("en_JP@calendar=japanese"));
    DateFormat format = cal.getDateTimeFormat(0, 0, loc);
    // Note: just 'y' doesn't work here.
    ((SimpleDateFormat) format).applyPattern("y.M.d");
    ParsePosition pos = new ParsePosition(0);
    // after the start of heisei accession.  Jan 1, 1H wouldn't work  because it is actually showa 64
    Date aDate = format.parse("1.1.9", pos);
    String inEn = enjformat.format(aDate);
    cal.clear();
    cal.setTime(aDate);
    int gotYear = cal.get(Calendar.YEAR);
    int gotEra = cal.get(Calendar.ERA);
    int expectYear = 1;
    int expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != expectYear) || (gotEra != expectEra)) {
        errln("Expected year " + expectYear + ", era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    } else {
        logln("Got year " + gotYear + " and era " + gotEra + ", == " + inEn);
    }
    // Test parse with missing era (should default to current era, heisei)
    // Test parse with incomplete information
    logln("Testing parse w/ just year...");
    Calendar cal2 = new JapaneseCalendar(loc);
    SimpleDateFormat fmt = new SimpleDateFormat("y", loc);
    SimpleDateFormat fmt2 = new SimpleDateFormat("HH:mm:ss.S MMMM d, yyyy G", new ULocale("en_US@calendar=gregorian"));
    cal2.clear();
    String samplestr = "1";
    logln("Test Year: " + samplestr);
    try {
        aDate = fmt.parse(samplestr);
    } catch (ParseException pe) {
        errln("Error parsing " + samplestr);
    }
    ParsePosition pp = new ParsePosition(0);
    fmt.parse(samplestr, cal2, pp);
    logln("cal2 after 1 parse:");
    String str = fmt2.format(aDate);
    logln("as Gregorian Calendar: " + str);
    cal2.setTime(aDate);
    gotYear = cal2.get(Calendar.YEAR);
    gotEra = cal2.get(Calendar.ERA);
    expectYear = 1;
    expectEra = JapaneseCalendar.CURRENT_ERA;
    if ((gotYear != 1) || (gotEra != expectEra)) {
        errln("parse " + samplestr + " of 'y' as Japanese Calendar, expected year " + expectYear + " and era " + expectEra + ", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str + ")");
    } else {
        logln(" year: " + gotYear + ", era: " + gotEra);
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 14 with DateFormat

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

the class JapaneseTest method Test5345parse.

@Test
public void Test5345parse() {
    // Test parse with incomplete information
    // DateFormat.LONG, Locale.US);
    DateFormat fmt2 = DateFormat.getDateInstance();
    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"));
    // 1868-09-08 00:00 Pacific Time (GMT-07:52:58)
    long aDateLong = -3197117222000L;
    if (TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
        // Java time zone implementation does not support LMTs
        // 1868-09-08 00:00 Pacific Time (GMT-08:00)
        aDateLong = -3197116800000L;
    }
    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();
    }
}
Also used : ULocale(android.icu.util.ULocale) SimpleDateFormat(android.icu.text.SimpleDateFormat) DateFormat(android.icu.text.DateFormat) ParseException(java.text.ParseException) SimpleDateFormat(android.icu.text.SimpleDateFormat) JapaneseCalendar(android.icu.util.JapaneseCalendar) Date(java.util.Date) Test(org.junit.Test)

Example 15 with DateFormat

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

the class DangiTest method TestResolution.

/**
 * Make sure IS_LEAP_MONTH participates in field resolution.
 */
@Test
public void TestResolution() {
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    DateFormat fmt = DateFormat.getDateInstance(cal, DateFormat.DEFAULT);
    // May 22 4334 = y4334 m4 d30 doy119
    // May 23 4334 = y4334 m4* d1 doy120
    final int THE_YEAR = 4334;
    final int END = -1;
    int[] DATA = { // If we set DAY_OF_YEAR only, that should be used
    Calendar.DAY_OF_YEAR, 1, END, // Expect 1-1
    1, // Expect 1-1
    0, // Expect 1-1
    1, // If we set MONTH only, that should be used
    Calendar.IS_LEAP_MONTH, 1, Calendar.DAY_OF_MONTH, 1, Calendar.MONTH, 3, END, // Expect 4*-1
    4, // Expect 4*-1
    1, // Expect 4*-1
    1, // Should ignore
    Calendar.MONTH, // Should ignore
    1, // Should ignore
    Calendar.IS_LEAP_MONTH, // Should ignore
    1, // Should ignore
    Calendar.DAY_OF_MONTH, // Should ignore
    1, Calendar.DAY_OF_YEAR, 121, END, // Expect 4*-2
    4, // Expect 4*-2
    1, // Expect 4*-2
    2, // If we set IS_LEAP_MONTH last, that should take precedence
    Calendar.MONTH, 3, Calendar.DAY_OF_MONTH, 1, // Should ignore
    Calendar.DAY_OF_YEAR, // Should ignore
    5, Calendar.IS_LEAP_MONTH, 1, END, // Expect 4*-1
    4, // Expect 4*-1
    1, // Expect 4*-1
    1 };
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < DATA.length; ) {
        cal.clear();
        cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
        buf.setLength(0);
        buf.append("EXTENDED_YEAR=" + THE_YEAR);
        while (DATA[i] != END) {
            cal.set(DATA[i++], DATA[i++]);
            buf.append(" " + fieldName(DATA[i - 2]) + "=" + DATA[i - 1]);
        }
        // Skip over END mark
        ++i;
        int expMonth = DATA[i++] - 1;
        int expIsLeapMonth = DATA[i++];
        int expDOM = DATA[i++];
        int month = cal.get(Calendar.MONTH);
        int isLeapMonth = cal.get(Calendar.IS_LEAP_MONTH);
        int dom = cal.get(Calendar.DAY_OF_MONTH);
        if (expMonth == month && expIsLeapMonth == isLeapMonth && dom == expDOM) {
            logln("OK: " + buf + " => " + fmt.format(cal.getTime()));
        } else {
            String s = fmt.format(cal.getTime());
            cal.clear();
            cal.set(Calendar.EXTENDED_YEAR, THE_YEAR);
            cal.set(Calendar.MONTH, expMonth);
            cal.set(Calendar.IS_LEAP_MONTH, expIsLeapMonth);
            cal.set(Calendar.DAY_OF_MONTH, expDOM);
            errln("Fail: " + buf + " => " + s + "=" + (month + 1) + "," + isLeapMonth + "," + dom + ", expected " + fmt.format(cal.getTime()) + "=" + (expMonth + 1) + "," + expIsLeapMonth + "," + expDOM);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DangiCalendar(android.icu.util.DangiCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Calendar(android.icu.util.Calendar) DateFormat(android.icu.text.DateFormat) Test(org.junit.Test)

Aggregations

DateFormat (android.icu.text.DateFormat)97 SimpleDateFormat (android.icu.text.SimpleDateFormat)80 Test (org.junit.Test)78 Date (java.util.Date)67 GregorianCalendar (android.icu.util.GregorianCalendar)44 ChineseDateFormat (android.icu.text.ChineseDateFormat)37 Calendar (android.icu.util.Calendar)37 ULocale (android.icu.util.ULocale)33 JapaneseCalendar (android.icu.util.JapaneseCalendar)27 IslamicCalendar (android.icu.util.IslamicCalendar)23 ChineseCalendar (android.icu.util.ChineseCalendar)22 ParseException (java.text.ParseException)21 BuddhistCalendar (android.icu.util.BuddhistCalendar)19 TimeZone (android.icu.util.TimeZone)19 HebrewCalendar (android.icu.util.HebrewCalendar)17 Locale (java.util.Locale)17 ParsePosition (java.text.ParsePosition)15 FieldPosition (java.text.FieldPosition)13 IOException (java.io.IOException)10 SimpleTimeZone (android.icu.util.SimpleTimeZone)7