Convert LocalDateTime to Timestamp in Java

Convert LocalDateTime to Timestamp in java

In this post, we will how to convert LocalDateTime to Timestamp .

Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion.

LocalDateTime

LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime;

LocalDateTime is an immutable object used to represent date in the format
Year-date-month-day-hour-minute-second. Time is represented in nanosecond precision.
Example :

Output :

2020-11-07T10:16:02.234214500

Note : Above output is in accordance to the time when I compiled my code. And output is subject to change, as the time and date changes.
LocalDateTime.now() : Returns LocalDateTime object at that particular instant of time.

Timestamp

Timestamp class can be imported from java.sql package, i.e., import java.sql.Timestamp;
This class allows JDBC API to identify as SQL Timestamp. SQL timestamp has fractional seconds value, This class objects makes timestamp compatible to SQL timestamp, making it easy for the JDBC API to run queries on time and manipulate database.

Timestamp provides methods to format and parse operations to support JDBC.

Convert LocalDateTime to Timestamp

You can use Timestamp’s valueOf() method to convert LocalDateTime to Timestamp. Converting LocalDateTime to Timestamp helps us to convert time into Timestamp that is compatible with SQL timestamp datatype.

Timestamp.valueof(LocalDateTime local): Returns Timestamp object with same date, same time, in correspondence to the time provided in LocalDateTime object.

Output :

Local Date Time : 2020-11-18T23:49:33.175092900
Time stamp : 2020-11-18 23:49:33.1750929

As mentioned above, output can subject to change based on the time code compiled.

That’s all about how to convert LocalDateTime to Timestamp in Java

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 09 January

    Convert set to String in Python

    Table of ContentsWays to convert set to string in PythonUsing the str() functionUsing the repr() functionUsing the join() functionUsing the map() functionConclusion Sets are an unordered collection of elements in Python. It does not contain duplicate elements and can be defined using the set() function in Python. Ways to convert set to string in Python […]

  • Convert String to Array in java
    11 December

    Convert String to Array in Java

    Table of ContentsHow to convert String to Array in JavaUsing toArray() method of SetUsing the split() method of String classUsing StringTokenizor classUsing the split() method of StringUtils classUsing split() method of Pattern classConclusion When developing applications in Java there are many cases where we will find ourselves converting data from one type to the other. […]

  • Convert float to int in Python
    19 June

    Convert float to int in Python

    Table of ContentsUsing the int() function to convert float to int in PythonUsing the math module functions to convert float to int in PythonUsing ceil()Using trunc()Using floor Python provides several in-built functions and modules for datatype conversions. Floating-point numbers can be easily converted to integers using some of these in-built functions. This tutorial will discuss […]

  • Java BigDecimal to Double
    17 May

    Java BigDecimal to Double

    Learn about how to convert BigDecimal to Double in java

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    Table of ContentsUsing toInstant() method of Date classUsing toInstant() method of Date classUsing java.sql.Date In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.