yyyy-MM-dd'T'HH:mm:ss.SSSSSSS'Z'格式

kus*_*shi 0 java datetime-conversion

我正在尝试将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));
Run Code Online (Sandbox Code Playgroud)

我的输出是

GMT Time: 31/3/17 6:26 AM
IST Time: 31/3/17 11:56 AM
Run Code Online (Sandbox Code Playgroud)

但我的实际输出应该是

GMT Time: 31/3/17 5:08 AM
IST Time: 31/3/17 10:38 AM
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

Ria*_*Nel 5

毫秒(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));
Run Code Online (Sandbox Code Playgroud)