本地日期和放大器;时间UTC,然后UTC当地的日期和放大器;时间 [英] Local date & time to UTC and then UTC to local date & time

查看:190
本文介绍了本地日期和放大器;时间UTC,然后UTC当地的日期和放大器;时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想现场的时间转换为UTC,然后向UTC时间语言环境。但我没有得到的结果。

I am trying to convert locale time to UTC, and then UTC to locale time. But I am not getting the result.

public class DateDemo
{

public static void main(String args[])
{
    DateFormat dateFormatter = 
               DateFormat.getDateTimeInstance
                 (DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());

    TimeZone.setDefault(TimeZone.getDefault());

    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");

    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);

    try
    {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String date = simpleDateFormatter.format(today);
        String time = simpleTimeFormatter.format(today);
        System.out.println("Today's only date: " + date);
        System.out.println("Today's only time: " + time);

        //// Locale to UTC converting

        simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        simpleTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = simpleTimeFormatter.format(today);
        System.out.println("Convert into UTC's date: " + utcDate);
        System.out.println("Convert into UTC's only time: " + utcTime);

         //// UTC to locale converting

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = simpleTimeFormatter.parse(utcTime);

        String getLocalDate = simpleDateFormatter.format(getDate);
        String getLocalTime = simpleTimeFormatter.format(getTime);
        System.out.println("Get local date: " + getLocalDate);
        System.out.println("Get local time: " + getLocalTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }
  }
 }

我送当地的日期和放大器;时间到Web服务,然后要求我需要检索的UTC日期和放大器时;时间,然后转换成语言环境日期和放大器;时间(即用户的本地设置)。

I am sending local date & time to the web service, and then when require I need to retrieve the UTC date & time and then convert into locale date & time (i.e. user's local settings).

示例输出:

Locale:11/9/12 8:15 PM
After parsing a date: Fri Nov 09 20:15:00 SGT 2012
Today's only date: 09/11/2012
Today's only time: 08:15:30 PM
Convert into UTC's date: 09/11/2012
Convert into UTC's only time: 12:15:30 PM
Get local date: 09/11/2012
Get local time: 12:15:30 PM


Saksak&放后; ADTC答案:

对于code片段,UTC日期和放大器;时间(什么是真正的未来的 GMT-5 的数据库,因为可能在美国)是输入的,我希望得到当地日期和放大器;时间作为输出。但是,这下段仍然给的 GMT-5 的时间。

For the code fragment, UTC date & time (what is actually coming as GMT-5 because database may be in USA) is the input, and I want to get local date & time as output. But this following segment is still giving GMT-5 time.

SimpleDateFormat simpleDateTimeFormatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a");

....

Date inDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("inTime")); Date outDateTime = simpleDateTimeFormatter.parse(intent.getExtras().getString("outTime"));

simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault()); simpleDateTimeFormatter.setTimeZone(simpleDateTimeFormatter.getTimeZone());

//TimeZone tzTimeZone = TimeZone.getDefault();

//System.out.println("Current time zone: " + tzTimeZone.getDisplayName());

String getLocalInTimeString = simpleDateTimeFormatter.format(inDateTime); 

String getLocalOutTimeString = simpleDateTimeFormatter.format(outDateTime);

我的问题: getLocalInTimeString &安培; getLocalOutTimeString 仍表现出的 GMT-5 的时机。什么是错在这里?我是否需要设置任何其他的东西?

My question: getLocalInTimeString & getLocalOutTimeString still showing GMT-5 timing. What's wrong here? Do I need to set any other things?

推荐答案

您需要做的,解决你的问题如下,你有你的code转换回本地时间顺序如下:

What you need to do to solve your problem is the following, you have your code to convert back to local time in this order :

simpleDateFormatter.setTimeZone(TimeZone.getDefault());
simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

Date getDate = simpleDateFormatter.parse(utcDate);
Date getTime = simpleTimeFormatter.parse(utcTime);

而你需要做的是等待,直到你解析utcDate,utcTime字符串返回Date对象
然后设置日期格式时区到本地区域如下:

and what you need to do is wait until you parse utcDate, utcTime Strings back to Date Object then set the date formatter time zone to local zone as follows :

Date getDate = simpleDateFormatter.parse(utcDate);
Date getTime = simpleTimeFormatter.parse(utcTime);

simpleDateFormatter.setTimeZone(TimeZone.getDefault());
simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

这应该打印正确的日期/时间在本地。

this should print the correct date/time in local.

编辑:
这里是完整的主要方法:

here is the full main method :

public static void main(String[] args) {
    DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
    TimeZone.setDefault(TimeZone.getDefault());
    SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("dd/MM/yyyy");
    SimpleDateFormat simpleTimeFormatter = new SimpleDateFormat("hh:mm:ss a");
    Date today = new Date();
    String localeFormattedInTime = dateFormatter.format(today);
    try {
        Date parsedDate = dateFormatter.parse(localeFormattedInTime);
        System.out.println("Locale:" + localeFormattedInTime);
        System.out.println("After parsing a date: " + parsedDate);

        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String date = simpleDateFormatter.format(today);
        String time = simpleTimeFormatter.format(today);
        System.out.println("Today's only date: " + date);
        System.out.println("Today's only time: " + time);

        //// Locale to UTC converting

        System.out.println("TimeZone.getDefault() >>> " + TimeZone.getDefault());

        simpleDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        simpleTimeFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));

        String utcDate = simpleDateFormatter.format(today);
        String utcTime = simpleTimeFormatter.format(today);
        System.out.println("Convert into UTC's date: " + utcDate);
        System.out.println("Convert into UTC's only time: " + utcTime);

        //// UTC to locale converting
        /**
         ** //////EDIT
        */
        // at this point your utcDate,utcTime are strings that are formated in UTC
        // so first you need to parse them back to Dates using UTC format not Locale
        Date getDate = simpleDateFormatter.parse(utcDate);
        Date getTime = simpleTimeFormatter.parse(utcTime);

        // NOW after having the Dates you can change the formatters timezone to your
        // local to format them into strings
        simpleDateFormatter.setTimeZone(TimeZone.getDefault());
        simpleTimeFormatter.setTimeZone(TimeZone.getDefault());

        String getLocalDate = simpleDateFormatter.format(getDate);
        String getLocalTime = simpleTimeFormatter.format(getTime);
        System.out.println("Get local date: " + getLocalDate);
        System.out.println("Get local time: " + getLocalTime);

    } catch (ParseException e) {
        e.printStackTrace();
    }

}

这篇关于本地日期和放大器;时间UTC,然后UTC当地的日期和放大器;时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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