Reputation:
I am getting a variable newValue
in java as
public void reTrigger() {
Date date1 = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH.mm.ss");
squery: "select newValue from SAVE_TXN WHERE ROWID = 1"
(newValue is of type nvarchar2)
String vidtime = sdf.format(parser.getValueOf("newValue")); //parser is another method which parses the squery
String currentDate = sdf.format(date1);
}
Now newvalue contains date and time like: '2020/05/17 18.30.44'
in DB, which I am fetching into vidtime.
Now I want to subtract this vidtime with currentDate in java and check whether it is greater than or equals to 120 hours or not. How to proceed ?
Upvotes: 0
Views: 579
Reputation: 338730
You are using terrible date-time classes that were years ago supplanted by the modern java.time classes with the adoption of JSR 310.
You should be retrieving date-time values from the database as date-time objects rather than strings. As of JDBC 4.2, we can exchange java.time classes with the database. See PreparedStatement::setObject
and ResultSet::getObject
.
For a database column of type:
TIMESTAMP WITH TIME ZONE
, retrieve as a OffsetDateTime
(support for the more commonly used Instant
and ZonedDateTime
classes was unfortunately and mysteriously omitted from JDBC 4.2).
TIMESTAMP WITHOUT TIME ZONE
, retrieve as a LocalDateTime
.
If faced with a string input, parse using DateTimeFormatter
class.
Your input lacks an indicator of time zone or offset-from-UTC. So we must parse as a LocalDateTime
.
DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuu/MM/dd HH.mm.ss" ) ;
LocalDateTime ldt = LocalDateTime.parse( input , f ) ;
Capturing the current moment requires a time zone. For any given moment, the date and the time-of-day vary around the globe by zone.
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime now = ZonedDateTime.now( z ) ;
We cannot compare this current moment (zdt
) to your input, the ldt
. Your input and a LocalDateTime
object do not represent a moment, are not a specific point on the timeline. Take your example, 2020/05/17 18.30.44
. We cannot know if that means 6:30 PM on the 17th in Tokyo, in Toulouse, or in Toledo – all different moments, several hours apart on the timeline.
So your Question as written cannot be solved.
If you know for certain that a time zone was intended for your input string, apply a time zone to produce a ZonedDateTime
.
ZoneId zIntended = ZoneOf.( "Africa/Tunis" ) ;
ZonedDateTime then = ldt.atZone( zIntended ) ;
Calculate elapsed time as a Duration
.
Duration d = Duration.between( now , then ) ;
if( d.toHours() > 120 ) { … }
Upvotes: 3
Reputation: 34
Maybe something like this :
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
final LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
final LocalDateTime dateToCompare = LocalDateTime.parse("2020-05-28 02:05:45",formatter);
final long hours = Duration.between(now, dateToCompare).toHours();
System.out.print(hours);
}
}
Upvotes: 1