时区转换。在我的项目中,我需要从一个时区转换到另一个时区。我能够从我的当前时区转换到另一个时区,但不能从一个不同的时区转换到另一个时区。例如,我在印度,我可以通过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
提交
取消
