2012-02-24 33 views
6

我写这段代码将当前系统日期和时间转换为其他时区。我没有收到任何错误,但我没有按预期得到我的输出。就像如果我在一个特定的时间执行我的计划。我的输出为::日期和时间转换为其他一些时区在java

印度现在时间是:: Fri Feb 24 16:09:23 IST 2012

在:: 中部标准时间的日期和时间: :根据CST时区Sat Feb 25 03:39:23 IST 2012

与实际时间是::

Friday, 24 February 4:39:16 a.m(GMT - 6:00) 

所以有一些时间差距。我不知道为什么会发生这种情况。任何帮助将不胜感激。该代码::

package MyPackage; 

import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 

import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 

public class Temp2 { 


    public static void main(String[] args) { 

     try { 
      Calendar currentdate = Calendar.getInstance(); 
      String strdate = null; 
      DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
      strdate = formatter.format(currentdate.getTime()); 
      TimeZone obj = TimeZone.getTimeZone("CST"); 

      formatter.setTimeZone(obj); 
      //System.out.println(strdate); 
      //System.out.println(formatter.parse(strdate)); 
      Date theResult = formatter.parse(strdate); 

      System.out.println("The current time in India is :: " +currentdate.getTime()); 

      System.out.println("The date and time in :: "+ obj.getDisplayName() + "is ::" + theResult); 
     } catch (ParseException e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+2

夏令效果? – Nishant 2012-02-24 10:44:59

+1

使用java.util处理时间很快就会变得很麻烦,如果您有一些时间,请查看joda-time API – angryInsomniac 2012-02-24 10:50:34

+0

看看这个http://stackoverflow.com/questions/2356672/date-parsing-formating-with -timezone-and-simpledateformat-problem-around-dst-swi – Pavan 2012-02-24 10:50:36

回答

15

它通过网络。可能已经google了。不管怎么说,这里是你的一个版本(无耻地挑选和here修改):

Calendar calendar = Calendar.getInstance(); 
TimeZone fromTimeZone = calendar.getTimeZone(); 
TimeZone toTimeZone = TimeZone.getTimeZone("CST"); 

calendar.setTimeZone(fromTimeZone); 
calendar.add(Calendar.MILLISECOND, fromTimeZone.getRawOffset() * -1); 
if (fromTimeZone.inDaylightTime(calendar.getTime())) { 
    calendar.add(Calendar.MILLISECOND, calendar.getTimeZone().getDSTSavings() * -1); 
} 

calendar.add(Calendar.MILLISECOND, toTimeZone.getRawOffset()); 
if (toTimeZone.inDaylightTime(calendar.getTime())) { 
    calendar.add(Calendar.MILLISECOND, toTimeZone.getDSTSavings()); 
} 

System.out.println(calendar.getTime()); 
+0

Thanx nishant ..的答案..我得到的时间ryt ..但日期仍然不正确..它给我..25日而不是24日...请如果你能得到那个ryt .. ?? thanx – 2012-02-24 11:16:02

+1

@ user1230183更新了答案。 – Nishant 2012-02-24 11:31:20

+0

Thanx nishant ..问题解决.. :) .. thanx很多.. :) – 2012-02-24 11:38:23

7

你的错误是调用parse而不是format

您可以致电parse从字符串中解析日期,但在您的情况下,您已经有日期并需要使用正确的时区格式化日期。

Calendar currentdate = Calendar.getInstance(); 
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
TimeZone obj = TimeZone.getTimeZone("CST"); 
formatter.setTimeZone(obj); 
System.out.println("Local:: " +currentdate.getTime()); 
System.out.println("CST:: "+ formatter.format(currentdate.getTime()) 

替换你的代码,我希望你会得到你期望的输出。

+0

我试过了..但它给了我一个异常java.lang.IllegalArgumentException:无法格式化给定的对象作为日期 – 2012-02-24 11:17:08

+0

对不起,我需要提供的不止一个行 - 答案更新 – 2012-02-24 11:18:25

3

处理Java中的日期在我的日常工作是一个不平凡的任务。我建议你使用Joda-Time来简化我们的编码时间,你不必“重新发明轮子”。

+0

thanx达里奥..对于这个建议..肯定会把我的代码这样..看看是否有用... – 2012-02-24 11:20:12

+0

Joda-时间主页已经移动。所以我更新了链接。 – naXa 2017-12-15 09:40:28

0

问题是当您打印日期obj时,它会调用toString方法,并且它会在您的机器默认时区中打印。试试这段代码,看看不同之处。

Calendar currentdate = Calendar.getInstance(); 
String strdate = null; 
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ssz"); 
strdate = formatter.format(currentdate.getTime()); 
System.out.println("strdate=>" + strdate); 
TimeZone obj = TimeZone.getTimeZone("CST"); 

formatter.setTimeZone(obj); 
strdate = formatter.format(currentdate.getTime()); 
Date theResult = formatter.parse(strdate); 

System.out.println("The current time in India is :: " +currentdate.getTime()); 

System.out.println("The date and time in :: " + obj.getDisplayName() + "is ::" + theResult); 
System.out.println("The date and time in :: " + obj.getDisplayName() + "is ::" + strdate); 
+0

Thanx abhutra ..我gt从你的代码ryt ... :) – 2012-02-24 11:41:45

+0

你是否得到了你正在做的错误..? – 2012-02-24 11:54:30

+0

嗯,不完全...但是你说什么,似乎我打印的日期没有转换到CST时区..它将不胜感激,如果你会解释我哪里出错了。?? – 2012-02-24 12:03:37

0

尝试是这样的

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(); 
+1

这很好解释你的答案。 – 2014-10-14 12:00:52

0

你可以用“CST6CDT” 因为在一些国家,它们遵循CDT在夏季和CST冬季

public static String getDateInCST() { 
      Calendar calendar = Calendar.getInstance(); 
      DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); 
      formatter.setTimeZone(TimeZone.getTimeZone("CST6CDT")); 
      String strdate = formatter.format(calendar.getTime()); 
      TimeZone.getAvailableIDs(); 
      return strdate; 
    } 
相关问题