Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been struggling to get to grips with Joda-Time, but feel that I've finnaly managed to get a grip on some of the functionality that I need.

I have written a function that will convert a DateTime from one timezone to another. I've not done any unit testing on it (but have done my own testing), but would appreciate comments on if it could be improved. The code is in native Java.

import org.joda.time.*;
import org.joda.time.format.*;
public class testing {

     * @param args
    public static void main(String[] args) {

public string ConvertTimeZones(String sFromTimeZone, String sToTimeZone, String sFromDateTime){
        DateTimeZone oFromZone       = DateTimeZone.forID(sFromTimeZone);
        DateTimeZone oToZone         = DateTimeZone.forID(sToTimeZone);

        DateTime oDateTime           = new DateTime(sFromDateTime);
        DateTime oFromDateTime       = oDateTime.withZoneRetainFields(oFromZone);

        DateTime oToDateTime         = new DateTime(oFromDateTime).withZone(oToZone);

        DateTimeFormatter oFormatter = new DateTimeFormat.forPattern("yyyy-MM-dd'T'H:mm:ss.SSSZ");
        DateTimeFormatter2           = new DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss");

        DateTime oNewDate            = oFormatter.withOffsetParsed().parseDateTime(oToDateTime.toString());

        return oFormatter2.withZone(oToZone).print(oNewDate.getMillis());

The arguments:

sFromTimeZone = UTC
sToTimeZone = Europe/London
sFromDateTime = 2012-05-08 18:00:00

should produce a return of

2012-05-08 19:00:00
share|improve this question
up vote 5 down vote accepted
  1. First of all, currently it does not compile. You should fix that, non-working codes are off-topic here.

  2. According to the Code Conventions for the Java Programming Language, method names should start with lowercase letters and class names should start with uppercase letters. ConvertTimeZones should be convertTimeZones.

  3. The o prefix is unnecessary for every variable.

  4. You could omit lots of intermediate objects. Here is a simplified version:

    public static String convertTimeZones(final String fromTimeZoneString, 
            final String toTimeZoneString, final String fromDateTime) {
        final DateTimeZone fromTimeZone = DateTimeZone.forID(fromTimeZoneString);
        final DateTimeZone toTimeZone = DateTimeZone.forID(toTimeZoneString);
        final DateTime dateTime = new DateTime(fromDateTime, fromTimeZone);
        final DateTimeFormatter outputFormatter 
            = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss").withZone(toTimeZone);
        return outputFormatter.print(dateTime);
  5. Here are a few unit tests:

    import static ...TimeZoneConverter.convertTimeZones;
    import static org.junit.Assert.assertEquals;
    import java.util.Arrays;
    import java.util.Collection;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    @RunWith(value = Parameterized.class)
    public class TimeZoneConverterTest {
        private final String expectedDatetime;
        private final String fromTimezone;
        private final String toTimezone;
        private final String inputDatetime;
        public TimeZoneConverterTest(final String expectedDatetime, 
                final String fromTimezone, final String toTimezone,
                final String inputDatetime) {
            this.expectedDatetime = expectedDatetime;
            this.fromTimezone = fromTimezone;
            this.toTimezone = toTimezone;
            this.inputDatetime = inputDatetime;
        public static Collection<Object[]> data() {
            final Object[][] data = new Object[][] {
                { "2012-05-08 19:00:00", 
                    "UTC", "Europe/London", "2012-05-08T18:00:00" },
                { "2012-05-08 17:00:00", 
                    "Europe/London", "UTC", "2012-05-08T18:00:00" },
                { "2012-05-08 20:00:00", 
                    "UTC", "CET", "2012-05-08T18:00:00" },
                { "2012-05-08 19:00:00", 
                    "America/Tijuana", "CET", "2012-05-08T10:00:00" },
                { "2012-11-08 21:00:00", 
                    "Europe/London", "Asia/Dubai", "2012-11-08T17:00:00" },
                { "2012-11-19 2:00:00", 
                    "Europe/London", "Asia/Tokyo", "2012-11-18T17:00:00" } };
            return Arrays.asList(data);
        public void test() {
                convertTimeZones(fromTimezone, toTimezone, inputDatetime));
share|improve this answer
Hi. I am getting exception: java.lang.IllegalArgumentException: Invalid format: "2015-03-25 19:38:56" is malformed at " 19:38:56" at org.joda.time.format.DateTimeParserBucket.doParseMillis(DateTimeParserBucket.jav‌​a:187) at org.joda.time.format.DateTimeFormatter.parseMillis( at org.joda.time.convert.StringConverter.getInstantMillis( at org.joda.time.base.BaseDateTime.<init>( at org.joda.time.DateTime.<init>( – Shajeel Afzal Mar 25 '15 at 20:09
@ShajeelAfzal: It seems to me that the code above still works well on my machine with Joda-Time 2.1. I guess it is quite old now so I've tried with 2.7 and it works as well. My guess it that you probably missed the T (it should be 2015-03-25T19:38:56). If it's not help could you ask a question with some code and context information on Stack Overflow and drop me a link here? – palacsint Mar 25 '15 at 20:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.