Sign In
Sign In

Installing Java on Ubuntu 22.04

Installing Java on Ubuntu 22.04
Hostman Team
Technical writer
Java
04.03.2024
Reading time: 7 min

The Java language is used in web development, writing desktop software, creating computer and mobile games (Android), as well as in scientific research and the financial sector. Java's popularity and prevalence are due to its security, application scalability, and adaptability.

This article will describe in detail the process of installing Java on Ubuntu. You will need a server with the Ubuntu 22.04 operating system, which you can order at Hostman. 

Installing Java

Before we proceed directly to the installation, we should talk about the existing versions of Java and its main components: JDK and JRE.

JRE is a software environment in which Java programs run. It includes JVM and Java class libraries.

JDK is a development platform for creating Java applications. It includes the JRE, compiler, debugger, and other useful developer tools.

At the time of writing, the latest version of Java is Java SE 21. It will be supported until September 2026. 

Let's look at the two most popular Java builds: OpenJDK and Oracle JDK. The former is an open-source version, and the latter is proprietary, for the use of which, in commercial developments, you need to pay. There is no difference between them for the average user. When installing OpenJDK, you can only install the JRE; in the case of Oracle JDK, both the JDK and the JRE will be installed.

In this article, we will describe installing both versions. After installation, we will tell you how to configure the use of a specific version by default and how to remove unnecessary ones from those installed on the OS.

Installing OpenJDK on Ubuntu

Here, we will be installing Java 19 on Ubuntu. 

First, let's update the list of apt packages in our system:

sudo apt update

Now, let's check whether Java is already installed in our system:  

java -version

In the output, you will see a message similar to the one below. The list of the available versions may vary.

Image4

To install Java on Ubuntu, use the command suggested by the console:

sudo apt install version_name

In our case, to download JRE from OpenJDK 19 to the server, we will enter openjdk-19-jre-headless instead of version_name, like this:

sudo apt install openjdk-19-jre-headless

Image9

After the installation is complete, check the Java version again: 

java -version

If the system displays your Java version, you have successfully installed Java on Ubuntu.

Image5

In addition to the JRE, you can also install the JDK.

To do this, use the command mentioned above:

sudo apt install version_name

However, this time, instead of version_name, enter openjdk-19-jdk-headless:

sudo apt install openjdk-19-jdk-headless

Image19

Now check that the JDK was installed successfully

javac -version

The system should respond with the following line:

Image1

This completes the installation of Java 19 on Ubuntu. If you want to install a different version, for example 17, simply replace the number 19 in the commands with 17.

Installing Oracle JDK on Ubuntu

We will install Java 17 on Ubuntu. First, we need to go to the Oracle website and find the "Compressed Archive" for our CPU type (ARM64 or x64). 

To check the CPU type of your Ubuntu server, run the command:

dpkg --print-architecture

Image10

In our case, it shows amd64, which means we should use the x64 archive.

Copy the download link to the clipboard.

Image14

Now you can download directly to the server using the wget command:

wget your_link

So, in our case:

wget https://download.oracle.com/java/17/archive/jdk-17.0.10_linux-x64_bin.tar.gz

Image3

Next, check the checksum of the downloaded archive: 

sha256sum jdk-17.0.10_linux-x64_bin.tar.gz

Image13

And compare it to the checksum on the website. To do this, click on sha256 next to the archive.

Here's the checksum we get on the website:

Image8

As you can see, the checksums match.

Now, unpack the archive into a folder on the server.

First, let's create a directory into which we will unpack the archive:

sudo mkdir -p path_to_directory

For example:

sudo mkdir -p /usr/lib/jvm

Next, unpack all the files of the downloaded archive into it.

sudo tar -zxvf jdk-17.0.10_linux-x64_bin.tar.gz -C /usr/lib/jvm

Now you can proceed to the installation. 

We will perform it using PPA. To add the PPA repository we will use the command below:

sudo add-apt-repository ppa:linuxuprising/java

Image18

After this, update the list of apt packages:

sudo apt update

And then install Oracle JDK 17 on the server:

sudo apt install oracle-java17-installer --install-recommends

When a message appears on the screen, as in the picture below, scroll down and click Ok.

Image12

Next, select Yes.

Image17

Upon completion, we will check if everything went well. To do this, enter the command we are already familiar with:

java -version

In response, you should receive a similar message:

Image11

As you can see, Oracle JDK 17 has been successfully installed to the server.

Let's also check the compiler installation:

javac -version

Image2

Installation of Java 17 on Ubuntu is complete.

Using one version to work with Java

In this article we install two versions of Java on a server. In practice, there may be more than two. In such cases, you can select a particular version as the default one. To do this, enter the following command into the terminal:

sudo update-alternatives --config java

As a result, the system will display all the versions on the server, as shown in the screenshot below.

Image7

To select your default version, enter its serial number in the line shown in the left column. To leave it as it is, press Enter.

This procedure can also be performed for compiler versions by replacing java with javac at the end of the command:

sudo update-alternatives --config javac

Image6

Removing Java

If, at some point, you no longer need one of the versions, you can safely delete it. Let's say you have installed the OpenJDK Java 16 on Ubuntu 22.04. To remove it, enter the following command into the terminal:

sudo apt purge openjdk-16*

If you want to remove every OpenJDK version installed on the server, use the command above without specifying the version.

In the case of Oracle Java, replace openjdk* in the command above with oracle-java*.

If you have installed Java 16 on Ubuntu, enter the following command into the console:

sudo apt purge oracle-java16-installer

If you need to remove another Java version, just change the version number in the command.

The JAVA_HOME variable

JAVA_HOME is used in many programs developed in Java. It points to the directory the JDK is installed to. 

To find out this directory, open the list of versions, as described in the section above:

sudo update-alternatives --config java

Image7

Now we know the location of Oracle Java 17 and OpenJDK 19. Since our example uses Oracle Java 17 by default, we copy the address specifically for it.

Now, use the copied address in the following command:

echo export JAVA_HOME="/usr/lib/jvm/java-17-oracle" >> ~/.bashrc

And then run:

source ~/.bashrc

Now let's check the changes made:

echo $JAVA_HOME

If the terminal displays the line shown below, everything was successful:

Image16

Testing Java

Let's check that everything is installed and working correctly. To do this, we will create a simple program displaying a greeting on the screen.

1. Create a program file:

sudo nano example.java

2. Then write the code for our program:

public class example { 
    public static void main(String[] args) {
        System.out.println("Hello, User! Java is working successfully.");
    }
}

Save the file and close the editor.

3. Now compile the program:

javac example.java

4. And launch it:

java example

The console should output the phrase that was placed in the System.out.println method. If it does, then everything works correctly.

Conclusion

In the article, we examined how to install Java on Ubuntu 22.04. We have  shown how to install different versions of Java on the Ubuntu server and remove them. 

Java
04.03.2024
Reading time: 7 min

Similar

Java

Catching and Handling Exceptions in Java

