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
venkateshyadav1243venkateshyadav1243 

Converting Date to words

Hi,
I have a requirement parsing csv file into salesforce(insert and update.)
I have a date field called in Period Start Date (MM/DD/YYYY.)
But my client is giving me Date Format like this MARCH 2014,
So can anyone please guide me how to convert date filed into This format MARCH 2014,APRIL 2013
a.PeriodStart__c=date.ValueOf(inputvalues[10]);

Thanks for your help and guidance
Regards,
Venkatesh
Boris BachovskiBoris Bachovski
I guess your day of month doesn't matter, so if that's the case then you can do something like this:

Map <String, Integer> months = new Map <String, Integer> {'january'=>1, 'february'=>2, 'march'=>3, 'april'=>4, 'may'=>5, 'june'=>6, 'july'=>7, 'august'=>8, 'september'=>9, 'october'=>10, 'november'=>11, 'december'=>12};

List <String> dateParts = inputvalues[10].split(' '); // split by whitespace and store elements in a list
String monthValue = dateParts[0].trim().toLowerCase(); // get the first element (month), trim whitespace and make it lowercase
Integer monthIndex = months.get(monthValue); // this is the month number (1 to 12)
Integer year = Integer.valueOf(dataParts[1].trim()); // this is the year as an integer

Date myDate = Date.newInstance(year, monthIndex, 1); // create a new date on the 1st of that month and year

David "w00t!" LiuDavid "w00t!" Liu
Here's how you could write this:
Integer day = 1;
Integer month;
Integer year;

// This line splits the text where the space is into two strings
// IE March 2014 now becomes March in the first position of the list and 2014 in the second
List<String> parsedDate = stringVariable.split(' ');

// This takes the second value in the list and makes it a year integer
year = Integer.valueOf(parsedDate[1]);

// This converts the first value in the list to an month integer
String monthString = parsedDate[0];
if (monthString.startsWith('JANUARY')) {
  month = 1;
} else if (monthString.startsWith('FEBRUARY')) {
  month = 2;
}  // repeat this for every month

// This is the date you want
Date d = Date.newInstance(year, month, day);


Boris BachovskiBoris Bachovski
Hey David that's not bad too. Have you thought about Test Coverage on that later? If, else if, else? :)
Boris BachovskiBoris Bachovski
Under
Map<string,id> offmap=new map<string,id>();
Put
Map <String, Integer> months = new Map <String, Integer> {'january'=>1, 'february'=>2, 'march'=>3, 'april'=>4, 'may'=>5, 'june'=>6, 'july'=>7, 'august'=>8, 'september'=>9, 'october'=>10, 'november'=>11, 'december'=>12};
And then replace this line
a.PeriodStart__c=date.valueof(inputvalues[9]);
with the following
List <String> dateParts = inputvalues[9].split(' '); // split by whitespace and store elements in a list
String monthValue = dateParts[0].trim().toLowerCase(); // get the first element (month), trim whitespace and make it lowercase
Integer monthIndex = months.get(monthValue); // this is the month number (1 to 12)
Integer year = Integer.valueOf(dataParts[1].trim()); // this is the year as an integer
 
a.PeriodStart__c = Date.newInstance(year, monthIndex, 1); // create a new date on the 1st of that month and year


venkateshyadav1243venkateshyadav1243
HI Boris,

I changed code when I am uploading its showing this error

System.TypeException: Invalid date: Jan-14

Boris BachovskiBoris Bachovski
Are you 100% sure that the data coming in the CSV is correct as your specified in your question? Can you please add the following before the last block of code:
System.debug(inputValues[9]);
and check the debug log to see what the actual value is?
Gaurav NirwalGaurav Nirwal
You can use this code for solving the problem

List <String> dateParts = inputvalues[10].split(' ');
String monthValue = dateParts[0].trim().toLowerCase(); 
Integer monthIndex = months.get(monthValue); 
Integer year = Integer.valueOf(dataParts[1].trim()); 
Date myDate = Date.newInstance(year, monthIndex, 1);
venkateshyadav1243venkateshyadav1243
hi Boris am not getting any value in inputvalue[9]
in debug log

am providing like this in my csv file "  jan-14 " , i tried to keep like this JANUARY 2014 in csv but i dont get any option to set date like this format
Boris BachovskiBoris Bachovski
I've provided an answer based on your input. Looks like it's different now. Based on the same logic you can work it out yourself, it's not hard. Change the map values according to your inputs, so 'january' will become 'jan' and so forth for the rest, and then change this line:

List <String> dateParts = inputvalues[9].split('-'); // split by a hyphen and store elements in a list




Boris BachovskiBoris Bachovski
And forgot to mention the year, you'll need to append '20':
Integer year = 2000 + Integer.valueOf(dataParts[1].trim()); // this is the year as an integer