How to Convert Timestamp to Date in Java 8 or Above


We can make use of Timestamp and LocalDate classes to convert timestamp to date using the toLocalDate() method.

Example:

package org.code2care.examples;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

/**
 * Convert Date format from yyyy-MM-dd
 * to dd/MM/yyyy example
 * <p>
 * author: Code2care.org
 */
public class Example {

    public static void main(String... args) {

        Timestamp timestamp = Timestamp.valueOf("2023-10-17 15:20:41");
        LocalDate localDate = timestamp.toLocalDateTime().toLocalDate();

        System.out.println("Timestamp: "+ timestamp);
        System.out.println("LocalDate: " + localDate);

    }

}

Output:

Convert Timestamp to date Java example

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap