Java Tutorials
  • Introduction to Java
    • What is Java?
    • History and Features of Java
    • Java Virtual Machine (JVM) and Bytecode
    • Why Java?
  • Setting up Java Development Environment
    • Installing Java Development Kit (JDK)
    • JDK vs JRE
    • Setting up IDE (Eclipse, IntelliJ, NetBeans) or Text Editor (VS Code, Sublime Text)
  • Basic Java
    • First Java Program : Hello World
    • Variable
    • Data Type
    • Constant
    • Date and Format
    • Operator
    • Condition
    • Looping
    • Function
    • Variadic Function
    • Enums
    • Array
    • Collection
    • Exception and Exception Handling
    • Naming Convention
  • Object Oriented Programming (OOP)
    • Classes and Objects
    • Inheritance and Polymorphism
    • Encapsulation and Abstraction
  • File Handling
    • Reading and Writing Binary File
    • Reading and Writing Text File
    • Serialization and Deserialization
  • Multithreading
    • Creating and Running Threads
    • Synchronization
    • Thread Pools and Executors
  • Collections API
    • Sorting and Comparable
    • Searching and Comparator
  • Java Database Connectivity (JDBC)
    • Introduction and Life Cycle
    • Connection to Database (MySQL)
    • Downloading JDBC Drivers for Various Databases
    • Maven and Gradle JDBC Drivers for Various Databases
    • JDBC URL Formats
    • Statement and PreparedStatement
    • CallableStatement
    • Selecting Data using JDBC
    • Inserting Data using JDBC
    • Updating Data using JDBC
    • Deleting Data using JDBC
    • Invoking Function and Stored Procedure using JDBC
  • Lambda
    • Introduction to Lambda Expressions
    • Functional Interface
    • Filtering, Mapping, Reducing
    • Lambda Expressions in Collections
    • Method References
    • Functional Programming Concepts
    • Stream API
    • Error Handling in Lambda Expressions
    • Optional in Functional Programming
    • Parallel Processing with Lambda
    • Functional Programming Patterns
    • Advanced Topics in Lambda Expressions
    • Best Practices and Design Patterns
    • Real-World Use Cases and Examples
Powered by GitBook
On this page
  1. Basic Java

Date and Format

In Java, the Date class, part of the java.util package, represents a specific instant in time with millisecond precision. Despite its name, Date doesn't just represent dates but also includes time information. However, the Date class has many limitations and has been largely replaced by the java.time package introduced in Java 8, which provides a more comprehensive and flexible way of handling dates and times. Still, understanding the basics of the Date class can be useful in certain situations.

Creating and Using Date Objects:

Creating a Date Object:

import java.util.Date;

public class DateExample {

    public static void main(String[] args) {
        Date currentDate = new Date(); // Current date and time
        System.out.println("Current Date and Time: " + currentDate);
    }
}

Formatting Date Objects (Using SimpleDateFormat):

import java.util.Date;
import java.text.SimpleDateFormat;

public class DateFormattingExample {

    public static void main(String[] args) {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        String formattedDate = sdf.format(currentDate);
        System.out.println("Formatted Date and Time: " + formattedDate);
    }
}

Important Points about Date Class:

  1. Default Format: When you print a Date object directly, it uses its default formatting, which includes the date and time down to the millisecond level.

  2. Formatting with SimpleDateFormat: If you want a custom date/time format, you can use the SimpleDateFormat class to format the Date object according to your requirements.

  3. Mutability: The Date class is mutable, meaning you can change its internal state after creation. This mutability can lead to issues in multithreaded environments.

  4. Deprecated Methods: Many methods of the Date class are deprecated, indicating that they are not recommended for use. It's recommended to use the classes in the java.time package for date and time operations instead.

Example Using java.time Package (Java 8+):

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeExample {

    public static void main(String[] args) {
        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
        String formattedDateTime = currentDateTime.format(formatter);
        System.out.println("Formatted Date and Time: " + formattedDateTime);
    }
}

Explanation:

This example uses the LocalDateTime class from the java.time package, which provides a more robust and flexible way to handle dates and times. The DateTimeFormatter is used for formatting the LocalDateTime object into a custom format. The result is similar to the previous example but uses the modern Java 8+ date and time API.

PreviousConstantNextOperator

Last updated 1 year ago