0

I have converted date format in milliseconds and time format in milliseconds. I am getting current time in more than 13 digits. CurrentTime= 1357755780000, StartingTime=1357602840, EndingTime=1357756140

But when I do comparison in below code, the if part is not executed, only the else part is executed.

Is there any mistake in my code? I want to make currentTime in 10 digits. So I think, conversion of date format to milliseconds is wrong.

 String toParse = getDateorTime(1) + " " + getDateorTime(2);
 long currentTime=0,startingTime=0,endingTime=0,milliseconds=0;
 try 
 {
    dateFormater = new SimpleDateFormat("yyyy/MMM/dd hh:mm"); 
    Date date = null;
    try {
       date = dateFormater.parse(toParse);
       date.setTime(milliseconds);
    }catch (Exception e) {
       System.out.println("\n Error in date parsing"+e.toString());
    }
    currentTime = (date.getTime());
    start=Long.parseLong((cursor.getString(5).trim()));
    end=Long.parseLong((cursor.getString(6).trim()));
 }catch (ParseException pe) {
    pe.printStackTrace();
 }

 if((currentTime>=startingTime)&&(currentTime<=endingTime))
 {
   //
 }
halfer
  • 19,824
  • 17
  • 99
  • 186
Achiever
  • 1,626
  • 10
  • 30
  • 54

3 Answers3

1

Based on your examples, you actually have startingTime and endingTime in SECONDS, while you're comparing it to currentTime in MILLISECONDS. Simply multiply the second-times by 1,000, like so:

if((currentTime>=startingTime*1000L)&&(currentTime<=endingTime*1000L))
323go
  • 14,143
  • 6
  • 33
  • 41
  • starting time and ending time in milliseconds only – Achiever Jan 09 '13 at 06:02
  • Everything is in milliseconds only yar. Current, starting and ending time. When i do compare "if part" is not executing only else part has been executed. Current Time= 13577598690000, StartingTime=1357602840,EndingTime=1357842540 – Achiever Jan 09 '13 at 06:08
  • Correct -- StartingTime and EndingTime is in SECONDS. Just add three zeroes to either, and you'll see what I mean. – 323go Jan 09 '13 at 14:51
0

Simply divide by 1000

Calendar cal = Calendar.getInstance();

System.out.println(cal.getTimeInMillis()/1000);
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
0

Convert the long values to string and if length is >10 simply substring the value (0,10) and then you can use string .equals too or covert them back to long for comparison .

android2013
  • 415
  • 2
  • 5