时区转换。

时区转换。

在我的项目中,我需要从一个时区转换到另一个时区。

我能够从我的当前时区转换到另一个时区,但不能从一个不同的时区转换到另一个时区。

例如,我在印度,我可以通过Date d=new Date();并将其分配给日历对象并设置时区。

但是,我不能从不同的时区执行到另一个时区。例如,我在印度,但我很难将时区从美国转换到英国。


杨__羊羊
浏览 590回答 3
3回答

DIEA

几个例子时区间转换时间时区间转换时间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

   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();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java