Skip to content

Groovy -23 – Return Current Date and Time Based on the Timezone Of The User

So Sales Cloud puts at our disposal some groovy methods that can be use to return the date and time.

However you will notice that what is return it will be UTC/GMT time, regardless of the user timezone, set in User Preferences.

 

So in order to overcome that we need to build our very own function.

The logical path of the code below will be that first we need to extract the user timezone.

Then using the user timezone we call upon the JAVA class of the Calendar(https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html )

And we use the getInstance() method of the Calendar class to be able to use the fields of this class and also to initialize them.

Once that is done we make use of another JAVA class called TimeZone (https://docs.oracle.com/javase/7/docs/api/java/util/TimeZone.html )

And we will use its getRawOffset method to get the amount of time in milliseconds to add to UTC to get standard time in the user timezone.

And after that we just do some math.

def var_securityContext = adf.context.getSecurityContext()  
 def var_UserTimeZone = var_securityContext.getUserProfile().getTimeZone()  
  
 Calendar var_calendar = Calendar.getInstance();  
 TimeZone var_tz = TimeZone.getTimeZone(var_UserTimeZone);  
 var_calendar.add(
Calendar.MILLISECOND,-(var_calendar.getTimeZone().getRawOffset())
);  
 var_calendar.add(Calendar.MILLISECOND, var_tz.getRawOffset());  
 Date myDate = new Date(var_calendar.getTimeInMillis());    
 def var_format = new Formatter()  
 def String rezult = var_format.format(
‘%tm/%td/%tY %tH:%tM:%tS’,myDate,myDate,myDate,myDate,myDate,myDate
)  
 return rezult   

Code above will return current date in the Timezone The user has set in Set Preferences and would look like this: 12/22/2016 23:35:29

 

But maybe you want to see the time in a 12h format with AM/PM.

Well that’s a bit tricky.

You see in Sales Cloud is not permitted to use Joda time API or to use java.text.SimpleDateFormat , which is a shame.

So we need to do that inside the script.

Code will look as below:

def var_securityContext = adf.context.getSecurityContext()  
 def var_UserTimeZone = var_securityContext.getUserProfile().getTimeZone()  
  
 Calendar var_calendar = Calendar.getInstance();  
 TimeZone var_tz = TimeZone.getTimeZone(var_UserTimeZone);  
 var_calendar.add(
Calendar.MILLISECOND,-(var_calendar.getTimeZone().getRawOffset())
);  
 var_calendar.add(Calendar.MILLISECOND, var_tz.getRawOffset());  
 Date myDate  
/* 
For 5 specific time-zones, I notice time is return with 1 hour less. 
This timezone are: America/Santiago ; America/Sao_Paulo ; 
Australia/Sydney ;Australia/Brisbane;Australia/Hobart 
Fix for this by compensating in code with 1 h for those 5 
specific time-zones. 
*/  
  
 if(
var_UserTimeZone == ‘America/Santiago’ ||
var_UserTimeZone == ‘America/Sao_Paulo’ || 
var_UserTimeZone == ‘Australia/Sydney’ || 
var_UserTimeZone ==‘Australia/Brisbane’ ||
 var_UserTimeZone ==‘Australia/Hobart’)  
 {  
 myDate = new Date(var_calendar.getTimeInMillis()+3600000)   
 }  
 else  
 {  
 myDate = new Date (var_calendar.getTimeInMillis())   
 }  
  
 Calendar v_Calendar = Calendar.getInstance();  
 v_Calendar.setTime(myDate);  
 String hours = v_Calendar.get(Calendar.HOUR_OF_DAY);  
 String minutes = v_Calendar.get(Calendar.MINUTE);  
 String mergeTime = hours + “.”+ minutes  
 double period = Double.valueOf(mergeTime)  
  
 def var_formatD = new Formatter()  
 def var_formatH = new Formatter()  
 def var_formatMS = new Formatter()  
 String v_date = var_formatD.format(
‘%tm/%td/%tY ‘,myDate,myDate,myDate,myDate,myDate,myDate
)  
 String v_hour = var_formatH.format(
‘%tH’,myDate,myDate,myDate,myDate,myDate,myDate
)  
 String v_minutesSec = var_formatMS.format(
‘:%tM:%tS’,myDate,myDate,myDate,myDate,myDate,myDate
)  
  
 String DisplayTime=“”  
  
 if(period >=0.0 && period <=11.59)  
 {  
 if(v_hour==“00”){  
 DisplayTime = v_date + ” “ + “12” + v_minutesSec + ” AM”  
 }  
 else{  
 DisplayTime = v_date + ” “ + v_hour + v_minutesSec + ” AM”  
 }  
 }  
 else if(period>=12.0 && period<=23.59){  
  
 switch(v_hour)  
 {  
 case “12”:  
 DisplayTime = v_date + ” “ + v_hour + v_minutesSec + ” PM”  
 break  
 case “13”:  
 DisplayTime = v_date + ” “ + “01” + v_minutesSec + ” PM”  
 break    
 case “14”:  
 DisplayTime = v_date + ” “ + “02” + v_minutesSec + ” PM”  
 break    
 case “15”:  
 DisplayTime = v_date + ” “ + “03” + v_minutesSec + ” PM”  
 break    
 case “16”:  
 DisplayTime = v_date + ” “ + “04” + v_minutesSec + ” PM”  
 break    
 case “17”:  
 DisplayTime = v_date + ” “ + “05” + v_minutesSec + ” PM”  
 break    
 case “18”:  
 DisplayTime = v_date + ” “ + “06” + v_minutesSec + ” PM”  
 break    
 case “19”:  
 DisplayTime = v_date + ” “ + “07” + v_minutesSec + ” PM”  
 break   
 case “20”:  
 DisplayTime = v_date + ” “ + “08” + v_minutesSec + ” PM”  
 break  
 case “21”:  
 DisplayTime = v_date + ” “ + “09” + v_minutesSec + ” PM”  
 break  
 case “22”:  
 DisplayTime = v_date + ” “ + “10” + v_minutesSec + ” PM”  
 break  
 case “23”:  
 DisplayTime = v_date + ” “ + “11” + v_minutesSec + ” PM”  
 break    
 }  
 }  
  
 return DisplayTime  

At this point DisplayTime will show a time as: 01/04/2017 06:53:32 PM

 

Also you could use any of the code above (get time in 24 hour clock style or 12 hour style) and create a global function.

Global function would return in both cases a String , and then you can use that global function reference in your triggers or formula fields.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *