Our Meeho!™ Blog brings you general news, tech stuff, tips, inspiration and more in relation to the Meeho!™ platform.

Sign up now for free!

Archive for the ‘Cases’ category

» Time Zone Conversion with java.util.Date

Posted by Anders Østergaard Jensen on 3/9 2010 at 3:54 AM

When building global web sites it is often necessary to display timestamps within the user’s own time zone. Ruby on Rails contains built-in functionality for this, whereas Java requires a more manual approach. In the following I will describe how such time zone conversion (and mapping) is easily implemented in Java.

The following method creates a time zone conversion from a standard java.util.Date (which defaults to the machine’s local time zone) to a specific time zone:

public static String formatDanishDate(Date d, String tz) {
DateFormat fmt = new SimpleDateFormat(”d/MM/yyyy ‘kl.’ HH:mm Z”);
TimeZone zone = getTimeZone(tz);
fmt.setTimeZone(zone);
return fmt.format(d);
}
public static String formatUSDate(Date d, String tz) {
DateFormat fmt = new SimpleDateFormat(”EEE, d/MM/yyyy ‘at’ KK:mm a Z”);
TimeZone zone = getTimeZone(tz);
logger.error(”+++ Chosen tz was: ” + zone.getDisplayName());
fmt.setTimeZone(zone);
logger.error(”LOL FORMAT: ” + fmt.format(d));
return fmt.format(d);
}
public static String formatUSDate(Date d, String tz) {

    DateFormat fmt = new SimpleDateFormat("EEE, d/MM/yyyy 'at' KK:mm a Z");

    TimeZone zone = getTimeZone(tz);

    fmt.setTimeZone(zone);

    return fmt.format(d);

  }

The above method depends on the following support function that searches through the Java library of supported time zones (with lower case matching).  If no match is found, getDefault() is returned. According to the SDK Javadoc, the default time zone depends on your system locale (if you are in Copenhagen, the standard time zone will be Europe/Copenhagen).

  public static TimeZone getTimeZone(String tz) {

    TimeZone retVal;

    for (String id: TimeZone.getAvailableIDs()) {

      if (id.toLowerCase().contains(tz.toLowerCase())) {

        retVal = TimeZone.getTimeZone(id);

        return retVal;

      }

    }

    return TimeZone.getDefault();

  }

Using the lowercase match search is especially useful if you are using time zones across different applications or frameworks with the same database. Fx. Ruby on Rails only stores the selected session time zone with the last identifier (e.g. ‘Copenhagen’ rather than ‘Europe/Copenhagen’), which makes it impossible to implement a one-to-one mapping between the two environments. Using getTimeZone(”Sydney”) or getTimeZone(”Copenhagen”) will thus return the Java time zone “Australia/Sydney” and ”Europe/Copenhagen” respectively.

Finally, the SimpleDateFormat lets you format the now converted Date representation to a localized format. Thus, the user can now also choose from different date formats in your application. This Javadoc link clarifies the different formatting options. For instance, a proper date representation for the Danish calendar would be:

DateFormat fmt = new SimpleDateFormat("d/MM/yyyy 'kl.' HH:mm Z");

Please let us know if you have further efficient methods for time zone conversion in Java and across different platforms.

» New case about Sidekick

Posted by Kasper Tidemann on 12/9 2009 at 8:23 PM

A new case has been added to our Danish web site, this time presenting the creative marketing bureau Sidekick and their use of Meeho!™. You can read the Danish case about Sidekick here.

» New case about Lydfolket

Posted by Kasper Tidemann on 12/1 2009 at 2:38 AM

Yet another client case surfaces on our Danish web site, this time focusing on the very competent guys of sound and light, Lydfolket (it means “The Sound People” in English). You can read the Danish case about Lydfolket here.

» New case about BLINDesign

Posted by Kasper Tidemann on 11/20 2009 at 4:28 AM

We have put up a new case on our Danish web site. This time it’s about the skilled bureau BLINDesign, experts in creating beautiful visual solutions, motion graphics, and much, much more. You can read the Danish case about BLINDesign here.