patternjavaMinor
Converting timedate timezones with Joda-Time
Viewed 0 times
timezoneswithtimetimedateconvertingjoda
Problem
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
The arguments:
should produce a return of
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:00should produce a return of
2012-05-08 19:00:00Solution
-
First of all, currently it does not compile. You should fix that, non-working codes are off-topic here.
-
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.
-
The
-
You could omit lots of intermediate objects. Here is a simplified version:
-
Here are a few unit tests:
First of all, currently it does not compile. You should fix that, non-working codes are off-topic here.
-
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.- Code Conventions for the Java Programming Language, 9 - Naming Conventions
- Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions
-
The
o prefix is unnecessary for every variable.-
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);
}-
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 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));
}
}Code Snippets
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);
}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));
}
}Context
StackExchange Code Review Q#18599, answer score: 9
Revisions (0)
No revisions yet.