Java 使用 joda 时间将一个时区转换为另一个时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21258214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:44:32  来源:igfitidea点击:

convert one time zone to another using joda time

javadatetimetimezonejodatime

提问by Harish Gupta

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

有一个表格,有一个国家下拉菜单,用户将选择国家,然后有一个时区下拉菜单,用户将选择用户选择的国家/地区可用的时区。然后用户将输入当地日期(例如:26-Dec-2014)和时间(23:11)(24 小时制) 此输入的日期和时间适用于所选国家和时区。现在我必须将此日期和时间转换为 GMT 时区。我如何使用 joda 时间来做到这一点

how the daylight saving time(DST) will be calculate?

如何计算夏令时(DST)?

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);
    }

i want to pass the date to this function in a format (24-Feb-2014 12:34) but it is not taking this format

我想以一种格式 (24-Feb-2014 12:34) 将日期传递给这个函数,但它没有采用这种格式

回答by Kristy Welsh

//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;
}