I've been struggling to get to grips with JODATIME, 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