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
Nayana KNayana K 

Invalid Date time exception only for particular date time

DateTime dtOrigin = DateTime.newInstance(2017, 03, 12, 2, 3, 0);
DateTime startDateOfSchedule = dtOrigin;
String timeVal = ' 02:03';
Date startDate = date.newinstance(startDateOfSchedule.year(), startDateOfSchedule.month(), startDateOfSchedule.day());

system.debug('=dtOrigin=='+dtOrigin);
system.debug('=startDateOfSchedule=='+startDateOfSchedule);
system.debug('=timeVal=='+timeVal);
system.debug('=startDate=='+startDate);
system.debug('=Formatted startDate=='+startDate.format());
DateTime dtVal = DateTime.parse(startDate.format()+''+timeVal);
system.debug('=dtVal=='+dtVal);

I have a problem in VF page for particular date time in apex controller. Above is snippet which I ran in dev console and got same error like appeared in VF.
In the above code, I am getting 'Invalid date/time: 12/03/2017 02:03'

If I change DateTime dtOrigin = DateTime.newInstance(2017, 03, 12, 2, 3, 0); to DateTime dtOrigin = DateTime.newInstance(2017, 03, 13, 2, 3, 0);, I won't get error of invalid datetime while parsing DT and assining into dtVal variable. I wonder, why this behaviour only for this specific date and that too for 2hours:some minutes.  

For other date and time combo, there is no error. I find hard time in understanding this.

Please help.
Banwari kevat1Banwari kevat1
Hi Nayana,
  This is the problem of your time  zone. You can use its as following:
DateTime dtOrigin = DateTime.newInstanceGmt(2017, 03, 12, 2, 3, 0);
DateTime startDateOfSchedule = dtOrigin;
String timeVal = ' 02:03 AM';
Date startDate = date.newinstance(startDateOfSchedule.year(), startDateOfSchedule.month(), startDateOfSchedule.day());

system.debug('=dtOrigin=='+dtOrigin);
system.debug('=startDateOfSchedule=='+startDateOfSchedule);
system.debug('=timeVal=='+timeVal);
system.debug('=startDate=='+startDate);
system.debug('=Formatted startDate=='+startDate.format());
DateTime dtVal = DateTime.parse(startDate.format()+''+timeVal);
system.debug('=dtVal=='+dtVal);
Please let me know if you have any query and Mark as answer if you have got.

Thanks 
Banwari
Nayana KNayana K
Hi Banwari,

Sorry, it din't work.
Banwari kevat1Banwari kevat1
Hi Nayana,
  Please tell me what errors occuring now. This is working for me.You also see log below:
User-added image

Thanks
Banwari
Nayana KNayana K
Oh it din't worked for me.
Time Zone: (GMT-05:00) Eastern Standard Time (America/New_York)
Locale : English (United States)

What's yours?
Banwari kevat1Banwari kevat1
My Time zone also like yours time zone.

User-added image

