时区转换 [英] Timezone conversion

查看:111
本文介绍了时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的项目中将一个时区转换为另一个时区。



我可以将我当前的时区转换为另一个,但不能从不同的时区转换另一个。



例如我在印度,我可以使用转换为印度到美国Date d = new Date(); 并将其分配给日历对象并设置时区。



但是,我不能从不同的时区到另一个时区。例如,我在印度,但是我无法将时区从美国转移到英国。

解决方案

/ p>

转换时间时区



在时区之间转换时间

  import java .util.Calendar; 
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class TimeZoneExample {
public static void main(String [] args){
//创建一个日历对象,并根据本地
/ /时区
日历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);


//打印本地时间
System.out.printf(本地时间:%02d:%02d:%02d\\\
,小时,分钟,秒);


//创建一个表示德国时区的日历对象。然后我们
//用当地时间的值来润滑日历的时间

日历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);


//打印德国时区的当地时间
System.out.printf(德国时间:%02d:%02d:%02d\\\
,小时,分,秒);
}
}


I need to convert from one timezone to another timezone in my project.

I am able to convert from my current timezone to another but not from a different timezone to another.

For example I am in India, and I am able to convert from India to US using Date d=new Date(); and assigning it to a calendar object and setting the time zone.

However, I cannot do this from different timezone to another timezone. For example, I am in India, but I am having trouble converting timezones from the US to the UK.

解决方案

Some examples

Convert time between timezone

Converting Times Between Time Zones

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);
    }
}

这篇关于时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