virsha
virsha

Reputation: 1170

Convert Joda LocalDate or java.util.Date to LocalDateTime having time at Start of the Day

I am using Joda 2.5

Facing an issue while converting the Joda LocalDate to LocalDateTime.

As,I am able to convert LocalDate to DateTime with Time atStartOfDay. I wanted the same feature but via LocalDateTime object.

My code is :

Suppose Date is coming from different REST service , say

Date currentDate = new Date("2015-02-05");

And now this Date is passed to some another service as shown below :

funService(currentDate);

public funService(Date currentDate)
{
       LocalDate localDateObject = new LocalDate(date);

// Now I have to convert this Date to LocalDateTime
    LocalDateTime localDateTime = localDateObject.toDateTimeAtStartOfDay();
//But this returns DateTime Object.

// As I don't want to store the DateTime Object because it also stores the TimeZone


}

I want the LocalDateTime Object as I am storing this LocalDateTime as Timestamp in the mysql.

Please provide the solution for to convert in LocalDate to LocalDateTime Object.

Eg : '2015-02-01' to '2015-02-01 00:00:01' YYYY-MM-DD to YYYY-MM-DD HH:MM:SS:zzz

But not LocalDate to DateTime

Upvotes: 1

Views: 6379

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338181

tl;dr

myPreparedStatement.setObject(       // Insert object into database for `TIMESTAMP WITHOUT TIME ZONE` type of column.
    … , 
    LocalDate.parse( "2015-02-05" )  // Parse input string to a date-only value lacking time-of-day and lacking time zone.
             .atStartOfDay()         // Determine start of a generic 24-hour day for a `LocalDateTime` object lacking offset-from-UTC and lacking time zone.
) ;

java.time

The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. See Tutorial by Oracle.

Parse your input string

LocalDate ld = LocalDate.parse( "2015-02-05" ) ;

Unzoned

If your business problem lacks any offset-from-UTC or time zone, is not a specific moment on the timeline, then get the first moment of a generic 24-hour day by converting to a LocalDateTime.

LocalDateTime ldt = ld.atStartOfDay() ;

ldt.toString(): 2015-02-05T00:00

To store this in database in a column of data type similar to the SQL standard type of TIMESTAMP WITHOUT TIME ZONE, use the java.time objects with a JDBC driver compliant with JDBC 4.2 and later. Better to exchange objects with your database rather than mere strings.

myPreparedStatement.setObject( … , ldt ) ;

…and…

LocalDateTime ldt = myResultSet.getObject( … , LocalDateTime.class ) ;

Zoned

If your business problem does intend an offset or zone, you must specify to get an actual point on the timeline.

The first moment of the day is not always 00:00:00. Anomalies such as Daylight Saving Time (DST) mean the day may begin at another time such as 01:00:00. Let java.time determine the first moment of the day with a call to LocalDate::atStartOfDay.

ZoneId z = ZoneId.of( "Africa/Tunis" ) ;
ZonedDateTime zdt = ld.atStartOfDay( z ) ;

zdt.toString(): 2015-02-05T00:00+01:00[Africa/Tunis]

Exchange java.time objects with your database as seen above. If you are trying to record actual moments in history, your database column should be of a type equivalent to the SQL standard TIMESTAMP WITH TIME ZONE.

myPreparedStatement.setObject( … , zdt ) ;

…and…

LocalDateTime ldt = myResultSet.getObject( … , ZonedDateTime.class ) ;

Your driver and/or database likely converts such zoned values to a UTC value. Or you may do so explicitly yourself. Extract an Instant object which is always in UTC.

myPreparedStatement.setObject( … , zdt.toInstant() ) ;

…and…

Instant instant = myResultSet.getObject( … , Instant.class ) ;
ZonedDateTime zdt = instant.atZone( z ) ;

About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.

Where to obtain the java.time classes?

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499800

Well once you get rid of time zone anomalies, you can probably just assume that every day starts at midnight, so use:

LocalDateTime localDateTime = localDate.toLocalDateTime(LocalTime.MIDNIGHT);

It's not clear why you wanted to end up at 1 second past midnight. If you really want that, create your own constant LocalTime representing that, and use it instead of MIDNIGHT in the above code.

Also note that if you've actually got a Date, you may already have lost information. It sounds like the incoming data is really a string - you'd be best off parsing that directly as a LocalDate to avoid time zones potentially causing problems.

Upvotes: 2

Related Questions