The Java programming language, like many others, has built-in tools for working with errors, i.e., exceptional situations (exceptions) where a program failure is handled by special code, separate from the basic algorithm. Thanks to exceptions, a developer can anticipate weak points in the codebase and preempt fatal errors at runtime. Therefore, handling exceptions in Java is a good practice that improves the overall reliability of code. The purpose of this article is to explore the principles of catching and handling exceptions, as well as to review the corresponding syntactic structures in the language intended for this. All the examples in this guide were run on Ubuntu 22.04, installed on a cloud server from Hostman. Installing OpenJDK and Running an Application The examples shown in this guide were run using OpenJDK. Installing it is straightforward. First, update the list of available repositories: sudo apt update Next, request the list of OpenJDK versions available for download: sudo apt search openjdk | grep -E 'openjdk-.*-jdk/' You’ll see a short list in the terminal: WARNING: apt does not have a stable CLI interface. Use with caution in scripts. openjdk-11-jdk/jammy-updates,jammy-security 11.0.25+9-1ubuntu1~22.04 amd64 openjdk-17-jdk/jammy-updates,jammy-security 17.0.13+11-2ubuntu1~22.04 amd64 openjdk-18-jdk/jammy-updates,jammy-security 18.0.2+9-2~22.04 amd64 openjdk-19-jdk/jammy-updates,jammy-security 19.0.2+7-0ubuntu3~22.04 amd64 openjdk-21-jdk/jammy-updates,jammy-security,now 21.0.5+11-1ubuntu1~22.04 amd64 [installed] openjdk-8-jdk/jammy-updates,jammy-security 8u432-ga~us1-0ubuntu2~22.04 amd64 We will use the openjdk-21-jdk version: sudo apt install openjdk-21-jdk You can then check that Java was installed correctly by requesting its version: java --version The terminal output will look something like this: openjdk 21.0.5 2024-10-15 OpenJDK Runtime Environment (build 21.0.5+11-Ubuntu-1ubuntu122.04) OpenJDK 64-Bit Server VM (build 21.0.5+11-Ubuntu-1ubuntu122.04, mixed mode, sharing) As shown, the exact version of OpenJDK is 21.0.5. All the examples in this guide should be saved in a separate file with a .java extension: nano App.java Then, fill the created file with an example code, such as: class App { public static void main(String[] args) { System.out.println("This text is printed to the console"); } } Note that the class name must match the file name. Next, compile the file: javac App.java And run it: java App The terminal will display the following: This text is printed to the console Types of Exceptions in Java All exceptions in Java have a specific type associated with the reason the exception occurred—the particular kind of program failure. There are two fundamental types of exceptions: Checked Exceptions — These occur at compile time. If they are not handled, the program won’t compile. Unchecked Exceptions — These occur at runtime. If unhandled, the program will terminate. The Error type is only conditionally considered an exception — it's a full-fledged error that inevitably causes the program to crash. Exceptions that can be handled using custom code and allow the program to continue executing are Checked Exceptions and Unchecked Exceptions. Thus, errors and exceptions in Java are different entities. However, both Errors and Exceptions (Checked and Unchecked) are types with additional subtypes that clarify the reason for the failure. Checked Exceptions Here's an example of code that triggers a compile-time exception: import java.io.File; import java.util.Scanner; public class App { public static void main(String[] args) { File someFile = new File("someFile.txt"); // create file reference Scanner scanner = new Scanner(someFile); // parse file contents } } Compilation will be interrupted, and you’ll see the following error in the terminal: App.java:7: error: unreported exception FileNotFoundException; must be caught or declared to be thrown Scanner scanner = new Scanner(someFile); ^ 1 error If you catch and handle this exception, the code will compile and be runnable. Unchecked Exceptions Here’s another example of code that triggers an exception only at runtime: class App { public static void main(String[] args) { int[] someArray = {1, 2, 3, 4, 5}; // create an array with 5 elements System.out.println(someArray[10]); // attempt to access a non-existent element } } No exception will occur during compilation, but after running the compiled code, you’ll see this error in the terminal: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 5 at app.main(app.java:4) This means that such an exception can be handled using user-defined code, allowing the program to continue execution. Error Finally, here’s an example of code that causes a runtime error: public class App { static int i = 0; public static int showSomething(int x) { i = i + 2; return i + showSomething(i + 2); } public static void main(String[] args) { App.showSomething(i); // trigger stack overflow } } The compilation will succeed, but during execution, the terminal will show a StackOverflowError: Exception in thread "main" java.lang.StackOverflowError at java.base/java.io.BufferedOutputStream.implWrite(BufferedOutputStream.java:220) at java.base/java.io.BufferedOutputStream.write(BufferedOutputStream.java:200) at java.base/java.io.PrintStream.implWrite(PrintStream.java:643) In this case, the error cannot be handled; it can only be fixed in the code. Exception Classes in Java Internally, all exceptions (and errors) in Java are represented as a set of classes, some of which inherit from others. The base class for all errors and exceptions is Throwable. Two other classes inherit from it—Error and Exception, which serve as base classes for a broad range of subclasses associated with specific exception types. The Error class describes error-type exceptions, as mentioned earlier, while the Exception class describes checked exceptions. Furthermore, the RuntimeException class inherits from Exception and describes unchecked exceptions. A simplified class hierarchy of Java exceptions can be represented as the following nested list: Throwable Error Exception CloneNotSupportedException InterruptedException ReflectiveOperationException ClassNotFoundException IllegalAccessException InstantiationException NoSuchFieldException NoSuchMethodException RuntimeException NullPointerException ArithmeticException IllegalArgumentException IndexOutOfBoundException NumberFormatException Each exception class includes methods for retrieving additional information about the failure. You can find the complete classification of Java exceptions, including those from additional packages, in a dedicated reference guide. Exception Handling Syntax in Java try and catch All exceptions are handled using special try and catch blocks, which are standard across most programming languages, including Java. Inside the try block, you write code that may potentially contain an error capable of throwing an exception. Inside the catch block, you write code that handles the exception that occurred in the previously defined try block. For example, a try-catch structure might look like this: public class App { public static void main(String[] args) { try { // code that might throw an exception int someVariable = 5 / 0; System.out.println("Who said you can’t divide by zero?"); } catch (ArithmeticException someException) { // code that handles the exception System.out.println("Actually, you can't divide by zero..."); } } } The output of this code in the console will be: Actually, you can't divide by zero... This specific example is based on an illegal division by zero operation, which is wrapped in a try block and throws an ArithmeticException. Accordingly, in the catch block, this exception is handled by printing an error message to the console. Thanks to this structure, the program can continue running even after the error during division by zero. finally Unlike many other programming languages, Java includes a special finally block as part of the exception handling mechanism. It always executes—regardless of whether an exception occurred or not. So, we can extend the previously shown structure: public class App { public static void main(String[] args) { try { // code that might throw an exception int someVariable = 5 / 0; } catch (ArithmeticException someException) { // code that handles the exception System.out.println("Actually, you can't divide by zero..."); } finally { // code that always executes System.out.println("Who cares if you can divide by zero or not? This message will appear anyway!"); } } } After running this code, the console will display: Actually, you can't divide by zero... Who cares if you can divide by zero or not? This message will appear anyway! To understand the practical need for the finally block, consider the following example out of any specific context: try { parseJson(response.json); } catch (JSONException someException) { System.out.println("Looks like there’s something wrong with the JSON..."); } // a function that hides the loading indicator hideLoaderUI(); In a program using this structure, the hideLoaderUI() function will never execute if an exception occurs. In this case, you could try calling hideLoaderUI() inside the exception handler if an exception occurs and also call it afterward if no exception occurred: try { parseJson(response.json); } catch (JSONException someException) { hideLoaderUI(); // duplicate System.out.println("Looks like there’s something wrong with the JSON..."); } hideLoaderUI(); // duplicate However, this results in undesirable duplication of the function call. Moreover, instead of a function, it might be an entire block of code, and duplicating such code is considered bad practice. Therefore, to guarantee the execution of hideLoaderUI() without duplicating the call, you can use a finally block: try { parseJson(response.json); } catch (JSONException someException) { System.out.println("Looks like there’s something wrong with the JSON..."); } finally { // the loading indicator will be hidden in any case hideLoaderUI(); } throw Java allows you to manually create (throw) exceptions using the special throw operator: public class App { public static void main(String[] args) { throw new Exception("Something strange seems to have happened..."); } } You can even create a variable for the exception ahead of time and then throw it: public class App { public static void main(String[] args) { var someException = new Exception("Something strange seems to have happened..."); throw someException; } } throws Another important keyword, throws (note the “s” at the end), allows you to explicitly declare the types of exceptions (in the form of class names) that a method may throw. If such a method throws an exception, it will propagate up to the calling code, which must handle it: public class App { public static void someMethod() throws ArithmeticException, NullPointerException, InterruptedException { int someVariable = 5 / 0; } public static void main(String[] args) { try { App.someMethod(); } catch (Exception someException) { System.out.println("Dividing by zero again? Do you even know what insanity is?"); } } } The console output will be: Dividing by zero again? Do you even know what insanity is? Creating Custom Exceptions The hierarchical structure of exceptions naturally allows for the creation of custom exception classes that inherit from the base ones. Thanks to custom exceptions, Java enables you to implement application-specific error handling paths. Thus, in addition to standard Java exceptions, you can add your own. Each custom exception, like any predefined one, can be handled using standard try-catch-finally blocks: class MyOwnException extends Exception { public MyOwnException(String message) { super(message); // calls the parent class constructor System.out.println("Warning! An exception is about to be thrown!"); } } public class App { public static void main(String[] args) { try { throw new MyOwnException("Just an exception. No explanation. Anyone got a problem?"); } catch (MyOwnException someException) { System.out.println(someException.getMessage()); } } } The console output will be: Warning! An exception is about to be thrown! Just an exception. No explanation. Anyone got a problem? Conclusion This tutorial demonstrated, through examples, why exceptions are needed in Java, how they arise (including how to manually throw one), and how to handle them using the corresponding language tools. Exceptions that can be caught and handled come in two types: Checked Exceptions: Handled at compile time. Unchecked Exceptions: Handled at runtime. In addition to these, there are fatal errors that can only be resolved by rewriting code: Errors: Cannot be handled. There are several syntax structures (blocks) used to handle exceptions: try: Code that may throw an exception. catch: Code that handles the possible exception. finally: Code that executes regardless of whether an exception occurred. As well as keywords to control the process of throwing exceptions: throw: Manually throws an exception. throws: Lists possible exceptions in a declared method. You can find the full list of methods in the parent Exception class in the official Oracle documentation.
20 June 2025 · 11 min to read
Java

Exception Handling in Java

Exception handling is a cornerstone of robust software development in Java, serving as the bridge between theoretical correctness and practical resilience. While most developers grasp the basics of try-catch blocks, the true art of exception management lies in balancing technical precision, architectural foresight, and performance optimization. This article dives deep into Java exception handling, exploring not only core concepts but also advanced patterns, anti-patterns, and strategies for integrating error management into modern architectures such as microservices, reactive systems, and cloud-native applications. The Philosophy of Exception Handling: Beyond Syntax Java’s exception handling mechanism is more than just a syntax requirement—it embodies a philosophy of controlled failure. Unlike languages that rely on error codes or silent failures, Java enforces a structured approach to unexpected scenarios, ensuring developers confront errors explicitly. This design choice reflects two principles: Fail-fast: Identify and address issues at the earliest possible stage. Separation of Concerns: Decouple business logic from error recovery. Understanding these principles is critical for designing systems where exceptions are not merely "handled" but strategically managed to enhance reliability. class FailFastExample { public static void main(String[] args) { int age = -5; // Simulate invalid input // Fail-fast: Validate input and throw exception if invalid if (age < 0) { throw new IllegalArgumentException("Age cannot be negative"); } System.out.println("Age: " + age); // This line won't execute if exception is thrown } } The Anatomy of Java Exceptions Java’s exception hierarchy is rooted in the Throwable class, with three primary categories: Checked Exceptions (Exception subclasses): Enforced by the compiler (e.g., IOException, SQLException). Represent recoverable errors (e.g., file not found, network issues). Require explicit handling via try-catch or propagation using throws. Unchecked Exceptions (RuntimeException subclasses): Not enforced by the compiler (e.g., NullPointerException, IllegalArgumentException). Often indicate programming errors (e.g., invalid arguments, logic flaws). Errors (Error subclasses): Severe, non-recoverable issues (e.g., OutOfMemoryError, StackOverflowError). Typically arise from JVM or system-level failures. The distinction between checked and unchecked exceptions is often debated. Modern frameworks like Spring have largely moved away from checked exceptions, favoring runtime exceptions to reduce boilerplate and improve code readability. import java.io.FileInputStream; import java.io.FileNotFoundException; class ExceptionTypesDemo { public static void main(String[] args) { // Unchecked exception (ArithmeticException) try { int result = 10 / 0; // Division by zero } catch (ArithmeticException ex) { System.out.println("Unchecked error: " + ex.getMessage()); } // Checked exception (FileNotFoundException) try { // Attempt to open a non-existent file new FileInputStream("ghost.txt"); } catch (FileNotFoundException ex) { System.out.println("Checked error: " + ex.getMessage()); } } } Custom Exceptions: Crafting Domain-Specific Errors While Java provides a rich set of built-in exceptions, custom exceptions enable domain-specific error signaling. For example, an e-commerce app might define: // Custom exception class class InvalidInputException extends RuntimeException { public InvalidInputException(String message) { super(message); // Pass the error message to the parent class } } class CustomExceptionDemo { public static void main(String[] args) { try { processInput(""); // Simulate empty input } catch (InvalidInputException ex) { System.out.println("Custom error: " + ex.getMessage()); } } // Method to validate input static void processInput(String input) { if (input.isEmpty()) { throw new InvalidInputException("Input cannot be empty"); } } } Best Practices for Custom Exceptions: Immutable State: Ensure exception objects are immutable to prevent unintended side effects. Rich Context: Include metadata (e.g., timestamps, error codes) to aid debugging. Avoid Overuse: Reserve custom exceptions for scenarios where standard exceptions are insufficient. Advanced Exception Handling Patterns Pattern 1: Exception Translation Wrap lower-level exceptions in higher-level abstractions to avoid leaking implementation details. For instance, convert a SQLException into a DataAccessException in a DAO layer: // Custom exception for wrapping low-level exceptions class CalculationException extends RuntimeException { public CalculationException(String message, Throwable cause) { super(message, cause); // Pass message and cause to the parent class } } class ExceptionTranslationDemo { public static void main(String[] args) { try { calculate(); // Perform calculation } catch (CalculationException ex) { System.out.println("Translated error: " + ex.getMessage()); System.out.println("Root cause: " + ex.getCause().getMessage()); } } // Method to simulate a calculation static void calculate() { try { int result = 10 / 0; // Division by zero } catch (ArithmeticException ex) { // Wrap the low-level exception in a custom exception throw new CalculationException("Calculation failed", ex); } } } Pattern 2: Circuit Breakers In distributed systems, use frameworks like Resilience4j to prevent cascading failures: class SimpleCircuitBreaker { private int failureCount = 0; // Track number of failures private static final int MAX_FAILURES = 2; // Maximum allowed failures public void execute() { // If failures exceed the limit, open the circuit if (failureCount >= MAX_FAILURES) { throw new RuntimeException("Circuit open: Service halted"); } try { // Simulate a failing service throw new RuntimeException("Service error"); } catch (RuntimeException ex) { failureCount++; // Increment failure count System.out.println("Failure #" + failureCount); } } public static void main(String[] args) { SimpleCircuitBreaker cb = new SimpleCircuitBreaker(); for (int i = 0; i < 3; i++) { try { cb.execute(); // Attempt to execute the service } catch (RuntimeException ex) { System.out.println(ex.getMessage()); } } } } Pattern 3: Global Exception Handlers In Spring Boot, use @ControllerAdvice to centralize exception handling: class GlobalHandlerDemo { public static void main(String[] args) { // Set a global exception handler for uncaught exceptions Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> { System.out.println("Global handler caught: " + ex.getMessage()); }); // Simulate an uncaught exception throw new RuntimeException("Unexpected error occurred!"); } } Performance Considerations Exception handling incurs overhead, particularly when stack traces are generated. Optimize with these strategies: class LightweightException extends RuntimeException { @Override public Throwable fillInStackTrace() { return this; // Skip stack trace generation for better performance } } class PerformanceDemo { public static void main(String[] args) { try { throw new LightweightException(); // Throw a lightweight exception } catch (LightweightException ex) { System.out.println("Caught lightweight exception"); } } } Avoid Exceptions for Control Flow: Using exceptions for non-error scenarios (e.g., looping) is inefficient. Lazy Initialization of Stack Traces: Use Throwable constructors that skip stack trace generation (Java 7+): Logging Wisely: Avoid logging the same exception multiple times across layers. Tools and Libraries Lombok: Simplify exception logging with @SneakyThrows. Guava Preconditions: Validate inputs and throw standardized exceptions. ELK Stack (Elasticsearch, Logstash, Kibana): Centralize exception monitoring. Sentry: Real-time error tracking with context-rich reports. Conclusion Exception handling is not an afterthought but a foundational design discipline. By embracing principles like context preservation, strategic logging, and architectural alignment, developers can transform error management from a chore into a competitive advantage. As Java continues to evolve—integrating with cloud platforms, reactive systems, and AI-driven observability tools—exception handling will remain a critical skill for building software that thrives in the face of uncertainty.
06 February 2025 · 8 min to read
Java

Java Date Format

Handling dates and times effectively is a critical aspect of many software applications. In Java, the SimpleDateFormat class from the java.text package offers developers a robust mechanism for formatting Date objects into strings and parsing strings back into Date objects. This guide explores the features, use cases, and best practices for leveraging SimpleDateFormat in your Java projects. Overview of SimpleDateFormat A method for creating unique patterns for date and time data representation is offered by SimpleDateFormat. These patterns are versatile, allowing developers to adapt date formats to their specific application requirements. Here’s an introductory example: import java.text.SimpleDateFormat; import java.util.Date; public class SimpleDateFormatDemo { public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss"); Date currentDate = new Date(); System.out.println("Formatted Date: " + formatter.format(currentDate)); } } This sample produces a string that is formatted in accordance with the given pattern and contains the current date and time: Formatted Date: 07/01/2025 14:35:00 Pattern Syntax for Formatting Dates The SimpleDateFormat class employs a symbolic pattern language to precise how dates and times should appear. Below is a table summarizing some key symbols: Symbol Description Example y Year 2025 (yyyy), 25 (yy) M Month 01 (MM), Jan (MMM) d Day of the month 07 (dd) H Hour (0-23) 14 (HH) h Hour (1-12) 02 (hh) m Minute 35 (mm) s Second 00 (ss) a AM/PM marker PM E Day of the week Tue (EEE), Tuesday (EEEE) z Time zone PST (z), Pacific Standard Time (zzzz) d Day of the month 07 (dd) H Hour (0-23) 14 (HH) h Hour (1-12) 02 (hh) m Minute 35 (mm) s Second 00 (ss) a AM/PM marker PM E Day of the week Tue (EEE), Tuesday (EEEE) z Time zone PST (z), Pacific Standard Time (zzzz) Combining these symbols allows developers to create highly tailored date and time formats. Customizing Date Formats Using SimpleDateFormat, you can craft custom formats to suit various requirements. Here’s an example demonstrating three distinct patterns: import java.text.SimpleDateFormat; import java.util.Date; public class DateFormatExamples { public static void main(String[] args) { Date currentDate = new Date(); SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat verboseFormat = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm a z"); System.out.println("ISO Format: " + isoFormat.format(currentDate)); System.out.println("Verbose Format: " + verboseFormat.format(currentDate)); System.out.println("Time Format: " + timeFormat.format(currentDate)); } } Sample Output: ISO Format: 2025-01-07 Verbose Format: Tuesday, January 07, 2025 Time Format: 02:35 PM PST Parsing Strings to Date Objects SimpleDateFormat also facilitates converting string representations of dates back into Date objects. This is especially useful for handling user input or reading data from external sources. import java.text.SimpleDateFormat; import java.util.Date; public class DateParsingDemo { public static void main(String[] args) { String inputDate = "07-01-2025 14:35:00"; SimpleDateFormat parser = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); try { Date parsedDate = parser.parse(inputDate); System.out.println("Parsed Date: " + parsedDate); } catch (Exception e) { System.out.println("Parsing failed: " + e.getMessage()); } } } Expected Output: Parsed Date: Tue Jan 07 14:35:00 PST 2025 Incorporating Time Zones The setTimeZone method in SimpleDateFormat allows for explicit handling of different time zones. Here’s an example: import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class TimeZoneHandling { public static void main(String[] args) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); formatter.setTimeZone(TimeZone.getTimeZone("UTC")); System.out.println("UTC Time: " + formatter.format(new Date())); formatter.setTimeZone(TimeZone.getTimeZone("America/New_York")); System.out.println("New York Time: " + formatter.format(new Date())); } } Output Example: UTC Time: 2025-01-07 22:35:00 UTC New York Time: 2025-01-07 17:35:00 EST Locale-Aware Formatting The SimpleDateFormat class supports locale-specific date formatting. By specifying a Locale, you can adapt your application for different regions and languages: import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class LocaleFormatting { public static void main(String[] args) { Date today = new Date(); SimpleDateFormat usFormatter = new SimpleDateFormat("EEEE, MMMM dd, yyyy", Locale.US); SimpleDateFormat frFormatter = new SimpleDateFormat("EEEE, MMMM dd, yyyy", Locale.FRANCE); System.out.println("US Format: " + usFormatter.format(today)); System.out.println("French Format: " + frFormatter.format(today)); } } Output Example: US Format: Tuesday, January 07, 2025French Format: mardi, janvier 07, 2025 Working with Legacy Code For projects that rely on legacy systems, SimpleDateFormat can be instrumental in ensuring compatibility with older data formats. By crafting patterns that match the specific requirements of legacy systems, developers can seamlessly bridge modern and older systems. However, it is critical to perform rigorous testing when working with legacy code. Edge cases like leap years, daylight saving time adjustments, or unusual formats often surface in older implementations. Developers should document the specific formats being used and verify the results using multiple test cases. In certain situations, refactoring legacy systems to use modern libraries like DateTimeFormatter may offer long-term benefits. While it might require upfront effort, the enhanced performance and reduced bug risk in newer libraries often justify the transition. Date Validation Techniques Validating dates is a common requirement in applications that accept user input. With SimpleDateFormat, you can ensure that input strings conform to the expected format before further processing. For example: import java.text.SimpleDateFormat; import java.util.Date; public class DateValidation { public static void main(String[] args) { String dateString = "31-02-2025"; SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); formatter.setLenient(false); try { Date validatedDate = formatter.parse(dateString); System.out.println("Valid Date: " + validatedDate); } catch (Exception e) { System.out.println("Invalid Date: " + e.getMessage()); } } } Output Example: Invalid Date: Unparseable date: "31-02-2025" Using the setLenient(false) method ensures that only logically valid dates are accepted, reducing the risk of errors in downstream processes. Common Pitfalls and Recommendations Thread Safety: Avoid sharing SimpleDateFormat instances across multiple threads. Use ThreadLocal or Java’s DateTimeFormatter for thread-safe alternatives. Error Handling: Always handle potential ParseException errors when parsing strings. ISO Standards: Utilize ISO 8601 formats (e.g., yyyy-MM-dd'T'HH:mm:ss'Z') for better interoperability. Dynamic Time Zones: Refrain from hardcoding time zones; instead, fetch them dynamically when necessary. Input Validation: Before parsing, make sure the input strings have the correct format. Transitioning to Modern Alternatives With the introduction of the java.time package in Java 8, many developers prefer DateTimeFormatter over SimpleDateFormat for its enhanced features and thread safety. import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class ModernDateFormatting { public static void main(String[] args) { LocalDateTime currentDateTime = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println("Formatted Date-Time: " + currentDateTime.format(formatter)); } } Sample Output: Formatted Date-Time: 2025-01-07 14:35:00 Additionally, DateTimeFormatter supports advanced features such as optional sections, localized styles, and predefined constants for ISO-compliant formats. These features make it a more versatile and robust choice for modern applications. Final Thoughts SimpleDateFormat remains a practical choice for date and time handling in Java. However, it is essential to understand its limitations, especially in terms of thread safety and modern compatibility. By adopting best practices and considering newer options like DateTimeFormatter, you can ensure robust and efficient date manipulation in your Java applications. Whether you are working with legacy systems, processing user inputs, or developing new features, a thorough knowledge of Java's date formatting features will empower you to handle date and time operations with confidence and precision. In addition, check out our app platform to find cloud apps, such as React, Angular, Vue and more.
15 January 2025 · 8 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support