Its working for me. Can you please share latest code for which you are trying currently. I will analyse your code.
Nayana KNayana K
<apex:page id="pg" tabStyle="Meeting__c" controller="ScheduleMeetingController">
    <c:Resources />
  
    <style>
        .pbSubheader { background-image: url("/img/alohaSkin/opacity75.png"); }
        .apexp .bPageBlock.apexDefaultPageBlock .pbBody { margin: 0px; }
        .requiredLegend { background-color: transparent; }
        h3 { font-size:1.2em; color:black; padding-left:1%; }
    </style>
  
    <apex:sectionHeader title="Meeting Edit" subtitle="New Meetings"/>
  
    <apex:form id="frm">
    <apex:pageBlock id="pb" title="Meeting Edit">
      <apex:pageMessages escape="false"></apex:pageMessages>
      
      <apex:pageBlockButtons >
        <apex:commandButton value="Save" action="{!saveMeetings}" status="busyCursor" rerender="frm"/>
        <apex:commandButton value="Cancel" action="{!cancelMeetings}" immediate="true" status="busyCursor" rerender="frm"/>
      </apex:pageBlockButtons>
      
      <div class="pbSubheader brandTertiaryBgr first tertiaryPalette">
        <span class="pbSubExtra">
          <span class="requiredLegend brandTertiaryFgr">
            <span class="requiredExampleOuter">
              <span class="requiredExample"> </span>
            </span>
            <span class="requiredText"> = Required Information</span>
          </span>
        </span>
        
        <h3>Information</h3>
      </div>
      
      <apex:pageBlockSection id="pbs1" collapsible="false" columns="2">
        <apex:inputField label="Meeting Name" value="{!newMeeting.Name}" required="true"/>
        <apex:inputField label="Facility" value="{!newMeeting.Facility__c}"/>
        <apex:inputField label="Class" value="{!newMeeting.Class__c}"/>
        <apex:inputField label="Primary Facilitator" value="{!newMeeting.Primary_Facilitator__c}"/>
        <apex:inputField label="Description" style="width: 75%;" value="{!newMeeting.Description__c}"/>
        <apex:inputField label="Secondary Facilitator" value="{!newMeeting.Secondary_Facilitator__c}"/>
        <apex:inputField label="Special Expenses" style="width: 75%;" value="{!newMeeting.Special_Expenses__c}"/>
        <apex:inputField label="Tertiary Facilitator" value="{!newMeeting.Tertiary_Facilitator__c}"/>
      </apex:pageBlockSection>
      
            <apex:pageBlockSection id="pbs2" title="Recurrence" collapsible="false" columns="1">
                <apex:pageBlockSectionItem id="pbsi1">
                <apex:outputLabel id="freq" value="Frequency"/>
                <apex:outputPanel style="padding:0px;margin:0px;">
                    <input type="radio" name="frequency" value="Weekly" checked="true" onchange="setSelectedFrequency(this);"/>&nbsp;Weekly
                    <apex:selectCheckboxes id="daysCheckBoxes" value="{!days}" style="display: inline-table;">
                        <apex:selectOptions value="{!daysItems}"/>
                    </apex:selectCheckboxes><br/>
                    <input type="radio" name="frequency" value="Bi-Weekly" onchange="setSelectedFrequency(this);"/>&nbsp;Bi-Weekly<br/>
                    <input type="radio" name="frequency" value="Monthly" onchange="setSelectedFrequency(this);"/>&nbsp;Monthly
                    <apex:inputHidden value="{!selectedFrequency}" id="selectedFreq"/>
                </apex:outputPanel>
                </apex:pageBlockSectionItem>
                <apex:inputField label="Start Date" value="{!startMeeting.Date_Time__c}"/>
                <apex:inputField label="End Date" value="{!endMeeting.Date_Time__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
  
  <script>
    function setSelectedFrequency(selectedRadio) {
      j$("#pg\\:frm\\:pb\\:pbs2\\:pbsi1\\:daysCheckBoxes").hide();
      if(j$(selectedRadio).val() == "Weekly"){
        j$("#pg\\:frm\\:pb\\:pbs2\\:pbsi1\\:daysCheckBoxes").show();
      }
      j$("#pg\\:frm\\:pb\\:pbs2\\:pbsi1\\:selectedFreq").val(  (selectedRadio).val());
    }
  </script>
</apex:page>
 
/**
 * Controller for ScheduleMeeting.page. This class create bulk rocords for Meeting object
 */
