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

The following examples show how to use android.icu.util.Calendar#setTimeInMillis() . 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: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean getBoundsForDate(long date, Rect outBounds) {
    Calendar calendarDate = Calendar.getInstance();
    calendarDate.setTimeInMillis(date);
    int listViewEntryCount = mListView.getCount();
    for (int i = 0; i < listViewEntryCount; i++) {
        WeekView currWeekView = (WeekView) mListView.getChildAt(i);
        if (currWeekView.getBoundsForDate(calendarDate, outBounds)) {
            // Found the date in this week. Now need to offset vertically to return correct
            // bounds in the coordinate system of the entire layout
            final int[] weekViewPositionOnScreen = new int[2];
            final int[] delegatorPositionOnScreen = new int[2];
            currWeekView.getLocationOnScreen(weekViewPositionOnScreen);
            mDelegator.getLocationOnScreen(delegatorPositionOnScreen);
            final int extraVerticalOffset =
                    weekViewPositionOnScreen[1] - delegatorPositionOnScreen[1];
            outBounds.top += extraVerticalOffset;
            outBounds.bottom += extraVerticalOffset;
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: TimePicker.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public final void autofill(AutofillValue value) {
    if (value == null || !value.isDate()) {
        Log.w(LOG_TAG, value + " could not be autofilled into " + this);
        return;
    }

    final long time = value.getDateValue();

    final Calendar cal = Calendar.getInstance(mLocale);
    cal.setTimeInMillis(time);
    setDate(cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE));

    // Must set mAutofilledValue *after* calling subclass method to make sure the value
    // returned by getAutofillValue() matches it.
    mAutofilledValue = time;
}
 
Example 3
Source File: DatePicker.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public final void autofill(AutofillValue value) {
    if (value == null || !value.isDate()) {
        Log.w(LOG_TAG, value + " could not be autofilled into " + this);
        return;
    }

    final long time = value.getDateValue();

    final Calendar cal = Calendar.getInstance(mCurrentLocale);
    cal.setTimeInMillis(time);
    updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
            cal.get(Calendar.DAY_OF_MONTH));

    // Must set mAutofilledValue *after* calling subclass method to make sure the value
    // returned by getAutofillValue() matches it.
    mAutofilledValue = time;
}
 
Example 4
Source File: CalendarRegressionTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Test
public void TestYearJump3279() {
    final long time = 1041148800000L;
    Calendar c = new GregorianCalendar();
    DateFormat fmt = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.US);

    c.setTimeInMillis(time);
    int year1 = c.get(Calendar.YEAR);
    
    logln("time: " + fmt.format(new Date(c.getTimeInMillis())));

    logln("setting DOW to " + c.getFirstDayOfWeek());
    c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek());
    logln("week: " + c.getTime());
    logln("week adjust: " + fmt.format(new Date(c.getTimeInMillis())));
    int year2 = c.get(Calendar.YEAR);
    
    if(year1 != year2) {
        errln("Error: adjusted day of week, and year jumped from " + year1 + " to " + year2);
    } else {
        logln("Year remained " + year2 + " - PASS.");
    }
}
 
Example 5
Source File: DangiTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Make sure no Gregorian dates map to Chinese 1-based day of
 * month zero.  This was a problem with some of the astronomical
 * new moon determinations.
 */
@Test
public void TestZeroDOM() {
    Calendar cal = Calendar.getInstance(new ULocale("ko_KR@calendar=dangi"));
    GregorianCalendar greg = new GregorianCalendar(1989, Calendar.SEPTEMBER, 1);
    logln("Start: " + greg.getTime());
    for (int i=0; i<1000; ++i) {
        cal.setTimeInMillis(greg.getTimeInMillis());
        if (cal.get(Calendar.DAY_OF_MONTH) == 0) {
            errln("Fail: " + greg.getTime() + " -> " +
                  cal.get(Calendar.YEAR) + "/" +
                  cal.get(Calendar.MONTH) +
                  (cal.get(Calendar.IS_LEAP_MONTH)==1?"(leap)":"") +
                  "/" + cal.get(Calendar.DAY_OF_MONTH));
        }
        greg.add(Calendar.DAY_OF_YEAR, 1);
    }
    logln("End: " + greg.getTime());
}
 
Example 6
Source File: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale The locale.
 */
private static Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}
 
Example 7
Source File: CalendarViewLegacyDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the day that the given x position is in, accounting for
 * week number.
 *
 * @param x The x position of the touch event.
 * @return True if a day was found for the given location.
 */
