简体   繁体   English

转换GMT日期为IST

[英]Convert GMT date to IST

I am trying to convert GMT time to IST using the following piece of code: 我正在尝试使用以下代码将GMT时间转换为IST:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
 Date date1 = sdf.parse("2015-01-25");
 sdf.setTimeZone(TimeZone.getTimeZone("IST"));
 Log.d("date",sdf.format(date1));

However, 2015-01-24 is being logged out in console.Where am i wrong? 但是,2015-01-24正在控制台中注销。我在哪里错?

Since you are on IST, you have to manually specify GMT. 由于您使用的是IST,因此必须手动指定GMT。 Like this, 像这样,

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));  // set this if your timezone is different

Date date1 = sdf.parse("2015-01-25"); // now time is Sun Jan 25 00:00:00 GMT 2015
sdf.setTimeZone(TimeZone.getTimeZone("IST"));
Log.d("date",sdf.format(date1)); // now time is Sun Jan 25 05:30:00 IST 2015

Since IST = GMT + 05.30, you will get the same date. 由于IST = GMT + 05.30,您将获得相同的日期。

The code snippet converts 2015-01-25 00:00:00 local time (whatever that is - seems to be east of IST) to IST and prints the date part of it. 代码段将2015年1月25日00:00:00的本地时间(无论是什么时间-似乎在IST东部)转换为IST并打印其中的日期部分。

So 2015-01-26 can never be output as IST would have to be at least 24 hours ahead of local time. 因此,永远不会输出2015-01-26,因为IST必须比本地时间至少提前24小时。

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

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