時區轉換。在我的項目中,我需要從一個時區轉換到另一個時區。我能夠從我的當前時區轉換到另一個時區,但不能從一個不同的時區轉換到另一個時區。例如,我在印度,我可以通過Date d=new Date();并將其分配給日歷對象并設置時區。但是,我不能從不同的時區執行到另一個時區。例如,我在印度,但我很難將時區從美國轉換到英國。
3 回答
DIEA
TA貢獻1820條經驗 獲得超3個贊
import java.util.Calendar;import java.util.GregorianCalendar;import java.util.TimeZone;public class TimeZoneExample {
public static void main(String[] args) {
// Create a calendar object and set it time based on the local
// time zone
Calendar localTime = Calendar.getInstance();
localTime.set(Calendar.HOUR, 17);
localTime.set(Calendar.MINUTE, 15);
localTime.set(Calendar.SECOND, 20);
int hour = localTime.get(Calendar.HOUR);
int minute = localTime.get(Calendar.MINUTE);
int second = localTime.get(Calendar.SECOND);
// Print the local time
System.out.printf("Local time : %02d:%02d:%02d\n", hour, minute, second);
// Create a calendar object for representing a Germany time zone. Then we
// wet the time of the calendar with the value of the local time
Calendar germanyTime = new GregorianCalendar(TimeZone.getTimeZone("Europe/Berlin"));
germanyTime.setTimeInMillis(localTime.getTimeInMillis());
hour = germanyTime.get(Calendar.HOUR);
minute = germanyTime.get(Calendar.MINUTE);
second = germanyTime.get(Calendar.SECOND);
// Print the local time in Germany time zone
System.out.printf("Germany time: %02d:%02d:%02d\n", hour, minute, second);
}}
慕哥6287543
TA貢獻1831條經驗 獲得超10個贊
Date date = new Date(); String formatPattern = ....; SimpleDateFormat sdf = new SimpleDateFormat(formatPattern); TimeZone T1; TimeZone T2; // set the Calendar of sdf to timezone T1 sdf.setTimeZone(T1); System.out.println(sdf.format(date)); // set the Calendar of sdf to timezone T2 sdf.setTimeZone(T2); System.out.println(sdf.format(date)); // Use the 'calOfT2' instance-methods to get specific info // about the time-of-day for date 'date' in timezone T2. Calendar calOfT2 = sdf.getCalendar();
添加回答
舉報
0/150
提交
取消
