Przetwarzanie sformatowanego ciągu dat w innej strefie czasowej do daty obiektu nie działa

Usiłuję przekonwertować sformatowaną datę String na obiekt Date. Date String jest sformatowany na inną strefę czasową.

Kiedy robięsdf.parse(String) zwraca mi mój obiekt Data systemu.

Kod jest jak poniżej,

static Date convertGMTTime(String timeZone, long longDate){
    Date convertedTime = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    try{
        Date date = new Date(longDate);
        System.out.println("timezone: "+timeZone +", timestamp: "+date);
        Locale locale = Locale.ENGLISH;
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
/*          DateFormat formatter = DateFormat.getDateTimeInstance(
                    DateFormat.DEFAULT,
                    DateFormat.DEFAULT,
                    locale);
        formatter.setTimeZone(destTimeZone);*/
        sdf.setTimeZone(destTimeZone);
        String convertedDateStr = sdf.format(date);
        System.out.println("convertedDateStr: "+convertedDateStr);
        convertedTime = sdf.parse(convertedDateStr);
        System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
    }catch(Exception e){
        e.printStackTrace();
    }
    return convertedTime;
}

Byłbym wdzięczny, gdyby ktoś mógł pomóc i wskazać, gdzie się mylę. Z góry dziękuję.

Wydajność:

strefa czasowa: Atlantic / Cape_Verde, timestamp: Tue Jun 26 17:38:11 IST 2012
Strefa czasowa źródła: sun.util.calendar.ZoneInfo [id = "Atlantic / Cape_Verde", offset = -3600000, dstSavings = 0, useDaylight = false, przejścia = 6, lastRule = null]

convertDateStr: 2012-06-26 11:08:11

convertTime: Tue Jun 26 17:38:11 IST 2012
sdf: sun.util.calendar.ZoneInfo [id = "Atlantic / Cape_Verde", offset = -3600000, dstSavings = 0, useDaylight = false, przejścia = 6, lastRule = null]

Więcej szczegółów do udostępnienia, Kiedy używam innego obiektu sdf (bez ustawiania dla niego strefy czasowej), zwraca mi poprawną godzinę i datę, ale nadal jest wybierana strefa czasowa z zegara systemowego

Kod

static Date convertGMTTime(String timeZone, long longDate){
    Date convertedTime = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdfParse = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try{
        Date date = new Date(longDate);
        TimeZone destTimeZone = TimeZone.getTimeZone(timeZone);// TimeZone.getDefault();
        System.out.println("Source timezone: "+destTimeZone);
        sdf.setTimeZone(destTimeZone);
        String convertedDateStr = sdf.format(date);
        System.out.println("convertedDateStr: "+convertedDateStr );
        convertedTime = sdfParse.parse(convertedDateStr,new ParsePosition(0));
        System.out.println("convertedTime: "+convertedTime + "sdf: "+sdf.getTimeZone());
    }catch(Exception e){
        e.printStackTrace();
    }
    return convertedTime;
}

Wydajność

Source timezone: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

convertedDateStr: 2012-06-26 12:24:56

convertedTime: Tue Jun 26 12:24:56 IST 2012 

sdf: sun.util.calendar.ZoneInfo[id="Atlantic/Cape_Verde",offset=-3600000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null]

Rozumiem, że kiedy nie przypisuję strefy czasowej do sdf, trwa strefa czasowa systemu, ale dlaczego nie wyświetla czasu w strefie czasowej systemu? Pokazuję to w strefie czasowej, tak jak w String, ale strefa czasowa jest inna.

Odpowiadając na ustawienie strefy czasowej, zwraca obiekt daty zgodnie z moim czasem systemowym, niezależnie od tego, że sdf ma inny zestaw stref czasowych.

Czy ktoś może wyjaśnić funkcjonalne zachowanie sdf.parse i sdf.format.

Dla mnie sdf.setTimeZone () ma swój wpływ, gdy używamy formatu i jest unieważniany, gdy używamy sdf.parse (). Uważam to za dość dziwne.

Doceń pomoc w tym zakresie.

questionAnswers(1)

yourAnswerToTheQuestion