I am new to Java. I have a requirement to calculate the number of days, hour, time, sec from a given date to today, current time.
For example, I am getting 2 fields from database: Date and Time.
Date field is in the format Wednesday 02-October-2022
and Time field in the format 11:51:1 PM
In my Java class, I am able to pass these 2 field in String format:
public String getLastRun(String failedDate, String failedTime)
{
}
String failedDate, String failedTime
are the Date and Time I got from DB in above format.
Now the issue is I need to calculate the number of days, hour, time, sec passed from that Date and time taken as input.
So, if current date is Thursday 02-November-2022
and time is 11:51:1 PM
, I can calculate the number of days, hour, time, sec passed.
Example: Output: Last service failed 30 Days 12 hours 55 minutes 45 seconds
Kind of output calculating the given Date and time field as input to this current Date and time?
So far, I have taken this Date and Time as input in this format of Wednesday 02-October-2022
and 11:51:1 PM
in the Java class, but I am still not sure about:
- How to generate or fetch current Date and Time in this format in Java code?
- How to calculate the number of days, hour, time, sec passed from the given format with current Date and Time?
I have checked these following links: Link1, Link2 with local dates, but none of these have helped me to get the number of days, hour, time, sec from the given date and time format dynamically.
I even tried with SimpleDateFormat as mentioned here but not able to get with this format.
Any sample or pointer to start with will be helpful.
Pls Note: Time zone is IST Asia/Kolkata