Search code examples
androiddate-formatmoodlemillisecondsandroid-date

How to convert current date and time format in milliseconds (only 10 digits)?


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))
 {
   //
 }

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