How to store a timestamp with UTC timezone offset in Java 6 without modifying anything?

2.3k views Asked by At

I'm terribly sorry if this had been asked before, I've tried searching for something that answers my questions but could find none.

The issue is this: Let's say I have a string that looks like this -> 2017-01-05-04:44:43-06:00 (format being yyyy-MM-dd-hh:mm:ss+/-UTC) Now my goal is to store it as it is in the Oracle database including the offset (as I understand I need to define the column as TIMESTAMP WITH TIME ZONE).

So how do I go about capturing the timestamp with the offset into a Java date and time object? I'm not looking to convert the times to UTC, I just want to store it as is and persist that into DB.

Please note that we are on Java 6 so I cannot use java.time package, neither can I use Joda Time. So any solution outside of these would be really appreciated.

Thanks.

1

There are 1 answers

0
Basil Bourque On BEST ANSWER

java.time

See the ThreeTen-Backport project for a back-port of much of the java.time functionality to Java 6 and 7 (java.time is built into Java 8 and later). The API nearly matches so that when you eventually migrate to Java 8 or later, you’ll need do little more than change your import statements.

The Joda-Time project is now in maintenance mode though actively maintained. The sponsors advise moving to the java.time classes.

ISO 8601

Replace that hyphen in the middle of your input string with a T to comply with the standard ISO 8601 formats. These standard formats are used by default in the java.time classes for parsing and generating string representations of date-time values.

OffsetDateTime odt = OffsetDateTime.parse( "2017-01-05T04:44:43-06:00" );

Search Stack Overflow

I'm terribly sorry if this had been asked before

Yes, your questions are all duplicates. Please search Stack Overflow for existing answers to all your issues:

  • backport of java.time, ThreeTen-Backport
  • Parsing strings containing an offset-from-UTC, OffsetDateTime class
  • exchanging date-time data with databases and Oracle in particular, JDBC 4.2 for directly exchanging java.time objects, or else calling new methods added to the old classes for converting to/from java.time to java.sql.Timestamp, or else on pre-Java-8 convert to Timestamp by count-from-epoch numbers.
  • Learning just how bad the old legacy date-time classes are, and you absolutely should go to the bother of adding the java.time backport library to your project.

Tip: Do not search from within Stack Overflow website. Their search engine is biased towards Questions while you want to search within Answers. Use site:stackoverflow.com in DuckDuckGo, Google, or Bing.


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.

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

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

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.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.