How to convert a text to dateTime

  • How can i convert a string like this Wed Aug 07 04:30:00 GMT 2013 to a dateTime value?

  • Map <String, Integer> monthNames = new Map <String, Integer> {'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12};
    List <String> stringParts = 'Wed Aug 07 04:30:00 GMT 2013'.split(' ');
    List <String> timeParts = stringParts[3].split(':');
    
    DateTime yourDateVariable = DateTime.newInstanceGmt(Integer.valueOf(stringParts[5]), monthNames.get(stringParts[1]), Integer.valueOf(stringParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));
    

    not working any other suggestion

    I just executed the above code in the developer console and this is the result - `16:53:54:420 USER_DEBUG [6]|DEBUG|----------2013-08-07 04:30:00`

    sorry my mistake

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM