2012-11-09 189 views
0

我试图将区域时间转换为UTC,然后UTC转换为区域时间。但我没有得到结果。本地日期和时间到UTC然后UTC到本地日期和时间

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

当需要我需要检索UTC日期&时间,然后转换成区域日期&时间(即用户的本地设置)我送本地日期&时间到Web服务,然后。

样本输出:

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答案后:

的代码片段,UTC日期&时间(什么是真正来为GMT-5因为数据库可能在美国)是输入,我想获得当地日期&时间作为输出。但是下面这个细分市场仍然在给予时间。

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定时。这里有什么问题?我需要设置其他东西吗?

+0

到底什么是不工作?例外?踪迹?运行程序的输出? –

+2

分享你的代码,它会听起来有利于我们的有效对话。 – gowri

+0

@Dibyendu注意到,使用两个单独的解析器日期和时间可能会导致转换问题。为了避免它们,使用一个解析器来一起解析日期和时间。请参阅我的答案了解更多详情。 – ADTC

回答

4

你需要做的,解决你的问题是下面的什么,你有你的代码转换回本地时间顺序如下:

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

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

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

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

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

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

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

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

} 
+0

您将代码提供了出来;)...但这不能可靠地工作。我得到一个半小时的差异。看到我的答案。 – ADTC

+0

@ADTC对不起,我写了我的答案,离开了笔记本电脑,然后又回来了,所以我没有看到你的答案,我测试了代码,它完全适合我。 –

+0

对不起,我错了。该方法是可靠的,但原始海报的使用方式是错误的。他在日期和时间上使用了单独的解析器,这导致了我得到的半小时差异。如果您将一个解析器同时用作一个字符串的日期和时间,那么您将得到正确的时间(没有我遇到的不稳定差异)。检查我的答案的最新更新。 – ADTC

4

你的问题是线54和55:

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

这些线路只是解析包含日期和时间字符串,但这些字符没有任何时区信息:

utcDate = "09/11/2012" 
    utcTime = "12:15:30 PM" 

因此,解析器假定字符串已经在您在第51行和第52行中设置的时区的语言环境中。

现在想想如何解决它;)提示:确保解析器假定字符串表示的时间是正确的。

PS:[!议决] 我解决了这个问题,但我发现,时区转换是不稳定的,至少我在哪里。 本地时间为8:30 pm。转换为UTC,12:30 pm(正确,8小时差)。转换回来,这是8:00 pm(错误,虽然设置的时区是正确的 - 我得到了原来的一个,并通过它在 - 我只得到7.5小时的差异)。 除非您能弄清楚发生了什么以及如何解决它,否则您应该寻找更可靠的方法。

[解决方法:]上面的问题实际上是由于原代码分裂的日期和时间为两个不同的解析器。如果您只将一个解析器同时用于日期和时间,则您将在目标语言环境中获得正确的日期和时间。所以最后解析器可靠,但你使用它的方式有很大的不同!

SimpleDateFormat simpleDateTimeFormatter 
      = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss a"); 
    Date getDateTime 
      = simpleDateTimeFormatter.parse(utcDate + " " + utcTime); 
    //use above line if you have the date and time as separate strings 
    simpleDateTimeFormatter.setTimeZone(TimeZone.getDefault()); 
    String getLocalDateTime = simpleDateTimeFormatter.format(getDateTime); 
    System.out.println("Get local date time: " + getLocalDateTime); 

为什么使用日期和时间两个单独PARSERS是靠不住的:
如上所述,这是一个坏主意,使用日期和时间部分两个独立的解析器。这里的原因:

Date getDate = simpleDateFormatter.parse(utcDate); 
    Date getTime = simpleTimeFormatter.parse(utcTime); 
    //Time zone changed to local here 
    String getLocalDate2 = simpleDateTimeFormatter.format(getDate); 
    String getLocalTime2 = simpleDateTimeFormatter.format(getTime); 
    System.out.println("Get local date2: " + getLocalDate2); 
    System.out.println("Get local time2: " + getLocalTime2); 

    OUTPUT: 
    Get local date2: 10/11/2012 08:00:00 AM 
    Get local time2: 01/01/1970 10:35:10 AM 

我得到了半小时的时差,因为默认的日期01/01/1970Date变量存储时间(第二行)使用。当这被转换为本地时区时,错误发生在格式化程序的基础上,它的转换默认日期为01/01/1970where I live,,时差为+ 7.5小时1970年 - 今天,它是+8小时)。这就是为什么两个单独的解析器是即使得到正确的结果也不可靠,并且您必须始终使用可同时接受日期和时间信息的组合解析器。

+0

感谢您的所有解释。 – Dibyendu

+0

不客气。你可以用绿色的勾号奖励我;)_(是的,我很无耻= P反正它完全取决于你)_ – ADTC

+1

没问题的朋友...... Ur程序也运行得很好。 – Dibyendu