Please tell me how to set the date (current date minus one day) and time equal to 19:00:00 using such a construction?
new java.sql.Timestamp(java.util.Calendar.getInstance.getTime().getTime())
LocalDateTime don't use.
Please tell me how to set the date (current date minus one day) and time equal to 19:00:00 using such a construction?
new java.sql.Timestamp(java.util.Calendar.getInstance.getTime().getTime())
LocalDateTime don't use.
I recommend you do it using the java.time
(the modern date-time API).
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = ZonedDateTime.now(ZoneId.systemDefault())
.minusDays(1)
.with(LocalTime.of(19, 0))
.toOffsetDateTime();
System.out.println(odt);
}
}
Output:
2020-12-24T19:00Z
You can use the OffsetDateTime
in your JDBC code as follows:
PreparedStatement st = conn.prepareStatement("INSERT INTO mytable (columnfoo) VALUES (?)");
st.setObject(1, odt);
st.executeUpdate();
st.close();
However, if you still want to use java.sql.Timestamp
, you can use ZonedDateTime
with the applicable timezone to get the required date-time and then convert it into Instant
from which you can get Epoch milliseconds. You can Finally use the Epoch milliseconds to construct an instance of java.sql.Timestamp
.
import java.sql.Timestamp;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
ZonedDateTime zdt = ZonedDateTime.now(ZoneId.systemDefault())
.minusDays(1)
.with(LocalTime.of(19, 0));
Timestamp timestamp = new Timestamp(zdt.toInstant().toEpochMilli());
System.out.println(timestamp);
}
}
Output:
2020-12-24 19:00:00.0
Notes:
ZoneId.systemDefault()
which uses the JVM's timezone. Change it to applicable timezone e.g. ZoneId.of("Europe/London")
.Instant
belongs to the modern date-time API. Learn about the modern date-time API from Trail: Date Time.import java.sql.Timestamp;
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_YEAR, -1);
calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
System.out.println(timestamp);
}
}
Output:
2020-12-24 19:00:00.0
This is my code
Set date to yesterday with Calendar.add(Calendar.DAY_OF_YEAR, -1)
Compare hour with Calendar.get(Calendar.HOUR_OF_DAY)
getHour
of java.sql.Timestamp is Deprecated.
replaced by Calendar.get(Calendar.HOUR_OF_DAY)
.
import java.sql.Timestamp;
import java.util.Calendar;
Calendar calendar = Calendar.getInstance(); // 2020-12-25 19:42:57.739
calendar.add(Calendar.DAY_OF_YEAR, -1);
Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
System.out.println(timestamp); // 2020-12-24 19:42:57.739
System.out.println(19 == calendar.get(Calendar.HOUR_OF_DAY)); // true
Bye!