public boolean getDayFromLocation(float x, Calendar outCalendar) {
    final boolean isLayoutRtl = isLayoutRtl();

    int start;
    int end;

    if (isLayoutRtl) {
        start = 0;
        end = mShowWeekNumber ? mWidth - mWidth / mNumCells : mWidth;
    } else {
        start = mShowWeekNumber ? mWidth / mNumCells : 0;
        end = mWidth;
    }

    if (x < start || x > end) {
        outCalendar.clear();
        return false;
    }

    // Selection is (x - start) / (pixels/day) which is (x - start) * day / pixels
    int dayPosition = (int) ((x - start) * mDaysPerWeek / (end - start));

    if (isLayoutRtl) {
        dayPosition = mDaysPerWeek - 1 - dayPosition;
    }

    outCalendar.setTimeInMillis(mFirstDay.getTimeInMillis());
    outCalendar.add(Calendar.DAY_OF_MONTH, dayPosition);

    return true;
}
 
Example 8
Source File: TwilightService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the twilight state for a specific location and time.
 *
 * @param location the location to use
 * @param timeMillis the reference time to use
 * @return the calculated {@link TwilightState}, or {@code null} if location is {@code null}
 */
private static TwilightState calculateTwilightState(Location location, long timeMillis) {
    if (location == null) {
        return null;
    }

    final CalendarAstronomer ca = new CalendarAstronomer(
            location.getLongitude(), location.getLatitude());

    final Calendar noon = Calendar.getInstance();
    noon.setTimeInMillis(timeMillis);
    noon.set(Calendar.HOUR_OF_DAY, 12);
    noon.set(Calendar.MINUTE, 0);
    noon.set(Calendar.SECOND, 0);
    noon.set(Calendar.MILLISECOND, 0);
    ca.setTime(noon.getTimeInMillis());

    long sunriseTimeMillis = ca.getSunRiseSet(true /* rise */);
    long sunsetTimeMillis = ca.getSunRiseSet(false /* rise */);

    if (sunsetTimeMillis < timeMillis) {
        noon.add(Calendar.DATE, 1);
        ca.setTime(noon.getTimeInMillis());
        sunriseTimeMillis = ca.getSunRiseSet(true /* rise */);
    } else if (sunriseTimeMillis > timeMillis) {
        noon.add(Calendar.DATE, -1);
        ca.setTime(noon.getTimeInMillis());
        sunsetTimeMillis = ca.getSunRiseSet(false /* rise */);
    }

    return new TwilightState(sunriseTimeMillis, sunsetTimeMillis);
}
 
Example 9
Source File: DatePickerSpinnerDelegate.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale The locale.
 */
private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}
 
Example 10
Source File: TimePicker.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
@Override
public void setDate(long date) {
    Calendar cal = Calendar.getInstance(mLocale);
    cal.setTimeInMillis(date);
    setHour(cal.get(Calendar.HOUR_OF_DAY));
    setMinute(cal.get(Calendar.MINUTE));
}
 
Example 11
Source File: DatePickerSpinnerDelegate.java    From DateTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a calendar for locale bootstrapped with the value of a given calendar.
 *
 * @param oldCalendar The old calendar.
 * @param locale      The locale.
 */
private Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
    if (oldCalendar == null) {
        return Calendar.getInstance(locale);
    } else {
        final long currentTimeMillis = oldCalendar.getTimeInMillis();
        Calendar newCalendar = Calendar.getInstance(locale);
        newCalendar.setTimeInMillis(currentTimeMillis);
        return newCalendar;
    }
}
 
Example 12
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 13
Source File: IndianTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Problem reported by Bruno Haible <[email protected]>
 *  -- 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");
    }
}
 
Example 14
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void stress(String[] data, int data_length, Locale loc, String locName) {
    String[] skeleton = {
            "EEEEdMMMMy",
            "dMMMMy",
            "dMMMM",
            "MMMMy",
            "EEEEdMMMM",
            "EEEdMMMy",
            "dMMMy",
            "dMMM",
            "MMMy",
            "EEEdMMM",
            "EEEdMy",
            "dMy",
            "dM",
            "My",
            "EEEdM",
            "d",
            "EEEd",
            "y",
            "M",
            "MMM",
            "MMMM",
            "hm",
            "hmv",
            "hmz",
            "h",
            "hv",
            "hz",
            "EEddMMyyyy", // following could be normalized
            "EddMMy", 
            "hhmm",
            "hhmmzz",
            "hms",  // following could not be normalized
            "dMMMMMy",
            "EEEEEdM",
    };

    int i = 0;

    SimpleDateFormat ref = new SimpleDateFormat(data[i++], loc);

    while (i<data_length) {
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date;
        Date date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch ( ParseException e ) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), date_2.getTime());

        for ( int skeletonIndex = 0; 
                skeletonIndex < skeleton.length; 
                ++skeletonIndex ) {
            String oneSkeleton = skeleton[skeletonIndex];
            // need special handle of "Thai" since the default calendar
            // of "Thai" is "Budd", not "Gregorian".
            DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance(oneSkeleton, loc);
            /*
            if ( locName.equals("th") ) {
                // reset calendar to be Gregorian
                GregorianCalendar gregCal = new GregorianCalendar(loc);
                DateFormat dformat = dtitvfmt.getDateFormat();
                DateFormat newOne = (DateFormat)dformat.clone();
                newOne.setCalendar(gregCal);
                dtitvfmt.setDateFormat(newOne);
            }
             */
            dtitvfmt.format(dtitv);
        }


        // test interval format by algorithm 
        for ( int style = DateFormat.FULL; style  < 4; ++style ) {
            SimpleDateFormat dtfmt = (SimpleDateFormat) DateFormat.getDateInstance(style, loc);
            FieldPosition pos = new FieldPosition(0);
            StringBuffer str = new StringBuffer("");
            Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
            Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
            fromCalendar.setTimeInMillis(dtitv.getFromDate());
            toCalendar.setTimeInMillis(dtitv.getToDate());
            dtfmt.intervalFormatByAlgorithm(fromCalendar, toCalendar, str, pos);
        } 
    } 
}
 
