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

Date parsing

Does anyone know how to parse a String in a predefined format to a Date? Specifically, I'm trying to convert '1-Apr' to a date.  


As a Java developer, I can't believe how much time I am spending reinventing the wheel. Why is there no DateFormat, or SimpleDateFormat class? Why on earth didn't they just use Java like Google Apps does?


I can currently able to convert the day and year in Apex, ableit with tedious code.  I tried to convert the month from Apr to 4 using an enum (ordinal() returns position), but there doesn't seem to be a way to convert a String to its Enum counterpart like I can do in Java. In fact, ordinal() doesn't even seem to be compiler friendly, despite being documented (perhaps it is new to Apex 17.)


Here is what I have so far, with month obviously hard coded:


     * Dates are in the format of d-Mon in the CSV file.  E.g., "1-May" is the first
     * of may.  The year should put it in the future.  Thus, if it is "1-Jan" and it
     * is currently December of 1009, then the year should be 2010. 
    public Date convertEffectiveDate(String inDate) {
           String[] dateParts = inDate.split('-');
           Integer day = Integer.valueOf(dateParts[0]);
           Integer month = 4;
           Date effective = Date.newInstance(, month, day);
           if (effective <
           return effective;




Did you try using a map (Map<String,Integer>) instead of an enum? Mapping string to a number...




There is an equivalent of the SimpleDateFormat.parse() method in Apex. Please refer to the following link for more details:


Sample code:


datetime myDateTime =;string mydtstring = mydatetime.format();system.assertequals('12/27/2009 11:46 AM', mydtstring);





That converted a date to a string, not the other way around.  Moreover, you need to be able to specify the format.


In my case, the format is "d-MMM".  


Anyway, I caved and created a DateUtilities for my wheel reinvention process.  Here is what I created for this problem:


public with sharing class DateUtilities {
    public static List<String> MONTHS = new String[12];
    static {
        MONTHS[0] = 'Jan';
        MONTHS[1] = 'Feb';
        MONTHS[2] = 'Mar';
        MONTHS[3] = 'Apr';
        MONTHS[4] = 'May';
        MONTHS[5] = 'Jun';
        MONTHS[6] = 'Jul';
        MONTHS[7] = 'Aug';
        MONTHS[8] = 'Sep';
        MONTHS[9] = 'Oct';
        MONTHS[10] = 'Nov';
        MONTHS[11] = 'Dec';
     * Converts a month from its 3 letter abbreviation to the number of the
     * month, from 1 to 12.
    public static Integer convertMonth(String shortMonth) {
        if (shortMonth != null) {
            shortMonth = shortMonth.toUpperCase();
            Integer idx = 1;
            for (String month : DateUtilities.MONTHS) {
                if (month.toUpperCase().equals(shortMonth))
                    return idx;
        return -1;
    static testMethod void testConvertMonthToInt() {
        System.assertEquals(5, convertMonth('May'));
        System.assertEquals(7, convertMonth('JUL'));
        System.assertEquals(1, convertMonth('jan'));
        System.assertEquals(12, convertMonth('DeC'));


Not completely how I had it in mind, but it will probably work :) Can see you got a background in Java, looking at the APEX you write ;)

3 years later I hear your pain. 


At least in my case I need to validate an input field where users either select or type in a date which has to be in yyyy-MM-dd format. 

This date is passed to a SOQL query which will cry if the format is wrong or if a bad value is passed in like 9999-99-99. 


So I wrote a regex to test the format first and then used Date.valueof. This will throw an exception if the date value is wrong. I pass the exception message to the user for validation messages.  


So its a mix of reinventing the wheel and using built in stuff.