Convert Localdate to Localdatetime or Java.Sql.Timestamp

Convert LocalDate to LocalDateTime or java.sql.Timestamp

JodaTime

To convert JodaTime's org.joda.time.LocalDate to java.sql.Timestamp, just do

Timestamp timestamp = new Timestamp(localDate.toDateTimeAtStartOfDay().getMillis());

To convert JodaTime's org.joda.time.LocalDateTime to java.sql.Timestamp, just do

Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());


JavaTime

To convert Java8's java.time.LocalDate to java.sql.Timestamp, just do

Timestamp timestamp = Timestamp.valueOf(localDate.atStartOfDay());

To convert Java8's java.time.LocalDateTime to java.sql.Timestamp, just do

Timestamp timestamp = Timestamp.valueOf(localDateTime);

Convert LocalDate to TimeStamp with UTC

Parse the given string to LocalDate and convert it into ZonedDateTime using LocalDate#atStartOfDay(ZoneId).

import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Testing {
public static void main(String[] args) {
LocalDate date = LocalDate.parse("2010-10-10");
ZonedDateTime zdt = date.atStartOfDay(ZoneId.of("Etc/UTC"));
System.out.println(zdt);

// Custom format
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss.SSS a zzz", Locale.ENGLISH);
System.out.println(dtf.format(zdt));
}
}

Output:

2010-10-10T00:00Z[Etc/UTC]
2010-10-10 00:00:00.000 AM UTC

Learn more about java.time, the modern Date-Time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

transform java.sql.Date to LocalDateTime

You are using terrible date-time classes that were years ago supplanted by the modern java.time classes defined in JSR 310.

  • Do not use java.sql.Date
  • Do not use java.util.Date
  • Do not use java.sql.Timestamp
  • Do not use java.util.Calendar

Use only java.time classes.

For exchanging date-time values with a database, use JDBC 4.2 or later.

The java.sql.Date class pretends to represent a date-only value.

If you are handed a java.sql.Date object, immediately convert it to a java.time.LocalDate. Use the new method toLocalDate added to that old class.

LocalDate localDate  = myJavaSqlDate.toLocalDate() ;

You asked for a java.time.LocalDateTime object. You have the necessary date portion. Now you need to assign the time-of-day portion.

LocalTime localTime = LocalTime.of( 15 , 30 );

Combine.

LocalDateTime localDateTime = LocalDateTime.of( localDate, localTime ) ;

A LocalDateTime is inherently ambiguous. 3:30 PM in Japan is a different moment than 3:30 PM in Morocco .

To determine a moment, a specific point on the timeline, place your LocalDateTime within the context of a time zone. You get a ZonedDateTimeObject.

ZoneId zoneId = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zonedDateTime = localDateTime.atZone( zoneId ) ;

To view that moment as seen in UTC, with an offset from UTC of zero hours-minutes-seconds, extract an Instant.

Instant instant = zonedDateTime.toInstant() ;

LocalDateTime to java.sql.Date in java 8?

There is no direct correlation between LocalDateTime and java.sql.Date, since former is-a timestamp, and latter is-a Date.

There is, however, a relation between LocalDate and java.sql.Date, and conversion can be done like this:

LocalDate date = //your local date
java.sql.Date sqlDate = java.sql.Date.valueOf(date)

Which for any given LocalDateTime gives you the following code:

LocalDateTime dateTime = // your ldt
java.sql.Date sqlDate = java.sql.Date.valueOf(dateTime.toLocalDate());

How to convert java.sql.timestamp to LocalDate (java8) java.time?

You can do:

timeStamp.toLocalDateTime().toLocalDate();

Note that timestamp.toLocalDateTime() will use the Clock.systemDefaultZone() time zone to make the conversion. This may or may not be what you want.

Convert java.sql.date to java.time.LocalDateTime

It was actually easier than I thought.
This worked for me:

//java.sql.ResultSet result
result.getTimestamp("value").toLocalDateTime()

joda-time-2.2 convert LocalDateTime to java.sql.Timestamp

Why can't you do

LocalDateTime ldt  = LocalDateTime.now();
Timestamp ts = new Timestamp(ldt.toDate().getTime());


Related Topics



Leave a reply



Submit