Example 15
Source File: DateIntervalFormatTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
private void expectUserCLDR(String[] data, int data_length) {
    int i = 1;
    while (i<data_length) {
        String locName = data[i++];
        ULocale loc = new ULocale(locName);
        SimpleDateFormat ref = new SimpleDateFormat(data[0], loc);
        // 'f'
        String datestr = data[i++];
        String datestr_2 = data[i++];
        Date date, date_2;
        try {
            date = ref.parse(datestr);
            date_2 = ref.parse(datestr_2);
        } catch ( ParseException e ) {
            errln("parse exception" + e);
            continue;
        }
        DateInterval dtitv = new DateInterval(date.getTime(), 
                date_2.getTime());

        DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yyyyMMMdd", loc);
        //DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("yMd");
        //SimpleDateFormat dtfmt = new SimpleDateFormat("yyyy 'year' MMM 'month' dd 'day'", loc);
        //dtitvfmt.setDateFormat(dtfmt);
        DateIntervalInfo dtitvinf = new DateIntervalInfo();
        dtitvinf.setFallbackIntervalPattern("{0} --- {1}");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.YEAR, "'all diff'");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.MONTH, "yyyy 'diff' MMM d - MMM y");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.DATE, "yyyy MMM d ~ d");
        dtitvinf.setIntervalPattern("yMMMd", Calendar.HOUR_OF_DAY, "yyyy MMMd HH:mm ~ HH:mm");
        dtitvfmt.setDateIntervalInfo(dtitvinf);
        FieldPosition pos = new FieldPosition(0);
        StringBuffer str = new StringBuffer("");
        DateFormat dtfmt = dtitvfmt.getDateFormat();
        Calendar fromCalendar = (Calendar) dtfmt.getCalendar().clone();
        Calendar toCalendar = (Calendar) dtfmt.getCalendar().clone();
        fromCalendar.setTimeInMillis(dtitv.getFromDate());
        toCalendar.setTimeInMillis(dtitv.getToDate());
        dtitvfmt.format(fromCalendar, toCalendar, str, pos);

        String expected = data[i++];
        String formatted = dtitvfmt.format(dtitv).toString();
        if ( !formatted.equals(Utility.unescape(expected)) )  {
            errln("CLDR: \"" + locName + "\\" + datestr + "\\" + datestr_2 + "\"\t expected: " + expected +"\tgot: " + formatted + "\n");
        }
    }
}
 
