Hi guys, i have a String time 23:00, how do i subtract it against a String today's time and get the remaining minutes? do i parse the String into a date type? any ideas is appreciated, thanks.

public String getTime()
    {
    SimpleDateFormat sdf = new SimpleDateFormat("kk:mm");
    Date s = sdf.parse("23:00");
    return sdf.format(s);
    }
public String now()
    {
      SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
      Date d = Calendar.getInstance().getTime();
      return sdf.format(d);
    }

Recommended Answers

All 4 Replies

You have most of it there. Parse the input to a Date, get "now" as a Date, then get the time in millisecs from those. Subtract the two millisecond values and divide by 60,000 to get minutes.

Hi James, thanks for replying, i manage to get the current time in millis

System.currentTimeMillis();

but how to i get the millis for the input time?

Date s = sdf.parse("23:00");

the code above parsed the String into a date right?

i've tried

s.getTimeInMillis();

but all i get is errors.

It's rarely a good idea to just guess method names - have a look at the API doc for the Date class - you'll soon find the method you need.

yup, thanks for the info, i managed to get the millis from the String now.
Thank you James =p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.