hawarden_
hawarden_

Reputation: 2160

Java : calculate remaining time compared to current time

I have a string in format :

2015-10-01 02:00

I want to print the remaining time compared to current time in Java, it should print in format :

It remains 1 day 4 hours 25 minutes 

How could I do that ? Thanks for any ideas.

Upvotes: 0

Views: 3826

Answers (1)

hawarden_
hawarden_

Reputation: 2160

I got it working! Could you withdraw all the unvotes please ?

public static void calculateRemainTime(String scheduled_date){

    // date format
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    // two dates
    java.util.Date scheduledDate;
    Calendar current = Calendar.getInstance();
    java.util.Date currentDate;
    String current_date = format.format(current.getTime());
        try {
            scheduledDate = format.parse(scheduled_date);
            currentDate = format.parse(current_date);
            long diffInMillies = scheduledDate.getTime() - currentDate.getTime();
            long diffence_in_minute = TimeUnit.MINUTES.convert(diffInMillies,TimeUnit.MILLISECONDS);
            System.out.println(diffence_in_minute);
        } catch (ParseException e) {
            e.printStackTrace();
        }
}

Upvotes: 6

Related Questions