Muszę przekonwertować aktualną datę i godzinę na millis i na inną strefę czasową GMT + 3 (moja strefa czasowa to GMT-2). Używam tego kodu, ale zwraca mi swój czas, ale do mojej strefy czasowej .... dlaczego?
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT-3"));
cal.get(Calendar.DAY_OF_MONTH);
cal.get(Calendar.MONTH);
cal.get(Calendar.YEAR);
cal.get(Calendar.HOUR_OF_DAY);
cal.get(Calendar.MINUTE);
cal.get(Calendar.SECOND);
cal.get(Calendar.MILLISECOND);
long timez = cal.getTime().getTime();
Odpowiedzi:
0 dla odpowiedzi № 1Musisz użyć SimpleDateFormat. Kalendarz zawsze używa domyślnie skonfigurowanej strefy czasowej na twoim komputerze. Tutaj jest przykład o tym, jak osiągnąć tę funkcjonalność za pomocą SimpleDateFormat.
0 dla odpowiedzi nr 2
Date date = new Date();
DateFormat firstFormat = new SimpleDateFormat();
DateFormat secondFormat = new SimpleDateFormat();
TimeZone firstTime = TimeZone.getTimeZone(args[0]);
TimeZone secondTime = TimeZone.getTimeZone(args[1]);
firstFormat.setTimeZone(firstTime);
secondFormat.setTimeZone(secondTime);
System.out.println("-->"+args[0]+": " + firstFormat.format(date));
System.out.println("-->"+args[1]+": " + secondFormat.format(date));
}
gdzie arg [0] i arg1 to dwie strefy czasowe. Zobacz to POŁĄCZYĆ
-1 dla odpowiedzi nr 3
Metoda getTime () zwraca ten sam czas. nie ma związku ze strefą czasową.