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
SFmaverickSFmaverick 

Take Text Input, Output Date

Ok, very simply I'm having trouble with a part of my visualforce page and controller where I try to take a user's input (Text format date in the form of DD/MM/YYYY) and then output it as a date.

 

I understand that BegDate is initialized to 'Null' when the page is loaded because the controller is compiled before the page, what I don't know is how to fix that.

 

I'm getting the error:

 

 

System.TypeException: Invalid integer: BegDate

Class.TestViewController.stringToDate: line 19, column 20 Class.TestViewController.getDayDate: line 7, column 16 External entry point


 

 

My VF Page Code is:

 

 

<apex:page showHeader="false" standardStyleSheets="false" controller="TestViewController">
  <apex:form >
    <apex:inputText value="{!BegDate}"/>
    
    <apex:outputText value="{!DayDate}"/>
  </apex:form>
</apex:page>

 My Controller Code is:

 

 

public class TestViewController {

    public String BegDate { get; set; }

    // Turns the User's string input into a date
    Public Date getDayDate(){
        return stringToDate('BegDate');
    }
    
    //Converts a string from mm/dd/yyyy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      if(s.length()== 0)
      {
      return NULL;
      }
      else{
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
      }
    }
}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
CaptainObviousCaptainObvious

Try this:

 

 

public class TestViewController {

    public String BegDate { get; set; }

    // Turns the User's string input into a date
    public Date getDayDate(){
        if (BegDate==null) {
            return null;
        } else {
            return stringToDate(BegDate);
        }
    }
    
    //Converts a string from mm/dd/yyyy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
    }
}

 

 

All Answers

hisrinuhisrinu

Are you looking for this?

 

 

date mydate = date.parse('12/27/2009');

SFmaverickSFmaverick

Thanks for hte reply Srini, that code is certainly much more convienent than the method I wrote, however it's not solving the issue I'm having.

 

When I use your code, I'm getting a different error, but for the same reason. The error is:

 

 

System.NullPointerException: Argument 1 cannot be null

Class.TestViewController.getDayDate: line 7, column 27 External entry point 

 

I understand that it's because BegDate is Null when the controller is initially compiled, but I have no idea how to get around this. How can I stop it from running the getter for DayDate until after the user has entered input?

 

CaptainObviousCaptainObvious

Try this:

 

 

public class TestViewController {

    public String BegDate { get; set; }

    // Turns the User's string input into a date
    public Date getDayDate(){
        if (BegDate==null) {
            return null;
        } else {
            return stringToDate(BegDate);
        }
    }
    
    //Converts a string from mm/dd/yyyy to a date
    public Date stringToDate(String s){
      //Input Date String is in the format mm/dd/yyyy
      String[] stringDate = s.split('/');
      Integer m =  Integer.valueOf(stringDate[0]);
      Integer d = Integer.valueOf(stringDate[1]);
      Integer y = Integer.valueOf(stringDate[2]);
      return date.newInstance(y,m,d);
    }
}

 

 

This was selected as the best answer
SFmaverickSFmaverick

TY SO MUCH CAPTAIN OBVIOUS.

 

Funny thing is, the solution was pretty obvious and I couldn't figure it out.