Search in sources :

Example 1 with DateUtil

use of com.codename1.util.DateUtil in project CodenameOne by codenameone.

the class SimpleDateFormat method format.

@Override
String format(Date source, StringBuilder toAppendTo) {
    if (pattern == null) {
        return super.format(source, toAppendTo);
    }
    // format based on local timezone
    Calendar calendar = Calendar.getInstance(TimeZone.getDefault());
    calendar.setTime(source);
    List<String> pattern = getPatternTokens();
    for (int i = 0; i < pattern.size(); i++) {
        String token = (String) pattern.get(i);
        char patternChar = token.charAt(0);
        token = token.substring(1);
        int len = token.length();
        int v = -1;
        switch(patternChar) {
            case LITERAL_LETTER:
                toAppendTo.append(token);
                break;
            case AMPM_LETTER:
                boolean am = calendar.get(Calendar.AM_PM) == Calendar.AM;
                String[] ampm = getDateFormatSymbols().getAmPmStrings();
                toAppendTo.append(am ? ampm[0] : ampm[1]);
                break;
            case ERA_LETTER:
                toAppendTo.append(getDateFormatSymbols().getEras()[calendar.get(ERA)]);
                break;
            case DAY_OF_WEEK_LETTER:
                v = calendar.get(Calendar.DAY_OF_WEEK) - 1;
                if (len > 3) {
                    toAppendTo.append(getDateFormatSymbols().getWeekdays()[v]);
                } else {
                    toAppendTo.append(getDateFormatSymbols().getShortWeekdays()[v]);
                }
                break;
            case TIMEZONE_LETTER:
                String[] names = getTimeZoneDisplayNames(calendar.getTimeZone().getID());
                if (names == null) {
                    toAppendTo.append(calendar.getTimeZone().getID());
                } else {
                    DateUtil du = new DateUtil(TimeZone.getTimeZone(names[DateFormatSymbols.ZONE_ID]));
                    toAppendTo.append(names[du.inDaylightTime(source) ? DateFormatSymbols.ZONE_SHORTNAME_DST : DateFormatSymbols.ZONE_SHORTNAME]);
                }
                break;
            case TIMEZONE822_LETTER:
                v = getOffsetInMinutes(calendar, calendar.getTimeZone());
                if (v < 0) {
                    toAppendTo.append(SIGN_NEGATIVE);
                    v = -v;
                } else {
                    toAppendTo.append(SIGN_POSITIVE);
                }
                toAppendTo.append(leftPad(v / 60, 2));
                toAppendTo.append(leftPad(v % 60, 2));
                break;
            case YEAR_LETTER:
                v = calendar.get(Calendar.YEAR);
                if (len == 2) {
                    v %= 100;
                }
                toAppendTo.append(leftPad(v, len));
                break;
            case MONTH_LETTER:
                v = calendar.get(Calendar.MONTH) - Calendar.JANUARY;
                if (len > 3) {
                    toAppendTo.append(L10NManager.getInstance().getLongMonthName(source));
                } else if (len == 3) {
                    toAppendTo.append(L10NManager.getInstance().getShortMonthName(source));
                } else {
                    toAppendTo.append(leftPad(v + 1, len));
                }
                break;
            case DAY_LETTER:
                v = calendar.get(Calendar.DAY_OF_MONTH);
                toAppendTo.append(leftPad(v, len));
                break;
            case HOUR_LETTER:
            case HOUR_1_LETTER:
            case HOUR12_LETTER:
            case HOUR12_1_LETTER:
                v = calendar.get(Calendar.HOUR_OF_DAY);
                if (patternChar == HOUR_1_LETTER && v == 0) {
                    v = 24;
                }
                if (patternChar == HOUR12_1_LETTER) {
                    v %= 12;
                    if (v == 0) {
                        v = 12;
                    }
                } else {
                    if (patternChar == HOUR12_LETTER) {
                        v %= 12;
                    }
                }
                toAppendTo.append(leftPad(v, len));
                break;
            case MINUTE_LETTER:
                v = calendar.get(Calendar.MINUTE);
                toAppendTo.append(leftPad(v, len));
                break;
            case SECOND_LETTER:
                v = calendar.get(Calendar.SECOND);
                toAppendTo.append(leftPad(v, len));
                break;
            case MILLISECOND_LETTER:
                v = calendar.get(Calendar.MILLISECOND);
                toAppendTo.append(leftPad(v, len));
                break;
            case WEEK_IN_YEAR_LETTER:
                v = calendar.get(WEEK_OF_YEAR);
                toAppendTo.append(leftPad(v, len));
                break;
            case WEEK_IN_MONTH_LETTER:
                v = calendar.get(WEEK_OF_MONTH);
                toAppendTo.append(leftPad(v, len));
                break;
            case DAY_IN_YEAR_LETTER:
                v = calendar.get(DAY_OF_YEAR);
                toAppendTo.append(leftPad(v, len));
                break;
            case DOW_IN_MONTH_LETTER:
                v = calendar.get(DAY_OF_WEEK_IN_MONTH);
                toAppendTo.append(leftPad(v, len));
                break;
        }
    }
    return toAppendTo.toString();
}
Also used : DateUtil(com.codename1.util.DateUtil) Calendar(java.util.Calendar)

Aggregations

DateUtil (com.codename1.util.DateUtil)1 Calendar (java.util.Calendar)1