CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Possible Plagiarism

Reposted on 2021-02-24
by Basil Bourque

Original Post

Original - Posted on 2016-04-22
by Basil Bourque



            
Present in both answers; Present only in the new answer; Present only in the old answer;

# *java.time*
You said:
> need to switch them with new Date and Time API in Java 8
Yes, absolutely. The legacy `Date`, `Calendar`, and `SimpleDateFormat` classes are *terrible*: confusing and flawed.
You said:
>When I retrieve my date from database(it is SimpleDateFormat and I cant change it)
If your date-time value is properly stored in the database in a date-time column, it does not have a "format" because it is not text.
## Zoned
If stored in a column of a type akin to the SQL-standard `TIMESTAMP WITH TIME ZONE`, retrieve as a [`OffsetDateTime`][1].
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
You said:
>I want to add some hours(for example 4 PM)
That is a contradiction. Do you want to [add four hours][2]?
OffsetDateTime odtFourHoursLater = odt.plusHours( 4 ) ;
Or do you want to set the time-of-day to 4 PM?
OffsetDateTime odtSetToFourPm = odt.with( LocalTime.of ( 16 , 0 ) ) ;
You said:
> that result I need to be in time zone which user selected from the dropdown(Lets say America/Caracas),
The `OffsetDateTime` class supported by JDBC 4.2 represents a moment as seen through an offset-from-UTC, a number of hours-minutes-seconds. Most databases will deliver this object to you with an offset of zero hours-minutes-seconds.
Understand that a time zone is not an offset. A time zone is a named history of the past, present, and future changes to the offset used by the people of a particular region. A time zone has a name in the format of `Continent/Region`.
To represent a moment as seen in a time zone rather than an offset, use `ZonedDateTime` class.
ZoneId zoneId = ZoneId.of( "America/Caracas" ) ; ZonedDateTime zdt = odt.atZoneSameInstant( zoneId ) ;
I cannot provide more specific code examples because your question is confused. Your exact goal is not clear.
## Not zoned
If stored in a column of a type akin to the SQL-standard `TIMESTAMP WITHOUT TIME ZONE`, retrieve as a `LocalDateTime`.
LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;
This type cannot represent a moment, a point on the timeline. Lacking the context of a time zone or offset-from-UTC means we do not know if, for example, noon on a particular date is noon in Tokyo Japan, noon in Toulouse France, or noon in Toledo Ohio US — all different moments, several hours apart. Therefore, this type cannot be adjusted into an arbitrary time zone.




