Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
lynxgeek
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720

Date Conversion problem from long to String

 i have used the following code to convert but its giving me jan-15 1970:7:32 instead of original time..

 

 

public static String getDate(long d) {
        Calendar cal = Calendar.getInstance();
        Date date=new Date(d);         //     here it is returning jan 15,1970...
        cal.setTime(date);
        String str = monts[cal.get(Calendar.MONTH)] + "-";
        str += cal.get(Calendar.DAY_OF_MONTH) + "-" + cal.get(Calendar.YEAR)+"-";
        str += cal.get(Calendar.HOUR)+":"+cal.get(Calendar.MINUTE);
        return str;
    }

Regular Contributor
Plato
Posts: 71
Registered: ‎07-11-2010
My Device: Bold 9000

Re: Date Conversion problem from long to String

what is

 

> long d

 

maybe it's a time in seconds and you need to convert it to milliseconds ?

 

Date date=new Date(d * 1000);


-----
A good decision is based on knowledge and not on numbers.
Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Date Conversion problem from long to String

To Get Current Date and Time

U can use

 

Simple Use This.   

 

import java.util.Date;

import net.rim.device.api.i18n.*;

    String date = new SimpleDateFormat("dd/MM/yyyy hh:mm").format(new Date(System.currentTimeMillis()));
        add(new RichTextField(date));

 

-------------------------------------------------------------------------------------------------------

Use Kudoed Button If got Solution. Or Press Accept as Solution Button.

Developer
lynxgeek
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720

Re: Date Conversion problem from long to String

thanks for the replay...

 

 

can you people convert the following unix time in to date which should have hpurs and minutes with date..

 

 

the unix time is......1279958400(i think the unix time returns a long value am i right?)

 

 

 

 

Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Date Conversion problem from long to String

I think This will Help u.

 

DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");

       long now = System.currentTimeMillis();

       Calendar calendar = Calendar.getInstance();

       calendar.setTimeInMillis(now);        
       System.out.println(now + " = " + formatter.format(calendar.getTime()))
---------------------------------------------------------------------------------------------------------------------------------------
Feel Free to Press Kudoed Button, if got Solution.
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Date Conversion problem from long to String

Plato is correct.  Since the Unix long value is in seconds, and the BlackBerry expects Milliseconds, you need to multiply the unix long by 1000 before doing any 'long to String' conversions.

Developer
lynxgeek
Posts: 272
Registered: ‎05-24-2010
My Device: DEV Alpha C & BB 9720

Re: Date Conversion problem from long to String

 

thanks allot..

i got exact date and time..

 

i modified and  used as ...

 

 

 

DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
                long dateLong =1279969200;
                Date date = new Date(dateLong*1000);
                String dateString =df.format(date);
                Dialog.alert("Date string:\n" +dateString);

Developer
nitinverma274
Posts: 950
Registered: ‎06-22-2010
My Device: 9550

Re: Date Conversion problem from long to String

Plz Mark as Solved Thread also.

 

Thanks

 

Contributor
manishsingla
Posts: 13
Registered: ‎03-09-2010
My Device: bold 9000

Re: Date Conversion problem from long to String

[ Edited ]

long value=123456789;

 DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm");
 df.formatLocal(value);