title: How to get formatted DateTime irrespective of the time zone in salesforce
body: <p>We use <code>formatGMT()</code> or <code>format()</code> methods of date time object in apex, it returns the formatted date based on the time zone. Although what should be done if we have a static date, such as date of birth that needs to be formatted, irrespective of the local time zone.<br />
  In my scenario when using <code>DateTime.format()</code>, The date of birth is showing correctly for users in India, although for the users in US, yesterday's date is displayed in some cases. In some cases it's converted correctly.</p>
  <p>For eg. The dob for x person is 1995-09-20 which gets formatted to 09/20/1995,
  Whereas for y person, the same method generates a formatted date 05/15/1990 when dob is 1990-05-16.<br />
  Basically the formatted date should be the exact replica of the dob, irrespective of time zone.</p>
  <p>Method:</p>
  <pre><code>private static String localeFormatDate (Date dateValue) {
      try{
          String format = String.isNotBlank (strDateFormat) ? strDateFormat: 'MM/dd/YYYY';
          Date myDate = Date.newInstance (dateValue.year(), dateValue.month(), dateValue.day()); 
          Time myTime = Time.newInstance(0, 0, 0, 0); 
          return DateTime.newInstanceGMT (myDate, myTime). format GMT (format);
      } catch (Exception ex){
          return null;
      }
  </code></pre>
  <p>}</p>
  <p>Additionally, I have also tried to set timezone as Asia/Colombo as the date is correctly formatted in that time zone:</p>
  <pre><code>return DateTime.newInstanceGMT(myDate, myTime).format (format, 'Asia/Colombo');
  </code></pre>
  <p>Although I'm not sure if that would generate the correct format date based on the when the method runs.</p>