Compare date in java - java-8

I am getting 2040-11-01T00:00:00.000-05:00 this format date in String from server. I want to compare this date with today's date to check incoming date in response is after today.
How I can check it in java8 way?

tl;dr
If you want to compare the moment represented by your text input against the current moment.
OffsetDateTime
.parse( "2040-11-01T00:00:00.000-05:00" )
.toInstant()
.isAfter(
Instant.now()
)
Details
Parse your input as a date with time-of-day on the context of an offset-from-UTC, an OffsetDateTime object. Then extract an Instant to see the same moment adjusted to an offset of zero hours-minutes-seconds.
OffsetDateTime odt = OffsetDateTime.parse( "2040-11-01T00:00:00.000-05:00" ) ;
Instant instant = odt.toInstant() ; // Offset of zero, as seen in UTC.
Get current moment. Compare.
Instant now = Instant.now() ;
boolean isFuture = instant.isAfter( now ) ;
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?
Java SE 8, Java SE 9, Java SE 10, Java SE 11, and later - Part of the standard Java API with a bundled implementation.
Java 9 brought some minor features and fixes.
Java SE 6 and Java SE 7
Most of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
Android
Later versions of Android (26+) bundle implementations of the java.time classes.
For earlier Android (<26), the process of API desugaring brings a subset of the java.time functionality not originally built into Android.
If the desugaring does not offer what you need, the ThreeTenABP project adapts ThreeTen-Backport (mentioned above) to Android. See How to use ThreeTenABP….

Related

Differences of implementations from IBM and Oracle of java BigDecimal in java 8?

Can anybody explain me the differences of implementations from IBM and Oracle of java BigDecimal, in java 8?
I know that the IBM implementation of BigDecimal (com.ibm.math.BigDecimal) is deprecated and I want to move to the Oracle implementation (java.math.BigDecimal), but I'd like to know the main differences between the 2 versions (starting at version java 8).
Thanks.
IBM donated their code to Java 5.
The BigDecimal implementation in OpenJDK is a continuation and evolution of that original IBM implementation. Notice the IBM copyright notice in the source code of OpenJDK. Perhaps the change history on that file might interest you.
Since their donation, I know some serious improvements were made, though I cannot remember the details. There were talks given at JavaOne conferences addressing these improvements, bug fixes, and math-related issues.
To migrate is utterly simple:
Change your import statements.
Know that any serialized objects cannot be read between the two implementations (being identified as coming from two different classes).
To quote IBM:
Enhanced BigDecimal
From Java™ 5.0, the IBM® BigDecimal class has been adopted by Oracle as java.math.BigDecimal. The com.ibm.math.BigDecimal class is reserved for possible future use by IBM and is currently deprecated. Migrate existing Java code to use java.math.BigDecimal.
The new java.math.BigDecimal uses the same methods as both the previous java.math.BigDecimal and com.ibm.math.BigDecimal. Existing code using java.math.BigDecimal continues to work correctly. The two classes do not serialize.
To migrate existing Java code to use the java.math.BigDecimal class, change the import statement at the start of your .java file from: import com.ibm.math.; to import java.math.;.

Are java.util.Date and java.util.Calendar deprecated?

It seems that the new java.time API offers everything from java.util.Date and much more.
Is there any reason to use java.util.Date when the newer java.time API is there since Java 8?
Should java.util.Date and java.util.Calendar be avoided completely?
Short answer: The new API java.time is way better than the old world with java.util.Date and java.util.Calendar. So yes, the new API should be preferred in new code.
For a quick overview: Once I had written a comparison of features in table form for various date-time-libraries. There is almost no feature which java.time is missing but exists in the old world:
configurable gregorian/julian cutover
printing based on the class FieldPosition (used in Swing-component FormattedTextField)
About deprecation: Although most parts of java.util.Date are deprecated since Java 1.1, the class itself (and java.util.Calendar, too) are not officially deprecated, just declared as de facto legacy. The support of the old classes is still important for the goal of backwards compatibility with legacy code. So Oracle will probably not stop the support at any time in the future. But maybe Oracle will apply more sophisticated deprecation strategies.
Future development: It is interesting that the release of Java-8 has not only incorporated a completely new date/time-API (java.time) but also seen some few enhancements to java.util.Calendar, for example Calendar.Builder or SHORT_STANDALONE etc. Well, I can only speculate but this also seems to indicate that Oracle is not willing to stop the support of the old API in the near future.

Does service builder support using Java 8 Date classes?

