使用joda时间将一个时区转换为另一个时区 [英] convert one time zone to another using joda time
问题描述
有一个国家的下拉菜单,用户将选择国家,然后是时区下拉,用户将选择用户所选择的国家/地区可用的时区,然后用户将进入当地日期(例如:2014年12月26日)和时间(23:11)(24小时),此输入的日期和时间是所选国家和时区。
现在我必须将这个日期和时间转换为GMT时区。如何使用joda时间来执行此操作?
there is a form having a drop down for country ,user will select the country ,then there is a drop down for time zone ,user will select the time zone which are available in country selected by the user.Then user will enter the local date( eg: 26-Dec-2014) and time( 23:11)(24 hours time) this entered date and time is for the selected country and time zone. now i have to convert this date and time to GMT time zone. how can i do this using joda time
如何计算夏令时(DST)?
how the daylight saving time(DST) will be calculate?
我已经做了一个接收参数的函数,从时区到时区,日期
i have made a function which accepts the parameters as from time zone,to time zone,date
public static String convertTimeZones( String fromTimeZoneString,
String toTimeZoneString, String fromDateTime) {
DateTimeZone fromTimeZone = DateTimeZone.forID(fromTimeZoneString);
DateTimeZone toTimeZone = DateTimeZone.forID(toTimeZoneString);
DateTime dateTime = new DateTime(fromDateTime, fromTimeZone);
DateTimeFormatter outputFormatter
= DateTimeFormat.forPattern("dd-MMM-yyyy HH:mm").withZone(toTimeZone);
return outputFormatter.print(dateTime);
}
我想以日期格式(24-Feb -2014 12:34)但是不采取这种格式
i want to pass the date to this function in a format (24-Feb-2014 12:34) but it is not taking this format
推荐答案
//so we can get the local date
//UTC = true, translate from UTC time to local
//UTC = false, translate from local to UTC
public static String formatUTCToLocalAndBackTime(String datetime, boolean UTC) {
String returnTimeDate = "";
DateTime dtUTC = null;
DateTimeZone timezone = DateTimeZone.getDefault();
DateTimeFormatter formatDT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
DateTime dateDateTime1 = formatDT.parseDateTime(datetime);
DateTime now = new DateTime();
DateTime nowUTC = new LocalDateTime(now).toDateTime(DateTimeZone.UTC);
long instant = now.getMillis();
long instantUTC = nowUTC.getMillis();
long offset = instantUTC - instant;
if (UTC) {
//convert to local time
dtUTC = dateDateTime1.withZoneRetainFields(DateTimeZone.UTC);
//dtUTC = dateDateTime1.toDateTime(timezone);
dtUTC = dtUTC.plusMillis((int) offset);
} else {
//convert to UTC time
dtUTC = dateDateTime1.withZoneRetainFields(timezone);
dtUTC = dtUTC.minusMillis((int) offset);
}
returnTimeDate = dtUTC.toString(formatDT);
return returnTimeDate;
}
这篇关于使用joda时间将一个时区转换为另一个时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!