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
softcloud2009softcloud2009 

How to convert string to Date format

Hi,

 

I have a string, '11/20/2010 12:00:00 PM' and i would like to set this into Date format (NOT DateTime) since i want pass this value into one of my field of type date.

 

Here my function:

 

private Date setStringToDateFormat(String myDate)
    {
        String[] myDateOnly = myDate.split(' ');
        String myDateString = myDateOnly.get(0);
        String myHourString = myDateOnly.get(1);
        DateTime d = DateTime.valueOf(myDateString + ' ' + myHourString);
        return Date.valueOf(d);
    }

 

However, everytime I run this, i received an error Invalid Date/Time.

 

Could someone help???

SSRS2SSRS2

You can use following method

 

 

private Date setStringToDateFormat(String myDate) {
   String[] myDateOnly = myDate.split(' ');
   String[] strDate = myDateOnly[0].split('/');
   Integer myIntDate = integer.valueOf(strDate[1]);
   Integer myIntMonth = integer.valueOf(strDate[0]);
   Integer myIntYear = integer.valueOf(strDate[2]);
   Date d = Date.newInstance(myIntYear, myIntMonth, myIntDate);
   return d;
}

 

 

-Suresh

_Prasu__Prasu_

use 

Datetime st = Datetime.parse('11/20/2010 12:00:00 PM');

 

works in some locales

softcloud2009softcloud2009

Thanks. It works.

Abhiraj DattaAbhiraj Datta
Hi,

Try this:
String myDate;
Date convertedDate = Date.valueOf(myDate);

Regards,
Abhiraj​
Ganesh MateGanesh Mate
Hello softcloud2009,

use this :-

String date = '2024-06-01';
Date newDate = Date.valueOf(date); 

Regards,
Ganesh