0
SimpleDateFormat default_date_formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS");
default_date_formatter.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(default_date_formatter.parse("2010-01-01T00:43:54.776000"));

gives Thu Dec 31 16:56:50 PST 2009 as the result instead of Thu Dec 31 16:43:54 PST 2009

The times are off 16:56:50 v/s 16:43:54

Thirumal
  • 8,280
  • 11
  • 53
  • 103
  • 2
    You should include the code which actually prints the `Thu Dec 31` timestamp to the console, but keep in mind that `System.out` will convert a `java.util.Date` to the whatever the time zone is of your JVM. – Tim Biegeleisen Jul 20 '20 at 06:11
  • Sure. But still the times are off considering different time zones – pandas_noob Jul 20 '20 at 06:15
  • 1
    You should follow the Java Naming Conventions: variabele names and method names should be written in camelCase and class names in PascalCase. – MC Emperor Jul 20 '20 at 21:38

1 Answers1

3

tl;dr

LocalDateTime                             // Represent a date wih time-of-day but lacking he context of a time zone or offset.
.parse( "2010-01-01T00:43:54.776000" )    // Parse your standard ISO 8691 text with microseconds. Returns a `LocalDateTime` object. This is *not* a moment, *not* a point on the timeline.
.atOffset( ZoneOffset.UTC )               // Apply an offset to determine a moment. Returns an `OffsetDateTime` object.
.toString()                               // Generate text in standard ISO 8691 format.

java.time

Stop using terrible date-time classes that were supplanted years ago by the modern java.time classes.

Always search Stack Overflow thoroughly before posting. All this has been covered many times already, so I’ll be brief.

LocalDateTime ldt = LocalDateTime.parse( "2010-01-01T00:43:54.776000" ) ;
OffsetDateTime odt = ldt.atOffset( ZoneOffset.UTC ) ;

Generate text in the standard ISO 8601 format.

String output = odt.toString() ;

Table of all date-time types in Java, both modern and legacy


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes. Hibernate 5 & JPA 2.2 support java.time.

Where to obtain the java.time classes?

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thank you. I'm new to Java and didn't know that SimpleDateFormat was deemed to be terrible – pandas_noob Jul 20 '20 at 07:07
  • In the ideal world newcomers to Java should have no chance of stumbling upon `SimpleDateFormat`. Unfortunately it is still mentioned in many places and without the warning that it is terrible and long outdated. – Ole V.V. Jul 21 '20 at 11:22