public with sharing class ScheduleMeetingController {
  //--------------------------------------------------------------------------
  // Properties
  public Meeting__c startMeeting{get;set;}
  public Meeting__c endMeeting{get;set;}
  public String selectedFrequency{get;set;}
  public Meeting__c newMeeting{get;set;}
  public List<String> days{get;set;}
  public List<SelectOption> daysItems{get;set;}
  
  private String clsId;
  
  /**
   * Container for Meeting__c
   */
  public static EnforcingCRUD_FLS.ObjectPropertiesContainer meetingContainer = new EnforcingCRUD_FLS.ObjectPropertiesContainer(
	// object
	Meeting__c.sObjectType,

	// fields
	new Schema.sObjectField[] {		
		Meeting__c.Name,
		Meeting__c.Class__c,
		Meeting__c.Facility__c,
		Meeting__c.Primary_Facilitator__c,
		Meeting__c.Secondary_Facilitator__c,
		Meeting__c.Description__c,
		Meeting__c.Tertiary_Facilitator__c,
		Meeting__c.Special_Expenses__c,
		Meeting__c.Date_Time__c
	}
  );
  
  //--------------------------------------------------------------------------
  // Methods
  public ScheduleMeetingController() {
    clsId = Apexpages.currentPage().getParameters().get('id');
    
    newMeeting = new Meeting__c();
    newMeeting.Class__c = clsId;
    newMeeting.Date_Time__c = system.now();
    
    startMeeting = new Meeting__c();
    startMeeting.Date_Time__c = system.now();
    
    endMeeting = new Meeting__c();
    endMeeting.Date_Time__c = system.now();
    
    selectedFrequency = 'Weekly';
    days = new List<String>();
    daysItems = new List<SelectOption>{
      new SelectOption('Sunday','Sunday'),
      new SelectOption('Monday','Monday'),
      new SelectOption('Tuesday','Tuesday'),
      new SelectOption('Wednesday','Wednesday'),
      new SelectOption('Thursday','Thursday'),
      new SelectOption('Friday','Friday'),
      new SelectOption('Saturday','Saturday')
    };
  }
  
  public PageReference saveMeetings() {
	if (!EnforcingCRUD_FLS.isCreatable(
	  new EnforcingCRUD_FLS.ObjectPropertiesContainer[] {
	    meetingContainer
	  })
	) {
      // Insufficient access
      return null;
    }

    PageReference result;
    if (startMeeting.Date_Time__c < System.now()) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.SEVERITY.ERROR, 'Meeting start date-time must be future date-time.'));
      return result;
    }

    List<Meeting__c> newMeetings = new List<Meeting__c>();
    
    DateTime startDateOfSchedule = startMeeting.Date_Time__c;
    String timeVal = (startDateOfSchedule.format()).subStringAfter('/'+startDateOfSchedule.year());
    Date startDate = date.newinstance(startDateOfSchedule.year(), startDateOfSchedule.month(), startDateOfSchedule.day());
    Date EndDate = date.newinstance(endMeeting.Date_Time__c.year(), endMeeting.Date_Time__c.month(), endMeeting.Date_Time__c.day());
    if (selectedFrequency == 'Weekly') {
      List<DateTime> meetingScheduledDates = new List<DateTime>();
      
      if (days.size() > 0) {
        Set<String> daysSet = new Set<String>();
        daysSet.addAll(days);
        
        while (startDate <= EndDate) {
          DateTime dtVal = DateTime.parse(startDate.format()+''+timeVal);
          if (daysSet.contains(dtVal.format('EEEE'))) {
            meetingScheduledDates.add(dtVal);
          }
          
          startDate = startDate.addDays(1);
        }
      } else {
        while (startDate <= EndDate) {
          DateTime dtVal = DateTime.parse(startDate.format()+''+timeVal);
          meetingScheduledDates.add(dtVal);
          startDate = startDate.addDays(7);
        }
      }
      
      for (DateTime scheduleDate: meetingScheduledDates) {
        Meeting__c currentMeeting = new Meeting__c();
        currentMeeting.Name = newMeeting.Name+' - '+scheduleDate.format();
        currentMeeting.Class__c = newMeeting.Class__c;
        currentMeeting.Facility__c = newMeeting.Facility__c;
        currentMeeting.Primary_Facilitator__c = newMeeting.Primary_Facilitator__c;
        currentMeeting.Secondary_Facilitator__c = newMeeting.Secondary_Facilitator__c;
        currentMeeting.Description__c = newMeeting.Description__c;
        currentMeeting.Tertiary_Facilitator__c = newMeeting.Tertiary_Facilitator__c;
        currentMeeting.Special_Expenses__c = newMeeting.Special_Expenses__c;
        currentMeeting.Date_Time__c = scheduleDate;
        newMeetings.add(currentMeeting);
      }
    } else if (selectedFrequency == 'Bi-Weekly') {
      while (startDate <= EndDate) {
        DateTime dtVal = DateTime.parse(startDate.format()+''+timeVal);
        Meeting__c currentMeeting = new Meeting__c();
        currentMeeting.Name = newMeeting.Name+'-'+dtVal.format();
        currentMeeting.Class__c = newMeeting.Class__c;
        currentMeeting.Facility__c = newMeeting.Facility__c;
        currentMeeting.Primary_Facilitator__c = newMeeting.Primary_Facilitator__c;
        currentMeeting.Secondary_Facilitator__c = newMeeting.Secondary_Facilitator__c;
        currentMeeting.Description__c = newMeeting.Description__c;
        currentMeeting.Tertiary_Facilitator__c = newMeeting.Tertiary_Facilitator__c;
        currentMeeting.Special_Expenses__c = newMeeting.Special_Expenses__c;
        currentMeeting.Date_Time__c = dtVal;
        newMeetings.add(currentMeeting);
        startDate = startDate.addDays(15);
      }
    } else if (selectedFrequency == 'Monthly') {
      while(startDate <= EndDate) {
        DateTime dtVal = DateTime.parse(startDate.format()+''+timeVal);
      
        Meeting__c currentMeeting = new Meeting__c();
        currentMeeting.Name = newMeeting.Name+'-'+dtVal.format();
        currentMeeting.Class__c = newMeeting.Class__c;
        currentMeeting.Facility__c = newMeeting.Facility__c;
        currentMeeting.Primary_Facilitator__c = newMeeting.Primary_Facilitator__c;
        currentMeeting.Secondary_Facilitator__c = newMeeting.Secondary_Facilitator__c;
        currentMeeting.Description__c = newMeeting.Description__c;
        currentMeeting.Tertiary_Facilitator__c = newMeeting.Tertiary_Facilitator__c;
        currentMeeting.Special_Expenses__c = newMeeting.Special_Expenses__c;
        currentMeeting.Date_Time__c = dtVal;
        newMeetings.add(currentMeeting);
        startDate = startDate.addMonths(1);
      }
    }
      
    Savepoint sp = Database.setSavepoint();
    try {
      insert newMeetings;
      result = new Pagereference('/' + clsId);
    } catch (Exception ex) {
      ApexPages.addMessages(ex);
      CfG.debugException(ex);
    }
    
    return result;
  }
   
  public Pagereference cancelMeetings() {
    return new Pagereference('/' + clsId);
  }
}



User-added image


If you check above image, On click of save, it seems like without going to server side controller, this error is coming from client side.