title: How can I convert from a String to Epoch microseconds?
body: <p>I have a String of this format: "2019-08-17T09:51:41.775+00:00". I need to convert it into Epoch microseconds, but my conversion is always an hour off.</p>
  
  <p>This is my code at the moment: </p>
  
  <pre><code>String timestamp = "2019-08-17T09:51:41.775+00:00"
  ZonedDateTime date = ZonedDateTime.parse(timestamp)
  Long epoch = date.toInstant().toEpochMilli() * 1000
  </code></pre>
  
  <p>So when I run this code, I get the result 1566035501775000. Now if I put 1566035501775 in a converter like this one: <a href="https://www.freeformatter.com/epoch-timestamp-to-date-converter.html" rel="nofollow noreferrer">https://www.freeformatter.com/epoch-timestamp-to-date-converter.html</a> to reverse it, I get this date: 8/17/2019, 10:51:41 AM. </p>
  
  <p>Why is it an hour off and how can I change that?</p>
  
  <p>I also tried it with a DateTimeFormatter:</p>
  
  <pre><code>DateTimeFormatter pat = DateTimeFormatter.ofPattern("YYYY-MM-dd'T'hh:mm:ss.SSSXXX")
  Instant.from(pat.parse(timestamp))
  </code></pre>
  
  <p>But this gives me this exception:</p>
  
  <pre><code>java.time.DateTimeException: Unable to obtain Instant from TemporalAccessor: {SecondOfMinute=41, MicroOfSecond=775000, DayOfMonth=17, HourOfAmPm=9, NanoOfSecond=775000000, MonthOfYear=8, OffsetSeconds=0, MinuteOfHour=51, MilliOfSecond=775, WeekBasedYear[WeekFields[SUNDAY,1]]=2019},ISO of type java.time.format.Parsed
  
      at java.time.Instant.from(Instant.java:378)
      at com.amazon.hailstonetracing.controller.chrome.WorkflowEventMapperTest.test(WorkflowEventMapperTest.kt:114)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
  Caused by: java.time.temporal.UnsupportedTemporalTypeException: Unsupported field: InstantSeconds
      at java.time.format.Parsed.getLong(Parsed.java:203)
      at java.time.Instant.from(Instant.java:373)
      ... 24 more
  </code></pre>
  
  <p>After researching I presume this is because I didn't define a ZoneId? I only have the offset from the timestamp String. But I get the same result if I specify <code>.withZone(ZoneId.of("UTC"))</code> on the <code>DateTimeFormatter</code>.</p>
  
  <p>Thanks for your help!</p>