Example 16
Source File: IBMCalendarTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test
public void TestRepeatedWallTime() {
    final Object[][] TESTDATA = {
        // Time zone            Input wall time                     WALLTIME_LAST in GMT                WALLTIME_FIRST in GMT
        {"America/New_York",    new CalFields(2011,11,6,0,59,59),   new CalFields(2011,11,6,4,59,59),   new CalFields(2011,11,6,4,59,59)},
        {"America/New_York",    new CalFields(2011,11,6,1,0,0),     new CalFields(2011,11,6,6,0,0),     new CalFields(2011,11,6,5,0,0)},
        {"America/New_York",    new CalFields(2011,11,6,1,0,1),     new CalFields(2011,11,6,6,0,1),     new CalFields(2011,11,6,5,0,1)},
        {"America/New_York",    new CalFields(2011,11,6,1,30,0),    new CalFields(2011,11,6,6,30,0),    new CalFields(2011,11,6,5,30,0)},
        {"America/New_York",    new CalFields(2011,11,6,1,59,59),   new CalFields(2011,11,6,6,59,59),   new CalFields(2011,11,6,5,59,59)},
        {"America/New_York",    new CalFields(2011,11,6,2,0,0),     new CalFields(2011,11,6,7,0,0),     new CalFields(2011,11,6,7,0,0)},
        {"America/New_York",    new CalFields(2011,11,6,2,0,1),     new CalFields(2011,11,6,7,0,1),     new CalFields(2011,11,6,7,0,1)},

        {"Australia/Lord_Howe", new CalFields(2011,4,3,1,29,59),    new CalFields(2011,4,2,14,29,59),   new CalFields(2011,4,2,14,29,59)},
        {"Australia/Lord_Howe", new CalFields(2011,4,3,1,30,0),     new CalFields(2011,4,2,15,0,0),     new CalFields(2011,4,2,14,30,0)},
        {"Australia/Lord_Howe", new CalFields(2011,4,3,1,45,0),     new CalFields(2011,4,2,15,15,0),    new CalFields(2011,4,2,14,45,0)},
        {"Australia/Lord_Howe", new CalFields(2011,4,3,1,59,59),    new CalFields(2011,4,2,15,29,59),   new CalFields(2011,4,2,14,59,59)},
        {"Australia/Lord_Howe", new CalFields(2011,4,3,2,0,0),      new CalFields(2011,4,2,15,30,0),    new CalFields(2011,4,2,15,30,0)},
        {"Australia/Lord_Howe", new CalFields(2011,4,3,2,0,1),      new CalFields(2011,4,2,15,30,1),    new CalFields(2011,4,2,15,30,1)},
    };

    Calendar calGMT = Calendar.getInstance(TimeZone.GMT_ZONE);

    Calendar calDefault = Calendar.getInstance();
    Calendar calLast = Calendar.getInstance();
    Calendar calFirst = Calendar.getInstance();

    calFirst.setRepeatedWallTimeOption(Calendar.WALLTIME_FIRST);
    calLast.setRepeatedWallTimeOption(Calendar.WALLTIME_LAST);

    for (Object[] test : TESTDATA) {
        String tzid = (String)test[0];
        TimeZone tz = TimeZone.getTimeZone(tzid);
        CalFields in = (CalFields)test[1];
        CalFields expLastGMT = (CalFields)test[2];
        CalFields expFirstGMT = (CalFields)test[3];

        // WALLTIME_LAST
        calLast.setTimeZone(tz);
        in.setTo(calLast);
        calGMT.setTimeInMillis(calLast.getTimeInMillis());
        CalFields outLastGMT = CalFields.createFrom(calGMT);
        if (!outLastGMT.equals(expLastGMT)) {
            errln("Fail: WALLTIME_LAST " + in + "[" + tzid + "] is parsed as " + outLastGMT + "[GMT]. Expected: " + expLastGMT + "[GMT]");
        }

        // default
        calDefault.setTimeZone(tz);
        in.setTo(calDefault);
        calGMT.setTimeInMillis(calDefault.getTimeInMillis());
        CalFields outDefGMT = CalFields.createFrom(calGMT);
        if (!outDefGMT.equals(expLastGMT)) {
            errln("Fail: (default) " + in + "[" + tzid + "] is parsed as " + outDefGMT + "[GMT]. Expected: " + expLastGMT + "[GMT]");
        }

        // WALLTIME_FIRST
        calFirst.setTimeZone(tz);
        in.setTo(calFirst);
        calGMT.setTimeInMillis(calFirst.getTimeInMillis());
        CalFields outFirstGMT = CalFields.createFrom(calGMT);
        if (!outFirstGMT.equals(expFirstGMT)) {
            errln("Fail: WALLTIME_FIRST " + in + "[" + tzid + "] is parsed as " + outFirstGMT + "[GMT]. Expected: " + expFirstGMT + "[GMT]");
        }
    }
}
 
Example 17
Source File: DatePickerSpinnerDelegate.java    From DateTimePicker with Apache License 2.0 4 votes vote down vote up
@Override
public Calendar getMaxDate() {
    final Calendar maxDate = Calendar.getInstance();
    maxDate.setTimeInMillis(mCalendarView.getMaxDate());
    return maxDate;
}
 
Example 18
Source File: DatePickerSpinnerDelegate.java    From DateTimePicker with Apache License 2.0 4 votes vote down vote up
@Override
public Calendar getMinDate() {
    final Calendar minDate = Calendar.getInstance();
    minDate.setTimeInMillis(mCalendarView.getMinDate());
    return minDate;
}
 
Example 19
Source File: DatePickerSpinnerDelegate.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public Calendar getMaxDate() {
    final Calendar maxDate = Calendar.getInstance();
    maxDate.setTimeInMillis(mCalendarView.getMaxDate());
    return maxDate;
}
 
Example 20
Source File: DatePickerSpinnerDelegate.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
@Override
public Calendar getMinDate() {
    final Calendar minDate = Calendar.getInstance();
    minDate.setTimeInMillis(mCalendarView.getMinDate());
    return minDate;
}