我正在尝试将 GMT 转换为 IST。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSS");
Date c= sdf.parse("2017-03-31T10:38:14.4723017Z");
Date date = new Date();
DateFormat istFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone istTime = TimeZone.getTimeZone("IST");
istFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(istTime);
System.out.println("GMT Time: " + istFormat.format(c));
System.out.println("IST Time: " + gmtFormat.format(c));
我的输出是
GMT Time: 31/3/17 6:26 AM
IST Time: 31/3/17 11:56 AM
但我的实际输出应该是
GMT Time: 31/3/17 5:08 AM
IST Time: 31/3/17 10:38 AM
我的代码有什么问题?
最佳答案
毫秒 (SSS) 只能是三位数。不止于此,日期会滚动 - 例如10:38:14.1000 变为 10:38:15.000。添加几百万毫秒...您会得到现在看到的行为。
试试这个。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
Date c = sdf.parse("2017-03-31T10:38:14.472Z");
System.out.println(c);
DateFormat istFormat = new SimpleDateFormat();
DateFormat gmtFormat = new SimpleDateFormat();
TimeZone gmtTime = TimeZone.getTimeZone("GMT");
TimeZone istTime = TimeZone.getTimeZone("IST");
istFormat.setTimeZone(gmtTime);
gmtFormat.setTimeZone(istTime);
System.out.println("GMT Time: " + istFormat.format(c));
System.out.println("IST Time: " + gmtFormat.format(c));
关于java - yyyy-MM-dd'T'HH :mm:ss. SSSSSSS'Z' 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000755/