I have a Date object (java.sql.Date
). I need to get 12 at night of that day. So I can create a time period from current time to end of the day.
How can I calculate that time with java.sql.Date
. Please send me an example.
I have a Date object (java.sql.Date
). I need to get 12 at night of that day. So I can create a time period from current time to end of the day.
How can I calculate that time with java.sql.Date
. Please send me an example.
Using java.util.Calendar
java.sql.Date sqlDate = ...;
Calendar cal = Calendar.getInstance();
cal.setTime(sqlDate);
cal.add(Calendar.DAY_OF_YEAR,1);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
java.sql.Date sqlTommorow = new java.sql.Date(cal.getTimeInMillis());
Ideally, use Joda Time which is a much better date and time API than the one in Java.
java.sql.Date sqlDate = ...;
LocalDate date = new LocalDate(sqlDate.getTime(), DateTimeZone.UTC);
LocalDate tomorrow = date.plusDays(1);
DateTime startOfDay = tomorrow.toDateTimeAtStartOfDay(DateTimeZone.UTC);
java.sql.Date sqlTomorrow = new java.sql.Date(startOfDay.getMillis());
Note that this assumes UTC everywhere... whereas I suspect you want midnight local time in which case you'll need to specify the time zone. Also note that midnight doesn't always occur on every day in all time zones due to daylight saving time - the above code will give you the start of the day instead.
Use the following code:
java.sql.Date now = new java.sql.Date( new java.util.Date().getTime() );
java.sql.Date tomorrow= new java.sql.Date( now.getTime() + 24*60*60*1000);
The java.util
Date-Time API and their formatting API, SimpleDateFormat
are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.
Also, quoted below is a notice from the home page of Joda-Time:
Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.
Solution using java.time
, the modern Date-Time API:
java.sql.Date
object to OffsetDateTime
.OffsetDateTime
to get the OffsetDateTime
(or LocalDateTime
or LocalDate
as per the type of your database field) with the required values.java.time
API with JDBC.Demo of the first two steps:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
public class Main {
public static void main(String[] args) {
// A sample data for demo - you already have this object
java.sql.Date javaSqlDate = new java.sql.Date(1234567890123L);
OffsetDateTime odtFromJavaSqlDate = new java.util.Date(javaSqlDate.getTime()).toInstant()
.atOffset(ZoneOffset.UTC);
OffsetDateTime odt = odtFromJavaSqlDate.plusDays(1).with(LocalTime.MIN);
System.out.println(odt);
// If required, convert it to LocalDateTime
LocalDateTime ldt = odt.toLocalDateTime();
System.out.println(ldt);
// If required, convert it to LocalDate
LocalDate date = odt.toLocalDate();
System.out.println(date);
}
}
Output:
2009-02-14T00:00Z
2009-02-14T00:00
2009-02-14
For any reason, if you need to convert this object of OffsetDateTime
to an object of java.util.Date
, you can do so as follows:
java.sql.Date result = new java.sql.Date(java.sql.Date.from(odt.toInstant()).getTime());
Learn more about 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.
this is probably not the nicest way, but IMHO it's relatively easy to understand. It creates a new midnight java.sql.Date object for the input date "now"
String DATE_FORMAT = "dd.MM.yyyy";
//create a date sql.Date object
java.sql.Date now = new java.sql.Date( new java.util.Date().getTime() );
//create a date formatter
SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT);
//get the midnight date by converting the object to a String and back to a Date object
java.util.Date midnightUtil = sf.parse( sf.format( now ) );
//create a new java.sql.Date object and add one days worth of milliseconds;
java.sql.Date midnight = new java.sql.Date( midnightUtil.getTime() + 86400000 );