Question

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))
 {
   //
 }
Was it helpful?

Solution

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))

OTHER TIPS

Simply divide by 1000

Calendar cal = Calendar.getInstance();

System.out.println(cal.getTimeInMillis()/1000);

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 .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top