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
Manoj ReddiManoj Reddi 

When Uploading Files using Visualforce page How to convert a text field values to a date field

Hi
I have the following text field value in excel file:
20170320

I would like to convert it to a date field value when uploading files using Apex class & Visualforce Page:
03/20/2017

Thanks

 
Best Answer chosen by Manoj Reddi
Narender Singh(Nads)Narender Singh(Nads)
Hi Manoj,

There is no direct function to do that.
You can do something like this.:
string s='20170320';
string year=s.substring(0,4);
string month=s.substring(4,6);
string day=s.substring(6);
system.debug(day+' '+month+' '+year);
Date tempDate = Date.newInstance(integer.valueof(year), integer.valueof(month), integer.valueof(day));
string FinalDate=tempDate.format();
system.debug(FinalDate);

Let me know if it helps
Thanks!

All Answers

RD@SFRD@SF
Hi Manoj,

In the controller, you can use text functions to split the text into the year, month and day and use the date function to convert the text to the date value.
Date myDate = Date.newInstance(2017, 03, 20);
If you would like to know more, follow the links below
Date functions: link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm).
Sting functions: link (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm)

Hope it helps
RD
 
Narender Singh(Nads)Narender Singh(Nads)
Hi Manoj,

There is no direct function to do that.
You can do something like this.:
string s='20170320';
string year=s.substring(0,4);
string month=s.substring(4,6);
string day=s.substring(6);
system.debug(day+' '+month+' '+year);
Date tempDate = Date.newInstance(integer.valueof(year), integer.valueof(month), integer.valueof(day));
string FinalDate=tempDate.format();
system.debug(FinalDate);

Let me know if it helps
Thanks!
This was selected as the best answer
Manoj ReddiManoj Reddi
Hi Narender,

Thanks for Reply,

You write only one text,  but i need multiple texts to date, see below one

20180228
20180228
20180228
20180228
20180228
20180228
20180116
20180228
20180228

Thanks
Manoj
 
Narender Singh(Nads)Narender Singh(Nads)
Hi Manoj,
Put all these values in a list of string type and then start a for loop to traverse that list and write date extraction logic inside that for loop and add the final dates to a new list of string type.

Thanks
Narender Singh(Nads)Narender Singh(Nads)
Hi Manoj,
Please mark the best answer if I was able to help you.

Thanks!