+ Start a Discussion
Vida YirenkyiVida Yirenkyi 

Current time issues

Hi,
In user debug:
DateTime currentTime = DateTime.now();
system.debug(currentTime);

The time output is an hour late but the time stamp value is correct. my locale is BST UK.
How can I get the debug log to give me the correct uk time.

Thank you

Vida

 
sandhya reddy 10sandhya reddy 10
Hi vida,

Try below code
public class MaintCalendar {

  String LongDate = ' ';

  // Init cLongDate with the current Datetime in long date format    
  public String getLongDate() {
    Datetime cDT = System.now();
    LongDate = cDT.format('EEEE, MMMM d, yyyy');
    return LongDate;
  }

}
and u have other functions too
System.now() - for date time in GMT
System.today() - just the date
System.currentTimeMillis - returns the time in milli seconds

If this answers your question please mark it as solved(BestAnswer)

Thanks and Regards
sandhya
 
Amit Chaudhary 8Amit Chaudhary 8
 
Datetime current = System.now(); // returns date time value in GMT time zone.
System.debug('---current ---->'+current );

TimeZone tz = UserInfo.getTimeZone();
System.debug('Display name: ' + tz.getDisplayName());
Please check below post for timezone
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_timezone.htm

Let us know if this will help you

Thanks
Amit CHaudhary
Vida YirenkyiVida Yirenkyi
Thanks Amit, I have run the code but the time stamp and user debug value is an hour different. Kind regards Vida
Vida YirenkyiVida Yirenkyi
Hi Sandhya, Thank you for the response. I have saved the class. How can I display it? Kind regards Vida
sandhya reddy 10sandhya reddy 10
Hi vida ,
use same system.debug(LongDate);

Thanks and Regards
sandhya
sandhya reddy 10sandhya reddy 10
Hi Vida,
I found that this will return you right time.

TimeZone timeZone = TimeZone.getTimeZone("London");
 Try this and let us know.

Thanks and Regards
sandhya
sandhya reddy 10sandhya reddy 10
put in single quotes
TimeZone timeZone = TimeZone.getTimeZone('London');
Vida YirenkyiVida Yirenkyi
Thanks Sandhya,

The compiler says "variable doesn't exist LongDate"
Vida YirenkyiVida Yirenkyi
I put : TimeZone timeZone = TimeZone.getTimeZone('London'); system.debug(timeZone); error: //Line: 1, Column: 21 Static methods cannot be invoked through an object instance: getTimeZone(String)
Vida YirenkyiVida Yirenkyi
Thank you gentlemen for your help. I have solved it.

string myDateTime = DateTime.now().format();
system.debug(myDateTime);
Jonye KeeJonye Kee

We demand that all our master jacks of all trades are completely affirmed and time-served.

You can check this out on West London handyman (https://onegoodhandyman.co.uk/)