Another Salesforce blog!!!

Salesforce, Apex

date format


 public static String formatDatetime(Datetime dt) {
        return dt.format('yyyy-dd-MM');
    }
public static Datetime parseDatetime(String dateStr) {
        if (dateStr==null)
            return null;
        
        // split the original string
        String[] tokens = dateStr.split(' ', 0);
        //String dayOfWeek = tokens[0];
        Integer day = Integer.valueOf(tokens[1]);
        Integer month = monthToInteger.get(tokens[2]);
        Integer year = Integer.valueOf(tokens[3]);
        String[] timeOfDay = tokens[4].split(':');
        String offset = tokens[5];
        
        
        // split the time substring
        Integer hour = Integer.valueOf( timeOfDay[0] );
        Integer minute = Integer.valueOf( timeOfDay[1] );
        Integer second = Integer.valueOf( timeOfDay[2] );
        
        Datetime dt = Datetime.newInstanceGmt(
                year,
                month,
                day,
                hour,
                minute,
                second
                );
                
        System.debug('DateString = \"'+dateStr+'\", Datetime = '+dt.formatLong());
        return dt;
    }

Leave a Reply

Your email address will not be published.

*