Java 8 Java.Time Package: Parsing Any String to Date [Code Snippets]

In one of my projects, I received a requirement that stated that while parsing a text file, Strings denoting a date or a timestamp are expected to be in many different formats that are not known in advance, yet all of them represent a valid date or timestamp needed to be parsed properly. So, the solution I came up with is this: To have a set of formats stored in the property file, and when a String needs to be parsed, the formats are read from a file and attempts to parse the String are made sequentially with each format until it is parsed successfully, or until we run out of formats. The advantages of this solution are that if you discover a valid String that was not parsed successfully, all you will need to do is to add a new format to your properties file and no re-compilation and re-deployment is needed. Also, this way, you can set your priorities: Say if the US date format is preferable to the European one, just place US formats first and only after the European ones. Also, in Java 8, the format Strings allow for the optional format sections denoted by '[]'. So, several formats actually may be combined into a single one with optional sections. For example, instead of:

MM/dd/yyyy

MM-dd-yyyy

MM.dd.yyyy


Java SimpleDateFormat Is Not Simple

Formatting and parsing dates is a daily (painful) task. Every day, it gives us another headache.

A common way to format and parse dates in Java is using SimpleDateFormat. Here is a common class we can use.

Love It or Hate It, Java Continues to Evolve

TechRepublic recently published an article about languages that developers love and those that they hate. This produced an interesting set of results since Java was the third most loved language as well as the second most hated. Clearly, Java, as a language, polarizes opinion. When I tweeted about this, I got one reply from Bjarne Stroustrup, the creator of C++:

“There are only two kinds of languages: the ones people complain about and the ones nobody uses.”

I think he has a point.

JavaLand 2019 Retrospective

In this article, I talk about my impressions from the JavaLand 2019 conference. This was my second time at the international conference, which, this year, took place in the theme park "Phantasialand" in Bruehl, near Cologne, Germany, from March 18th-20th.

Additionally, you can download the presentations here, as well as lecture recordings here.

Java 8 Comparator: How to Sort a List

In this article, we’re going to see several examples on how to sort a List in Java 8.

Sort a List of Strings Alphabetically

List<String> cities = Arrays.asList(
       "Milan",
       "london",
       "San Francisco",
       "Tokyo",
       "New Delhi"
);
System.out.println(cities);
//[Milan, london, San Francisco, Tokyo, New Delhi]

cities.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println(cities);
//[london, Milan, New Delhi, San Francisco, Tokyo]

cities.sort(Comparator.naturalOrder());
System.out.println(cities);
//[Milan, New Delhi, San Francisco, Tokyo, london]

We’ve written London with a lowercase "L" to better highlight differences between Comparator.naturalOrder(), which returns a Comparator that sorts by placing capital letters first, and String.CASE_INSENSITIVE_ORDER, which returns a case-insensitive Comparator.

Two Ways to Join String in Java 8: StringJoiner and String.join Examples

Joining multiple String literals or objects into one is a common programming requirement, and you will often find situations where you need to convert a list of String or a Collection of String into a CSV String for your application. For a long time, JDK API has no way to join multiple String literals or objects together, which forces programmers to write hacks like looping through all String objects and manually joining them using String concatenation to create the final, joined String. Even though this approach worked, it was filled with errors and hacks; you need to be careful not to add delimiter before the first element and after the last element, which often caused issues, particularly for new Java developers.

But the bigger problem with that approach was that everyone needed to re-invent the wheel. Since it was a very common requirement, you found many programmers writing the same routines and making the same mistakes, often ending in StackOverflow to solve their problems.  Thankfully, Java 8 solved this problem once for all.

Java 8 Steaming (groupBy Example)

In this article, I would like to share few scenarios where we will see how the Java 8 Stream and related functions can be useful for reading a file and applying some aggregate functions, which we generally do in our day-to-day SQL/queries on the database side.

So, I am taking an example of employee CSV files, which is shown below:

20 Examples of Using Java’s CompletableFuture

This post revisits Java 8’s CompletionStage API and specifically its implementation in the standard Java library CompletableFuture. The API is explained by examples that illustrate the various behaviors, where each example focuses on one or two specific behaviors.

Since the CompletableFuture class implements the CompletionStage interface, we first need to understand the contract of that interface. It represents a stage of a certain computation which can be done either synchronously or asynchronously. You can think of it as just a single unit of a pipeline of computations that ultimately generates a final result of interest. This means that several CompletionStages can be chained together so that one stage’s completion triggers the execution of another stage, which in turn triggers another, and so on.

When to Use Java 8 Default Methods in Interfaces

One of the Oracle’s articles describes in great detail when Default methods should be used:

The section Interfaces describes an example that involves manufacturers of computer-controlled cars who publish industry-standard interfaces that describe which methods can be invoked to operate their cars. What if those computer-controlled car manufacturers add new functionality, such as flight, to their cars? These manufacturers would need to specify new methods to enable other companies (such as electronic guidance instrument manufacturers) to adapt their software to flying cars. Where would these car manufacturers declare these new flight-related methods? If they add them to their original interfaces, then programmers who have implemented those interfaces would have to rewrite their implementations. If they add them as static methods, then programmers would regard them as utility methods, not as essential, core methods.

Mythbusters: 5 Myths About How Java Is Getting Better

Java was originally design for interactive television, but it was too advanced of technology for the cable television industry at the time. The history of Java starts with a team called Green Team, who initiated this project to develop a language for digital devices, such as set-top boxes, televisions, etc. However, it was suited for Internet programming. Later, Java incorporated by Netscape.

The main reasons for creating Java were simple: we needed a language that was robust, portable, platform-independent, secure, high-performance, multithreaded, architecture-neutral, object-oriented, interpreted, and dynamic.

JVM Calendar: Java Is Still Free

I traditionally write up a retrospective on the year from the perspective of the Java Ecosystem. But this past year, there were some major announcements by Oracle on their positioning around Java support and subscription licensing, which has sent the ecosystem into a bit of a tailspin.

The information by Oracle was often misread, misinterpreted, and misunderstood with the added complication of many folks not understanding their options going forward.

Deploying Spring Boot 2.x Applications in WebLogic 12.1.3.1 Using Gradle Build

Deploying Spring Boot 2.x applications in WebLogic Versions 12.2.x is simpler than deploying those in WebLogic 12.1.x using Gradle build. I was struggling with the deployment of 2.x applications in 12.1.x version of WebLogic. There is no issue if you use Maven to build. But, using the Gradle build is complicated. But, finally, after some research got the solution and deployed successfully. Today, I will demonstrate how to deploy 2.x applications in 12.1.x version of WebLogic. Let's start. For my project, I'm using below tech stack:

a. Gradle 4.5+
b. Spring Boot 2.1.1.RELEASE
c. Java 8
d. WebLogic 12.1.3.1
e. javax.servlet 4.0.1 (Excliptly defining because of older WebLogic)