How to calculate milliseconds in blackberry + java

85 Views Asked by At

can you please how to calculate milliseconds in blackberry + java .I am getting time stamp like.

static public String getTimeStamp(){

        String timeFormat = "HH.mm.ss";



        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        return sdf.format(now);
    }
2

There are 2 best solutions below

0
On BEST ANSWER

Change your code to:

    static public String getTimeStamp(){

        String timeFormat = "HH.mm.ss.SSS ";
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        return sdf.format(now).substring(timeFormat.length() - 4);         
    }

to get mislliseconds.

Another, more elegant way of doing this is:

static public String getTimeStamp(){    
        return "" + System.currentTimeMillis()%1000);
}

Because modulo by 1000 gives you milliseconds.

0
On

Try:

if (value != null) 
 {
     long parsed = HttpDateParser.parse( value );
 }

Please refer: http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/io/http/HttpDateParser.html

HttpDateParser is a powerful util class in BB when it comes to dates, i have always used in my BB time