我在两种不同格式之间转换Date对象时遇到了问题。
这些是dateFormats
private final SimpleDateFormat sdf_input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ", Locale.ITALY);
private final SimpleDateFormat sdf_output = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.ITALY);
这是我用来从输入字符串转换为输出字符串
的代码Date tmp = sdf_input.parse(timestamp);
this.timestamp = sdf_output.format(tmp);
System.out.println("CONVERSIONE: "+timestamp+" -> "+this.timestamp);
这是执行的输出
CONVERSIONE: 2016-06-23T18:01:59.652546+0200 -> 2016-06-23T18:12:51Z
CONVERSIONE: 2016-06-23T18:01:59.652612+0200 -> 2016-06-23T18:12:51Z
CONVERSIONE: 2016-06-23T18:01:59.652452+0200 -> 2016-06-23T18:12:51Z
CONVERSIONE: 2016-06-23T18:01:59.867481+0200 -> 2016-06-23T18:16:26Z
CONVERSIONE: 2016-06-23T18:02:00.102929+0200 -> 2016-06-23T18:03:42Z
CONVERSIONE: 2016-06-23T18:02:00.102861+0200 -> 2016-06-23T18:03:42Z
CONVERSIONE: 2016-06-23T18:02:00.102660+0200 -> 2016-06-23T18:03:42Z
CONVERSIONE: 2016-06-23T18:02:00.102995+0200 -> 2016-06-23T18:03:42Z
CONVERSIONE: 2016-06-23T18:02:00.102788+0200 -> 2016-06-23T18:03:42Z
CONVERSIONE: 2016-06-23T18:02:00.103056+0200 -> 2016-06-23T18:03:43Z
CONVERSIONE: 2016-06-23T18:02:00.209643+0200 -> 2016-06-23T18:05:29Z
CONVERSIONE: 2016-06-23T18:02:00.232735+0200 -> 2016-06-23T18:05:52Z
CONVERSIONE: 2016-06-23T18:02:00.231628+0200 -> 2016-06-23T18:05:51Z
CONVERSIONE: 2016-06-23T18:02:00.232609+0200 -> 2016-06-23T18:05:52Z
CONVERSIONE: 2016-06-23T18:02:00.232808+0200 -> 2016-06-23T18:05:52Z
CONVERSIONE: 2016-06-23T18:02:00.231775+0200 -> 2016-06-23T18:05:51Z
CONVERSIONE: 2016-06-23T18:02:00.280670+0200 -> 2016-06-23T18:06:40Z
CONVERSIONE: 2016-06-23T18:02:00.281655+0200 -> 2016-06-23T18:06:41Z
CONVERSIONE: 2016-06-23T18:02:00.282772+0200 -> 2016-06-23T18:06:42Z
CONVERSIONE: 2016-06-23T18:02:00.281788+0200 -> 2016-06-23T18:06:41Z
CONVERSIONE: 2016-06-23T18:02:00.282871+0200 -> 2016-06-23T18:06:42Z
CONVERSIONE: 2016-06-23T18:02:00.282629+0200 -> 2016-06-23T18:06:42Z
CONVERSIONE: 2016-06-23T18:02:00.505474+0200 -> 2016-06-23T18:10:25Z
CONVERSIONE: 2016-06-23T18:02:00.505653+0200 -> 2016-06-23T18:10:25Z
CONVERSIONE: 2016-06-23T18:02:00.506724+0200 -> 2016-06-23T18:10:26Z
CONVERSIONE: 2016-06-23T18:02:00.506792+0200 -> 2016-06-23T18:10:26Z
CONVERSIONE: 2016-06-23T18:02:00.505589+0200 -> 2016-06-23T18:10:25Z
CONVERSIONE: 2016-06-23T18:02:00.506578+0200 -> 2016-06-23T18:10:26Z
CONVERSIONE: 2016-06-23T18:02:04.561530+0200 -> 2016-06-23T18:11:25Z
CONVERSIONE: 2016-06-23T18:02:05.845597+0200 -> 2016-06-23T18:16:10Z
CONVERSIONE: 2016-06-23T18:02:09.981509+0200 -> 2016-06-23T18:18:30Z
CONVERSIONE: 2016-06-23T18:02:12.329653+0200 -> 2016-06-23T18:07:41Z
CONVERSIONE: 2016-06-23T18:02:14.305630+0200 -> 2016-06-23T18:07:19Z
CONVERSIONE: 2016-06-23T18:02:14.305480+0200 -> 2016-06-23T18:07:19Z
CONVERSIONE: 2016-06-23T18:02:14.305565+0200 -> 2016-06-23T18:07:19Z
CONVERSIONE: 2016-06-23T18:02:15.364254+0200 -> 2016-06-23T18:08:19Z
CONVERSIONE: 2016-06-23T18:02:16.243355+0200 -> 2016-06-23T18:06:19Z
CONVERSIONE: 2016-06-23T18:02:16.243537+0200 -> 2016-06-23T18:06:19Z
CONVERSIONE: 2016-06-23T18:02:16.243424+0200 -> 2016-06-23T18:06:19Z
CONVERSIONE: 2016-06-23T18:02:16.243482+0200 -> 2016-06-23T18:06:19Z
CONVERSIONE: 2016-06-23T18:02:16.473340+0200 -> 2016-06-23T18:10:09Z
CONVERSIONE: 2016-06-23T18:02:16.473424+0200 -> 2016-06-23T18:10:09Z
CONVERSIONE: 2016-06-23T18:02:16.533370+0200 -> 2016-06-23T18:11:09Z
CONVERSIONE: 2016-06-23T18:02:19.046342+0200 -> 2016-06-23T18:03:05Z
CONVERSIONE: 2016-06-23T18:02:19.719421+0200 -> 2016-06-23T18:14:18Z
CONVERSIONE: 2016-06-23T18:02:19.719319+0200 -> 2016-06-23T18:14:18Z
CONVERSIONE: 2016-06-23T18:02:19.719555+0200 -> 2016-06-23T18:14:18Z
CONVERSIONE: 2016-06-23T18:02:20.797860+0200 -> 2016-06-23T18:15:37Z
CONVERSIONE: 2016-06-23T18:02:25.528294+0200 -> 2016-06-23T18:11:13Z
CONVERSIONE: 2016-06-23T18:02:25.528375+0200 -> 2016-06-23T18:11:13Z
CONVERSIONE: 2016-06-23T18:02:25.528439+0200 -> 2016-06-23T18:11:13Z
CONVERSIONE: 2016-06-23T18:02:25.528505+0200 -> 2016-06-23T18:11:13Z
CONVERSIONE: 2016-06-23T18:02:25.528592+0200 -> 2016-06-23T18:11:13Z
CONVERSIONE: 2016-06-23T18:02:25.529306+0200 -> 2016-06-23T18:11:14Z
CONVERSIONE: 2016-06-23T18:02:26.139847+0200 -> 2016-06-23T18:04:45Z
CONVERSIONE: 2016-06-23T18:02:26.257798+0200 -> 2016-06-23T18:06:43Z
CONVERSIONE: 2016-06-23T18:02:28.101521+0200 -> 2016-06-23T18:04:09Z
CONVERSIONE: 2016-06-23T18:02:28.432787+0200 -> 2016-06-23T18:09:40Z
CONVERSIONE: 2016-06-23T18:02:28.684613+0200 -> 2016-06-23T18:13:52Z
CONVERSIONE: 2016-06-23T18:02:28.765876+0200 -> 2016-06-23T18:15:13Z
CONVERSIONE: 2016-06-23T18:02:28.846772+0200 -> 2016-06-23T18:16:34Z
CONVERSIONE: 2016-06-23T18:02:29.341246+0200 -> 2016-06-23T18:08:10Z
CONVERSIONE: 2016-06-23T18:02:29.341335+0200 -> 2016-06-23T18:08:10Z
CONVERSIONE: 2016-06-23T18:02:29.342104+0200 -> 2016-06-23T18:08:11Z
CONVERSIONE: 2016-06-23T18:02:29.351248+0200 -> 2016-06-23T18:08:20Z
CONVERSIONE: 2016-06-23T18:02:29.351337+0200 -> 2016-06-23T18:08:20Z
CONVERSIONE: 2016-06-23T18:02:29.351405+0200 -> 2016-06-23T18:08:20Z
CONVERSIONE: 2016-06-23T18:02:30.498554+0200 -> 2016-06-23T18:10:48Z
CONVERSIONE: 2016-06-23T18:02:31.512218+0200 -> 2016-06-23T18:11:03Z
CONVERSIONE: 2016-06-23T18:02:35.997197+0200 -> 2016-06-23T18:19:12Z
CONVERSIONE: 2016-06-23T18:02:35.997292+0200 -> 2016-06-23T18:19:12Z
CONVERSIONE: 2016-06-23T18:02:35.997416+0200 -> 2016-06-23T18:19:12Z
CONVERSIONE: 2016-06-23T18:02:35.997355+0200 -> 2016-06-23T18:19:12Z
CONVERSIONE: 2016-06-23T18:02:36.001226+0200 -> 2016-06-23T18:02:37Z
CONVERSIONE: 2016-06-23T18:02:36.001360+0200 -> 2016-06-23T18:02:37Z
CONVERSIONE: 2016-06-23T18:02:36.860512+0200 -> 2016-06-23T18:16:56Z
CONVERSIONE: 2016-06-23T18:02:38.025692+0200 -> 2016-06-23T18:03:03Z
CONVERSIONE: 2016-06-23T18:02:38.045314+0200 -> 2016-06-23T18:03:23Z
CONVERSIONE: 2016-06-23T18:02:42.099268+0200 -> 2016-06-23T18:04:21Z
CONVERSIONE: 2016-06-23T18:02:42.099640+0200 -> 2016-06-23T18:04:21Z
CONVERSIONE: 2016-06-23T18:02:42.099489+0200 -> 2016-06-23T18:04:21Z
CONVERSIONE: 2016-06-23T18:02:42.099569+0200 -> 2016-06-23T18:04:21Z
CONVERSIONE: 2016-06-23T18:02:42.109191+0200 -> 2016-06-23T18:04:31Z
CONVERSIONE: 2016-06-23T18:02:42.109270+0200 -> 2016-06-23T18:04:31Z
CONVERSIONE: 2016-06-23T18:02:42.204322+0200 -> 2016-06-23T18:06:06Z
CONVERSIONE: 2016-06-23T18:02:43.480420+0200 -> 2016-06-23T18:10:43Z
CONVERSIONE: 2016-06-23T18:02:44.327143+0200 -> 2016-06-23T18:08:11Z
CONVERSIONE: 2016-06-23T18:02:44.680299+0200 -> 2016-06-23T18:14:04Z
CONVERSIONE: 2016-06-23T18:02:43.480171+0200 -> 2016-06-23T18:10:43Z
CONVERSIONE: 2016-06-23T18:02:43.480259+0200 -> 2016-06-23T18:10:43Z
CONVERSIONE: 2016-06-23T18:02:43.480315+0200 -> 2016-06-23T18:10:43Z
CONVERSIONE: 2016-06-23T18:02:43.480366+0200 -> 2016-06-23T18:10:43Z
CONVERSIONE: 2016-06-23T18:02:43.790065+0200 -> 2016-06-23T18:15:53Z
CONVERSIONE: 2016-06-23T18:02:44.030157+0200 -> 2016-06-23T18:03:14Z
CONVERSIONE: 2016-06-23T18:02:44.030273+0200 -> 2016-06-23T18:03:14Z
CONVERSIONE: 2016-06-23T18:02:44.030334+0200 -> 2016-06-23T18:03:14Z
CONVERSIONE: 2016-06-23T18:02:44.030410+0200 -> 2016-06-23T18:03:14Z
CONVERSIONE: 2016-06-23T18:02:44.330137+0200 -> 2016-06-23T18:08:14Z
CONVERSIONE: 2016-06-23T18:02:44.680127+0200 -> 2016-06-23T18:14:04Z
CONVERSIONE: 2016-06-23T18:02:44.680189+0200 -> 2016-06-23T18:14:04Z
CONVERSIONE: 2016-06-23T18:02:44.680242+0200 -> 2016-06-23T18:14:04Z
CONVERSIONE: 2016-06-23T18:02:44.680355+0200 -> 2016-06-23T18:14:04Z
CONVERSIONE: 2016-06-23T18:02:44.680411+0200 -> 2016-06-23T18:14:04Z
CONVERSIONE: 2016-06-23T18:02:46.404120+0200 -> 2016-06-23T18:09:30Z
CONVERSIONE: 2016-06-23T18:02:46.404204+0200 -> 2016-06-23T18:09:30Z
CONVERSIONE: 2016-06-23T18:02:46.410121+0200 -> 2016-06-23T18:09:36Z
CONVERSIONE: 2016-06-23T18:02:46.706104+0200 -> 2016-06-23T18:14:32Z
CONVERSIONE: 2016-06-23T18:02:47.300106+0200 -> 2016-06-23T18:07:47Z
CONVERSIONE: 2016-06-23T18:02:47.300187+0200 -> 2016-06-23T18:07:47Z
CONVERSIONE: 2016-06-23T18:02:47.325128+0200 -> 2016-06-23T18:08:12Z
CONVERSIONE: 2016-06-23T18:02:47.547784+0200 -> 2016-06-23T18:11:54Z
CONVERSIONE: 2016-06-23T18:02:47.563866+0200 -> 2016-06-23T18:12:10Z
CONVERSIONE: 2016-06-23T18:02:48.125137+0200 -> 2016-06-23T18:04:53Z
CONVERSIONE: 2016-06-23T18:02:48.125343+0200 -> 2016-06-23T18:04:53Z
CONVERSIONE: 2016-06-23T18:02:48.125249+0200 -> 2016-06-23T18:04:53Z
CONVERSIONE: 2016-06-23T18:02:48.129176+0200 -> 2016-06-23T18:04:57Z
CONVERSIONE: 2016-06-23T18:02:48.129336+0200 -> 2016-06-23T18:04:57Z
CONVERSIONE: 2016-06-23T18:02:48.129404+0200 -> 2016-06-23T18:04:57Z
CONVERSIONE: 2016-06-23T18:02:48.425121+0200 -> 2016-06-23T18:09:53Z
CONVERSIONE: 2016-06-23T18:02:48.425248+0200 -> 2016-06-23T18:09:53Z
CONVERSIONE: 2016-06-23T18:02:48.429186+0200 -> 2016-06-23T18:09:57Z
CONVERSIONE: 2016-06-23T18:02:48.435138+0200 -> 2016-06-23T18:10:03Z
CONVERSIONE: 2016-06-23T18:02:48.439145+0200 -> 2016-06-23T18:10:07Z
CONVERSIONE: 2016-06-23T18:02:48.439290+0200 -> 2016-06-23T18:10:07Z
CONVERSIONE: 2016-06-23T18:02:49.505166+0200 -> 2016-06-23T18:11:14Z
CONVERSIONE: 2016-06-23T18:02:49.505236+0200 -> 2016-06-23T18:11:14Z
CONVERSIONE: 2016-06-23T18:02:49.506094+0200 -> 2016-06-23T18:11:15Z
CONVERSIONE: 2016-06-23T18:02:49.510105+0200 -> 2016-06-23T18:11:19Z
CONVERSIONE: 2016-06-23T18:02:49.508244+0200 -> 2016-06-23T18:11:17Z
CONVERSIONE: 2016-06-23T18:02:49.511108+0200 -> 2016-06-23T18:11:20Z
CONVERSIONE: 2016-06-23T18:02:49.895285+0200 -> 2016-06-23T18:17:44Z
CONVERSIONE: 2016-06-23T18:02:49.895104+0200 -> 2016-06-23T18:17:44Z
CONVERSIONE: 2016-06-23T18:02:49.895219+0200 -> 2016-06-23T18:17:44Z
CONVERSIONE: 2016-06-23T18:02:49.898104+0200 -> 2016-06-23T18:17:47Z
CONVERSIONE: 2016-06-23T18:02:49.898256+0200 -> 2016-06-23T18:17:47Z
CONVERSIONE: 2016-06-23T18:02:49.898327+0200 -> 2016-06-23T18:17:47Z
CONVERSIONE: 2016-06-23T18:02:50.829374+0200 -> 2016-06-23T18:16:39Z
CONVERSIONE: 2016-06-23T18:02:50.829142+0200 -> 2016-06-23T18:16:39Z
CONVERSIONE: 2016-06-23T18:02:50.829249+0200 -> 2016-06-23T18:16:39Z
CONVERSIONE: 2016-06-23T18:02:50.829315+0200 -> 2016-06-23T18:16:39Z
CONVERSIONE: 2016-06-23T18:02:50.829434+0200 -> 2016-06-23T18:16:39Z
CONVERSIONE: 2016-06-23T18:02:50.829495+0200 -> 2016-06-23T18:16:39Z
CONVERSIONE: 2016-06-23T18:02:52.227234+0200 -> 2016-06-23T18:06:39Z
CONVERSIONE: 2016-06-23T18:02:52.329286+0200 -> 2016-06-23T18:08:21Z
CONVERSIONE: 2016-06-23T18:02:52.357248+0200 -> 2016-06-23T18:08:49Z
CONVERSIONE: 2016-06-23T18:02:52.389289+0200 -> 2016-06-23T18:09:21Z
CONVERSIONE: 2016-06-23T18:02:52.414304+0200 -> 2016-06-23T18:09:46Z
CONVERSIONE: 2016-06-23T18:02:52.414419+0200 -> 2016-06-23T18:09:46Z
CONVERSIONE: 2016-06-23T18:02:52.460243+0200 -> 2016-06-23T18:10:32Z
CONVERSIONE: 2016-06-23T18:02:52.460380+0200 -> 2016-06-23T18:10:32Z
CONVERSIONE: 2016-06-23T18:02:52.460454+0200 -> 2016-06-23T18:10:32Z
CONVERSIONE: 2016-06-23T18:02:52.568355+0200 -> 2016-06-23T18:12:20Z
CONVERSIONE: 2016-06-23T18:02:52.568231+0200 -> 2016-06-23T18:12:20Z
CONVERSIONE: 2016-06-23T18:02:52.569322+0200 -> 2016-06-23T18:12:21Z
CONVERSIONE: 2016-06-23T18:02:52.794367+0200 -> 2016-06-23T18:16:06Z
CONVERSIONE: 2016-06-23T18:02:52.794137+0200 -> 2016-06-23T18:16:06Z
CONVERSIONE: 2016-06-23T18:02:52.794294+0200 -> 2016-06-23T18:16:06Z
CONVERSIONE: 2016-06-23T18:02:52.917557+0200 -> 2016-06-23T18:18:09Z
CONVERSIONE: 2016-06-23T18:02:53.080141+0200 -> 2016-06-23T18:04:13Z
CONVERSIONE: 2016-06-23T18:02:53.080370+0200 -> 2016-06-23T18:04:13Z
CONVERSIONE: 2016-06-23T18:02:53.080448+0200 -> 2016-06-23T18:04:13Z
CONVERSIONE: 2016-06-23T18:02:53.417696+0200 -> 2016-06-23T18:09:50Z
CONVERSIONE: 2016-06-23T18:02:53.529258+0200 -> 2016-06-23T18:11:42Z
CONVERSIONE: 2016-06-23T18:02:53.529127+0200 -> 2016-06-23T18:11:42Z
CONVERSIONE: 2016-06-23T18:02:53.530181+0200 -> 2016-06-23T18:11:43Z
CONVERSIONE: 2016-06-23T18:02:53.530353+0200 -> 2016-06-23T18:11:43Z
CONVERSIONE: 2016-06-23T18:02:53.530434+0200 -> 2016-06-23T18:11:43Z
CONVERSIONE: 2016-06-23T18:02:53.539135+0200 -> 2016-06-23T18:11:52Z
CONVERSIONE: 2016-06-23T18:02:55.632057+0200 -> 2016-06-23T18:13:27Z
CONVERSIONE: 2016-06-23T18:02:55.632159+0200 -> 2016-06-23T18:13:27Z
CONVERSIONE: 2016-06-23T18:02:55.642357+0200 -> 2016-06-23T18:13:37Z
CONVERSIONE: 2016-06-23T18:02:55.642070+0200 -> 2016-06-23T18:13:37Z
CONVERSIONE: 2016-06-23T18:02:55.642217+0200 -> 2016-06-23T18:13:37Z
CONVERSIONE: 2016-06-23T18:02:55.642285+0200 -> 2016-06-23T18:13:37Z
CONVERSIONE: 2016-06-23T18:02:55.992441+0200 -> 2016-06-23T18:19:27Z
CONVERSIONE: 2016-06-23T18:02:55.992227+0200 -> 2016-06-23T18:19:27Z
CONVERSIONE: 2016-06-23T18:02:55.992364+0200 -> 2016-06-23T18:19:27Z
CONVERSIONE: 2016-06-23T18:02:55.992525+0200 -> 2016-06-23T18:19:27Z
CONVERSIONE: 2016-06-23T18:02:55.993183+0200 -> 2016-06-23T18:19:28Z
CONVERSIONE: 2016-06-23T18:02:55.993304+0200 -> 2016-06-23T18:19:28Z
CONVERSIONE: 2016-06-23T18:02:56.092556+0200 -> 2016-06-23T18:04:28Z
CONVERSIONE: 2016-06-23T18:02:56.092236+0200 -> 2016-06-23T18:04:28Z
CONVERSIONE: 2016-06-23T18:02:56.092473+0200 -> 2016-06-23T18:04:28Z
CONVERSIONE: 2016-06-23T18:02:56.092630+0200 -> 2016-06-23T18:04:28Z
CONVERSIONE: 2016-06-23T18:02:56.092697+0200 -> 2016-06-23T18:04:28Z
CONVERSIONE: 2016-06-23T18:02:56.092764+0200 -> 2016-06-23T18:04:28Z
CONVERSIONE: 2016-06-23T18:02:56.197311+0200 -> 2016-06-23T18:06:13Z
CONVERSIONE: 2016-06-23T18:02:56.196207+0200 -> 2016-06-23T18:06:12Z
CONVERSIONE: 2016-06-23T18:02:56.196353+0200 -> 2016-06-23T18:06:12Z
CONVERSIONE: 2016-06-23T18:02:56.196427+0200 -> 2016-06-23T18:06:12Z
CONVERSIONE: 2016-06-23T18:02:56.196510+0200 -> 2016-06-23T18:06:12Z
CONVERSIONE: 2016-06-23T18:02:56.197182+0200 -> 2016-06-23T18:06:13Z
CONVERSIONE: 2016-06-23T18:02:56.417433+0200 -> 2016-06-23T18:09:53Z
CONVERSIONE: 2016-06-23T18:02:56.416196+0200 -> 2016-06-23T18:09:52Z
CONVERSIONE: 2016-06-23T18:02:56.417204+0200 -> 2016-06-23T18:09:53Z
CONVERSIONE: 2016-06-23T18:02:56.417360+0200 -> 2016-06-23T18:09:53Z
CONVERSIONE: 2016-06-23T18:02:56.418194+0200 -> 2016-06-23T18:09:54Z
CONVERSIONE: 2016-06-23T18:02:56.418337+0200 -> 2016-06-23T18:09:54Z
正如您所看到的那样,我有一个问题,但我不明白这个问题的原因。 你们中有人可以帮帮我吗?
非常感谢!
答案 0 :(得分:5)
问题是毫秒,你只能有三个输入数字。
CONVERSIONE: 2016-06-23T18:02:00.232808+0200 -> 2016-06-23T18:05:52Z
程序认为有232808毫秒~232秒= 3分52秒,这就是你(错误地)得到的。
如何解决您的问题?使用Java 8中包含的java.time包中的类。它支持精度高达纳秒。来自LocalTime的文档:
LocalTime是一个表示时间的不可变日期时间对象, 通常被视为小时 - 秒 - 秒。时间表示为纳秒 精确。例如,可以存储值“13:45.30.123456789” 在LocalTime中。
答案 1 :(得分:0)
从上述链接中可以看到Java 8的Java文档,告诉我们毫秒组件的长度为3个整数:SimpleDateFormat
因此,在输入中,您必须为毫秒组件仅输入3个整数。