Dynamic Dates with Java

I made a new change for build 48 of Fusion to show the date portion based on time. The difficulty is making it all work with all the different time zones and locales. 24 hours isn’t always a day because of daylight savings time. So I have to use Calendar. Here’s the static code:

public static String stripFieldFromPattern(SimpleDateFormat sdf, Date d,
        DateFormat.Field field) {
    StringBuilder b = new StringBuilder();
    boolean isLastCharValid = true;
    boolean isNextCharValid = false;
    AttributedCharacterIterator i = sdf.formatToCharacterIterator(d);
    for (char c = i.first(); c != AttributedCharacterIterator.DONE; c = i
            .next()) {

        Map<Attribute, Object> attributes = i.getAttributes();

        if (attributes.containsKey(field)) {
            isLastCharValid = false;
            continue;
        }

        char nextChar = i.next();
        isNextCharValid = (nextChar != AttributedCharacterIterator.DONE && i
                .getAttribute(field) == null);
        i.previous();

        if (!attributes.isEmpty() || c == ' ') {
            b.append(c);
        } else {
            if (isLastCharValid && isNextCharValid)
                b.append(c);
        }
        isLastCharValid = true;
    }
    return b.toString();
}

This will strip any field from a date pattern. I’d strip the Year field if an event occurred in the last year. Here’s the implementation as written in Fusion:

Calendar eventDateTime = Calendar.getInstance();
Calendar yesterday = Calendar.getInstance();
Calendar lastYear = Calendar.getInstance();

eventDateTime.setTimeInMillis(msgItem.getCompletionDateTime());
yesterday.add(Calendar.DATE, -1);
lastYear.add(Calendar.YEAR, -1);

String timeString;
String providerName;
if (eventDateTime.after(yesterday)) {
    timeString = DateFormat.getTimeInstance(DateFormat.SHORT,
            Locale.getDefault()).format(eventDateTime.getTime());
    providerName = IMProvider.IMServiceNames[msgItem
            .getIMProviderType().ordinal()];
} else if (eventDateTime.after(lastYear)) {
    SimpleDateFormat sdfOriginal = (SimpleDateFormat) SimpleDateFormat
            .getDateTimeInstance(DateFormat.SHORT,
                    DateFormat.SHORT, Locale.getDefault());
    timeString = Utils.stripFieldFromPattern(sdfOriginal,
            eventDateTime.getTime(), DateFormat.Field.YEAR);
    providerName = IMProvider.IMServiceNames[msgItem
            .getIMProviderType().ordinal()];

} else {
    timeString = DateFormat.getDateTimeInstance(DateFormat.SHORT,
            DateFormat.SHORT, Locale.getDefault()).format(
            eventDateTime.getTime());
    providerName = IMProvider.IMServiceShortNames[msgItem
            .getIMProviderType().ordinal()];
}

String format = "%1$s";
if (!this.useColoredStatus && !this.useIMServiceIcon
        && !this.useIMServiceIndicator)
    format += " via %2$s";
subText = String.format(format, timeString, providerName);

I just realized the providerName is repeated unnecessarily. Oops! Regardless, it has nothing to with the topic here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: