function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Vasco SIlveiroVasco SIlveiro 

Convert number to string without losing the zero

Hey ,

I´ve create a buttom where it creates a password based on the birthday of my account (YYYYMMDD) , the problem is that when we the day/month as a zero its ignored . Any one has a solutions:

Eg: Birthday : 1992/05/09
      Password : 199259 instead of 19920509

This is the code
 if( resultados != null && resultados.size() > 0){
          
            for(Account  result: resultados){
                    
                    result.Username__c = result.NIF__c;
                    result.Password__c = ''+result.PersonBirthdate.day() + result.PersonBirthdate.month() + result.PersonBirthdate.year();
            } 
        }

Thank u in advanced
Best Answer chosen by Vasco SIlveiro
Boom dev9xBoom dev9x
I would create variables for the day and month.

It will look like this:

            for(Account  result: resultados){
                    String bday = (result.PersonBirthdate.day()>=10)?''+result.PersonBirthdate.day()):'0'+result.PersonBirthdate.day();
                    String mo = (result.PersonBirthdate.month()>=10)?''+result.PersonBirthdate.month()):'0'+result.PersonBirthdate.month();
                    result.Username__c = result.NIF__c;
                    result.Password__c = bday + mo + result.PersonBirthdate.year();
            } 
 

All Answers

Boom dev9xBoom dev9x
I would create variables for the day and month.

It will look like this:

            for(Account  result: resultados){
                    String bday = (result.PersonBirthdate.day()>=10)?''+result.PersonBirthdate.day()):'0'+result.PersonBirthdate.day();
                    String mo = (result.PersonBirthdate.month()>=10)?''+result.PersonBirthdate.month()):'0'+result.PersonBirthdate.month();
                    result.Username__c = result.NIF__c;
                    result.Password__c = bday + mo + result.PersonBirthdate.year();
            } 
 
This was selected as the best answer
Vishnu VaishnavVishnu Vaishnav
Hi,

Here is modified code::
if( resultados != null && resultados.size() > 0){       
    for(Account  result: resultados){            
        result.Username__c = result.NIF__c;
        string d = result.PersonBirthdate.day()+'';
        string m = result.PersonBirthdate.month()+'';
        string y = result.PersonBirthdate.year()+'';
        if( m.length() == 1 )
            m='0'+m;
        if( d.length() == 1 )
            d='0'+d;
        result.Password__c = d+''+m+''+y;
    } 
}

:::======================================================================:::
Qusetion Solved ? then mark as best answer to make helpful to others .....
Frédéric TrébuchetFrédéric Trébuchet
Hi,

This one seems more simple and respond to the requirement (password = yyyymmdd):
result.Password__c = ''+
  (((result.PersonBirthdate.year()*100 + result.PersonBirthdate.month())*100) + result.PersonBirthdate.day());
I've tried using constant values, not using date methods, but should works too.
Here what I've tested:
public static String getPass() {
    String pass;
    pass = ''+(((1962*100 + 09)*100) + 5);
    system.debug(pass);
    pass = ''+(((1962*100 + 05)*100) + 5);
    system.debug(pass);
    pass = ''+(((1962*100 + 09)*100) + 25);
    system.debug(pass);
}
Hope this helps,
Fred