简体   繁体   English

将UTC时间戳转换为任何timeZone

[英]Converting UTC Timestamp to Any timeZone

I am trying to convert UTC date time stamp to any Timezone but i always end up with UTC date time in both cases. 我试图将UTC日期时间戳转换为任何时区,但在两种情况下,我总是以UTC日期时间结尾。

 import java.sql.Timestamp;
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.TimeZone;

  public class UTCtoLocalTime {

private static String ConvertToLocalTime(String id, String time) throws ParseException{
    DateFormat localtime = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    localtime.setTimeZone(TimeZone.getTimeZone(id));
    return localtime.format(localtime.parse(time));
}

public static void main(String[] args) throws ParseException {
    SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
    dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));

    String time =  dateFormatGmt.format(new Timestamp(System.currentTimeMillis()));

    System.out.println("UTC time "+ time);
    System.out.println("Local Time "+ ConvertToLocalTime("America/Mexico_City", time));
}

} }

I am not sure where i am doing mistake. 我不确定我在哪里做错。 Can any one help me? 谁能帮我?

Date doesn't has a time zone. 日期没有时区。 In String time you are displaying a Date in some way and expecting that display to use some time zone other . 在String time您将以某种方式显示日期,并期望该显示使用其他时区。 It won't. 不会的

String time represents a timestamp in one time zone, and you want to change that to a string which represents a timestamp in another time zone, 字符串time表示一个时区中的时间戳,您想将其更改为表示另一时区中的时间戳的字符串,

So basically you have 所以基本上你有

Create another SimpleDateFormat and apply the second time zone to it Use that to format the Date object to a new String 创建另一个SimpleDateFormat并对其应用第二个时区使用该时区将Date对象格式化为新的String

Here is implemented Code 这是实现的代码

public class TestClass {
        private static String ConvertToLocalTime(String id, String time) throws ParseException{
            DateFormat localtime = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            localtime.setTimeZone(TimeZone.getTimeZone(id));
            return localtime.format(localtime.parse(time));
        }

        public static void main(String[] args) throws ParseException {
            SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));

            String time =  dateFormatGmt.format(new Timestamp(System.currentTimeMillis()));
            System.out.println("UTC time "+ time);

            SimpleDateFormat dateFormatGmt1 = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
            String time1 =  dateFormatGmt1.format(new Timestamp(System.currentTimeMillis()));
            System.out.println("Local Time "+ ConvertToLocalTime("America/Mexico_City", time1));
        }

        }

OutPut on my Machine 在我的机器上输出

UTC time 2015-Jun-16 15:45:16
Local Time 2015-Jun-16 21:15:16

Here i use this to serve my purpose 在这里我用它来达到我的目的

public static String UTCtolocaltime(String timzoneid, String UTCTime){
        String convertedlocaldatetime;
        Date datetimeinutc = null;
        SimpleDateFormat utcdateFormat = new SimpleDateFormat(some patteren); 
        utcdateFormat.setTimeZone(TimeZone.getTimeZone(UTC));
        try {
            datetimeinutc = utcdateFormat.parse(UTCTime);
        } catch (ParseException e1) {
            log.error(e1);
        }
        SimpleDateFormat localdateFormat = new SimpleDateFormat(some patteren); 
        localdateFormat.setTimeZone(TimeZone.getTimeZone(timzoneid));
             convertedlocaldatetime = localdateFormat.format(datetimeinutc);
             return convertedlocaldatetime;
    }

Easy to in JODA time: 易于在JODA时代:

Assuming you have a DateTime storing the incoming date time which in UTC, the way to get the string presentation of it for another timezone is merely: 假设您有一个DateTime存储输入的日期时间(以UTC为单位),那么在另一个时区中获取它的字符串表示的方法仅仅是:

String result = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss")
                               .withZone(DateTimeZone.forID(timeZoneId))
                               .print(utcDateTime);

Different combination of String , LocalDateTime and Datetime conversion between UTC and another timezone are all very easy UTC与另一个时区之间的String,LocalDateTime和Datetime转换的不同组合都非常容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM