Reputation: 3535
I know that Date
class can be really painful at times.
Could you please give me some painkillers in the form of the piece of advice on how to subtract time?
What I have is:
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
String s = "13.04.2015 16:00";
String s2 = "13.04.2015 15:30";
Date date = format.parse(s);
System.out.println(timeFormat.format(date.getTime() - format.parse(s2).getTime()));
This gives me 02:30:00, but I'd love to get 00:30:00 :)
Upvotes: 3
Views: 121
Reputation: 31
I'm not sure if you are stuck to Java Date class by some requirements, but if you are working with date and time a lot in your application I recommend Joda-Time library. It solves many problems out of the box and allows executing operations on date and time in easier way.
Upvotes: 3
Reputation: 1205
I suggest to force common TimeZone
for both SimpleDateFormat
:
format.setTimeZone(TimeZone.getTimeZone("UTC"));
timeFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
before formatting date
.
Upvotes: 6