----------
# About *java.time*
The [*java.time*](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) framework is built into Java 8 and later. These classes supplant the troublesome old [legacy](https://en.wikipedia.org/wiki/Legacy_system) date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), & [`SimpleDateFormat`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html).
To learn more, see the [*Oracle Tutorial*](http://docs.oracle.com/javase/tutorial/datetime/TOC.html). And search Stack Overflow for many examples and explanations. Specification is [JSR 310](https://jcp.org/en/jsr/detail?id=310).
The [*Joda-Time*](http://www.joda.org/joda-time/) project, now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advises migration to the [java.time](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) classes.
You may exchange *java.time* objects directly with your database. Use a [JDBC driver](https://en.wikipedia.org/wiki/JDBC_driver) compliant with [JDBC 4.2](http://openjdk.java.net/jeps/170) 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?
- [**Java SE 8**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8), [**Java SE 9**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9), [**Java SE 10**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_10), [**Java SE 11**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_11), and later - Part of the standard Java API with a bundled implementation. - [**Java 9**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9) brought some minor features and fixes. - [**Java SE 6**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_6) and [**Java SE 7**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_7) - Most of the *java.time* functionality is back-ported to Java 6 & 7 in [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/). - [**Android**](https://en.wikipedia.org/wiki/Android_(operating_system)) - Later versions of Android (26+) bundle implementations of the *java.time* classes. - For earlier Android (<26), the process of [*API desugaring*](https://developer.android.com/studio/write/java8-support#library-desugaring) brings a [subset of the *java.time*](https://developer.android.com/studio/write/java8-support-table) functionality not originally built into Android. - If the desugaring does not offer what you need, the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project adapts [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) (mentioned above) to Android. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706).






[1]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/OffsetDateTime.html [2]: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/OffsetDateTime.html#plusHours(long)
# tl;dr
Standard [**ISO 8601**][1] format is used by your input string.

Instant.parse ( "2011-08-12T20:17:46.384Z" )

# ISO 8601
This format is defined by the sensible practical standard, [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601).
The `T` separates the date portion from the time-of-day portion. The `Z` on the end means [UTC][2] (that is, an offset-from-UTC of zero hours-minutes-seconds). The `Z` is [pronounced “Zulu”][3].
# java.time
The old date-time classes bundled with the earliest versions of Java have proven to be poorly designed, confusing, and troublesome. Avoid them.
Instead, use the [java.time](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) framework built into Java 8 and later. The java.time classes supplant both the old date-time classes and the highly successful Joda-Time library.
The java.time classes use [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) by default when parsing/generating textual representations of date-time values.
The [`Instant`](https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html) class represents a moment on the timeline in [UTC](https://en.wikipedia.org/wiki/Coordinated_Universal_Time) with a resolution of [nanoseconds](https://en.wikipedia.org/wiki/Nanosecond). That class can directly parse your input string without bothering to define a formatting pattern.
Instant instant = Instant.parse ( "2011-08-12T20:17:46.384Z" ) ;

----------
[![Table of date-time types in Java, both modern and legacy][4]][4]




----------
# About *java.time*
The [*java.time*](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) framework is built into Java 8 and later. These classes supplant the troublesome old [legacy](https://en.wikipedia.org/wiki/Legacy_system) date-time classes such as [`java.util.Date`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Date.html), [`Calendar`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Calendar.html), & [`SimpleDateFormat`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html).
To learn more, see the [*Oracle Tutorial*](http://docs.oracle.com/javase/tutorial/datetime/TOC.html). And search Stack Overflow for many examples and explanations. Specification is [JSR 310](https://jcp.org/en/jsr/detail?id=310).
The [*Joda-Time*](http://www.joda.org/joda-time/) project, now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), advises migration to the [java.time](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/package-summary.html) classes.
You may exchange *java.time* objects directly with your database. Use a [JDBC driver](https://en.wikipedia.org/wiki/JDBC_driver) compliant with [JDBC 4.2](http://openjdk.java.net/jeps/170) 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?
- [**Java SE 8**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8), [**Java SE 9**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9), [**Java SE 10**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_10), [**Java SE 11**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_11), and later - Part of the standard Java API with a bundled implementation. - [**Java 9**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_9) brought some minor features and fixes. - [**Java SE 6**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_6) and [**Java SE 7**](https://en.wikipedia.org/wiki/Java_version_history#Java_SE_7) - Most of the *java.time* functionality is back-ported to Java 6 & 7 in [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/). - [**Android**](https://en.wikipedia.org/wiki/Android_(operating_system)) - Later versions of Android (26+) bundle implementations of the *java.time* classes. - For earlier Android (<26), a process known as [*API desugaring*](https://developer.android.com/studio/write/java8-support#library-desugaring) brings a [subset of the *java.time*](https://developer.android.com/studio/write/java8-support-table) functionality not originally built into Android. - If the desugaring does not offer what you need, the [***ThreeTenABP***](https://github.com/JakeWharton/ThreeTenABP) project adapts [***ThreeTen-Backport***](http://www.threeten.org/threetenbp/) (mentioned above) to Android. See [*How to use ThreeTenABP…*](http://stackoverflow.com/q/38922754/642706).


[![Table of which java.time library to use with which version of Java or Android][5]][5]



[1]: https://en.wikipedia.org/wiki/ISO_8601 [2]: https://en.m.wikipedia.org/wiki/Coordinated_Universal_Time [3]: https://en.wikipedia.org/wiki/Coordinated_Universal_Time#Time_zones [4]: https://i.stack.imgur.com/p0TgD.png [5]: https://i.stack.imgur.com/Sksw9.png

        
Present in both answers; Present only in the new answer; Present only in the old answer;