-8

I have two string variables for timer i.e.

String StartTimer1 = "00:00:00";
String EndTimer2 = "23:59:59";

Now , I need to calculate the time left from the present time (lets say if it is 05: 30:00) once the timer has started and time left has to be in milliseconds.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ashlesha
  • 109
  • 1
  • 2
  • 12
  • 1
    have you tried something ? – A4L May 10 '13 at 16:17
  • 3
    Have you heard about Date and SimpleDateFormat Classes? or worst but functional have heard about String Parse? – Ricardo Cacheira May 10 '13 at 16:18
  • http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html – user902383 May 10 '13 at 16:22
  • This is my homework. I am learning Java. I found this link and I tried it. It works, but I want simplify it more. http://stackoverflow.com/questions/11021838/parsing-duration-string-into-milliseconds – ashlesha May 10 '13 at 16:22
  • @ashlesha: if you delete this question, it will get you all lost reputation back. – mvp May 10 '13 at 16:27
  • You need to understand what is the point of your homework question. I can tell you how to do this easily in 2 ways using SimpleDateFormat or with String parse to int, but it could not be what your teacher want. – Ricardo Cacheira May 10 '13 at 16:31

1 Answers1

0

Here some tipps to get started:

You can parse the String to a Java Date like this:

SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
Date startTimer1Date = format.parse(StartTimer1);

You can get the current Date and Time like this:

Date dateNow = new Date();

And since you are working with time only (and not with date and time), you will need to manipulate the Date, Month and Year of all values to a common base, e.g. :

Calendar c = Calendar.getInstance();
c.setTime(dateNow);
c.set(Calendar.YEAR, 1970);
c.set(Calendar.DAY_OF_YEAR, 1);
dateNow = c.getTime();
Seb
  • 182
  • 2
  • 12
  • Thanks for the guidance. I really appreciate it. Everyone who visited this page , rated -ve to the question instead of atleast giving minimum guidance. I posted this question from my mom's account instead of asking the same question to her, which i am sure will be more better than posting here. THanks a lot. – ashlesha May 10 '13 at 16:33
  • With a question like that, it's not what you teacher want. But yeah it's an answer for your Question. – Ricardo Cacheira May 10 '13 at 16:43