CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Auto Generate Created and Modified Date Time in Spring Boot

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Auto Generate Created and Modified Date Time in Spring Boot

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 2 min read ยท 0 Comments
Share: in X

In Spring Boot, managing timestamps for data auditing is simplified with Spring Data JPAโ€™s auditing features. By using the @CreatedDate and @LastModifiedDate annotations along with @EntityListeners, you can automatically record when an entity is created or updated. Simply enable auditing with @EnableJpaAuditing in your main class to integrate this functionality seamlessly.

Table of Contents

  • Quick Setup
    • Annotate Your Entity:
    • Enable Auditing in Application:
  • Understanding the Annotations

Quick Setup

To fully utilize the benefits of auto generate created and modified date time in Spring Boot, ensure that your application is configured correctly.

Annotate Your Entity:

We need to annotate our entity class with @EntityListeners and extend AuditingEntityListener. Also, we need to use @CreatedDate and @LastModifiedDate annotations for the date-time fields.

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
@EntityListeners(AuditingEntityListener.class)
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // Other fields...

    @CreatedDate
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    // Getters and setters...
}

With the implementation of auto generate created and modified date time in Spring Boot, the application will seamlessly handle date-time values.

Enable Auditing in Application:

Enable auditing in our Spring Boot application by adding the @EnableJpaAuditing annotation.

I prefer to use this annonation in the main class. If you want to use it in custom configuration then you can.

Example:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.data.jpa.repository.config.EnableJpaAuditing;


@SpringBootApplication
@EnableJpaAuditing
public class SBApplication {

	public static void main(String[] args) {
		SpringApplication.run(BanyanServiceApplication.class, args);
	}


}

That’s it ๐Ÿ™‚

Using the functionality to auto generate created and modified date time in Spring Boot can greatly simplify your entity management and auditing processes.

With these annotations and configuration, Spring Data JPA will automatically update the createdAt and updatedAt fields in our entity class.

Understanding the Annotations

The @CreatedDate and @LastModifiedDate annotations are part of Spring Data JPA’s auditing capabilities. By incorporating these annotations into our entity classes, we can automate the recording of creation and modification timestamps without the need for manual intervention.

Related Posts:

  • Most Frequently Asked Spring Boot Interview…
  • Spring Data JPA Projection
  • Pagination and Sorting in Spring Boot
  • Setting Up an EC2 Instance with Amazon CloudFront…
  • Spring Data REST example.
  • Master Spring Data JPA Method Queries: The Ultimate…
Tags:javaspring-bootspring-data-jpa
Was this article helpful?
โ† Previous ArticleHandle Null Values in QueryDSL Projections
Next Article โ†’What is JSP in Java?

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap