Har*_*pta 5 java timezone datetime jodatime
有一个表格,有一个国家下拉菜单,用户将选择国家,然后有一个时区下拉菜单,用户将选择用户选择的国家/地区可用的时区。然后用户将输入当地日期(例如:26-Dec-2014)和时间(23:11)(24 小时制) 此输入的日期和时间适用于所选国家和时区。现在我必须将此日期和时间转换为 GMT 时区。我如何使用 joda 时间来做到这一点
如何计算夏令时(DST)?
我做了一个函数,它接受从时区到时区到日期的参数
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);
}
Run Code Online (Sandbox Code Playgroud)
我想以一种格式 (24-Feb-2014 12:34) 将日期传递给这个函数,但它没有采用这种格式
//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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16054 次 |
最近记录: |