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
rihan007
Posts: 455
Registered: ‎01-14-2009
My Device: Apple iPhone 3GS 16GB
Accepted Solution

Issue with Calendar ?

hi all,

 

I am trying to read the date ("27/05/1984 00:02:12") and print it. I am using the following code which just reads the date and creates a calendar object, then converts it to long and print it 

 

 

     String date="27/05/1984 00:02:12";  
            SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");  
            String[] parts =    Utils.split(date, ' ');
            String[] dparts =   Utils.split(parts[0], '/');
            String[] tparts =   Utils.split(parts[1], ':');
                
            Calendar getCal = Calendar.getInstance();
            
             getCal.set(Calendar.DAY_OF_MONTH,Integer.parseInt(dparts[0])); 
            getCal.set (Calendar.MONTH,Integer.parseInt(dparts[1] ));  
            getCal.set(Calendar.YEAR,Integer.parseInt(dparts[2]));
            
            getCal.set (Calendar.HOUR,Integer.parseInt(tparts[0] ));
            getCal.set(Calendar.MINUTE,Integer.parseInt(tparts[1]));
            getCal.set(Calendar.SECOND,Integer.parseInt(tparts[2])); 

            System.out.println("Date : "+sdf.formatLocal(getCal.getTime().getTime()));

 The output I am expecting is "27/05/1984 00:02:12" But I am getting "27/06/1984 00:02:12" . The code is adding a month to the provided string for some reason. I cant find out why. please could anyone help?

 

thanks

 

 

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Issue with Calendar ?

[ Edited ]

January is month zero.  Putting this another way, if you set:

 

getCal.set (Calendar.MONTH,1);

 

you are setting a date in February.  So you need to code this:

 

getCal.set (Calendar.MONTH,Integer.parseInt(dparts[1] )-1);

Developer
rihan007
Posts: 455
Registered: ‎01-14-2009
My Device: Apple iPhone 3GS 16GB

Re: Issue with Calendar ?

Thanks peter_strange.

 

who else would come back so quick with an answer if not you. Kudos!!!!