Does liferay 6.2 allow using new Java 8 date classes such as LocalDate to be used as a datatype for entity column fields,in order to utilise more features on the same.We need to use
<column name="createDate" type="LocalDate" />
instead of<column name="createDate" type="Date" />
Is there some other means to achieve this?Does tweaking hibernate version,allow usage on the same,if so,is that valid?
Liferay 6.2 started with Java 7 compatibility and only got compatible with Java 8 later, mostly due to limitations of the supported application servers - this is a good indicator that there's nothing foreseen with regards to Java 8 classes. While they might work, you might run into untested territory. I'm neither saying it does nor it doesn't - although I suspect that you have tried already and ran into problems when you ask the question here.
As Guivani Grifante said, upgrading Hibernate within Liferay most likely will result in a huge mess, and if you are ready to do that, I'd rather ask to upgrade to Liferay 7 (though I didn't double-check its Hibernate version).
Another means to get to such an adoption is to use the old Date type and just add a converting getLocalDate() and setLocalDate() to the YourModelImpl and just implement the conversion yourself. Yes, a bit weird, but might be the quickest fix, without a lot of immediate upgrade hassle. However, if you use 6.2 CE, note that there won't be any more updates and you should move on to 7 sooner rather than later. If you're on EE, you'll have a while of support left.
This isn't the only problem if you deploy a portlet with Java 8 into Liferay. If your code was compiled with Java 8 and contains remote services generated by service builder, you will get an error while deploy such portlet. This is caused because Liferay uses a Jodd api to analyze bytecode and get the parameters names to show in /api/jsonws
Hibernate 5.x has support to new date api, however, there is little chance of upgrade without break Liferay 6.2.

Still necessary to call setFormOfUse() on prepared statement parameters for JPA/Oracle NCHAR NVARCHAR?

I am working on legacy code that uses JPA where the code still calls setFormOfUse() for each database parameter of type NCHAR, NVARCHAR.
I am aware that there was a time that this was required.
I am aware
that there was a time after that when one could tweak the default
setting so that it does not have to be caled for each binding.
Is it still necessary to invoke the setFormOfUse() at this point of time (dec 2016) with the latest JDBC drivers? From which JDBC/Oracle versions one can do what? I can only find snippets of information unrelated from the versions.
Edit 1 - Related Questions
Getting Hibernate and SQL Server to play nice with VARCHAR and NVARCHAR
Hibernate with NVARCHAR2
Using Types.NVARCHAR with oracle JDBC driver to work with Cyrillic chars: This gives a hint that 'setFormOfUse()' is necessary until ojdbc driver 11.2.0.1 (jul. 2009) and you can use setNString from driver 11.2.0.2 (aug 2010).
Hibernate with NVARCHAR2 This question is interesting because of the Hibernate version information.
Edit 2 - JDBC Spec
I searched the JDBC specs and I found this information:
JDBC 3.0 (JSR 54; 2002) Nothing in there about NCHAR, NVARCHAR, NCLOB, ...
JDBC 4.0 (JSR 221; 2006) This mentions setNString() and others. So the earliest year that 'setNString' could replace 'setUseOf' would be 2006. So I will now look at the Hibernate releases and Oracle driver releases from 2006 to find the earliest setNString support (if it is supported).
Edit 3 - Conclusion
Conclusions
From Oracle ojdbc-11.2.0.2 (aug 2010) and higher you can use the standard JDBC 'setNString()' in stead of the Oracle specific 'setFormOfUse()'.
If using Hibernate ORM, it is supported from version 4.1.10 and above.
Implications
If using ojdbc-11.2.0.1 (jul 2009) you should write your own OracleDialect and explicitly call 'setFormOfUse()'.
If using ojdbc-11.2.0.2 (aug 2010) or higher + Hibernate < 4.1.10 you have to write your own OracleDialect and call 'setNString()'.
If using ojdbc-11.2.0.2 or higher + Hibernate >= 4.1.10 you don't have to do anything, the conversion will work out-of the box.

Is there a class in java.time comparable to the Joda-Time Interval?

I'm evaluating to migrate my project from the usage of Joda-Time to the java.time package in Java 8. In Joda-Time, I heavily used the Interval class. I could not find anything like this in java.time.
Is there a comparable class?
Sorry for you, there is no equivalent in JSR-310 to JodaTime-Interval-class. I have doubts if this will ever come, but project lead Stephen Colebourne considers at least to support it in the scope of his external library Threeten-Extra, see this issue.
If you are happy with JodaTime you should keep it. Not everything in JodaTime is ported to Java 8 (Interval is not the only issue).
Update from 2014-12-13:
The situation in Java-8 has not changed, but you might also consider other external libraries beyond Joda-Time. Either Threeten-Extra which now includes a very simple interval class since v0.9 (see the other answer of S. Colebourne here) or my library Time4J which offers the range package since v2.0.
JDK 8 JSR-310 does not have an Interval class. The concept of intervals was descoped to ensure that the rest of the library could be completed.
The ThreeTen-Extra project hosts additional non-JDK date-time classes, and v0.9 includes Interval.
Note: Answer updated 2014-12-10 to include presence of Interval in ThreeTen-Extra.
Use Range class from Guava.
See https://stackoverflow.com/a/23485533/1214902 for more details.
Class java.time.Duration may give you similar functionality. In general java 8 time package is very comprehensive and flexible

Resources