Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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
add comment

1 Answer

up vote 2 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;
        }
    
        @Parameters
        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);
        }
    
        @Test
        public void test() {
            assertEquals(expectedDatetime, 
                convertTimeZones(fromTimezone, toTimezone, inputDatetime));
        }
    }
    
share|improve this answer
add comment

Your Answer

 
discard

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.