Friday, November 2, 2012

Convert String to Timezone with or without Day Light Savings using JODA DateTime

Handing date and time using java.utils.Date or java.utils.Calendar can be a bit problematical especially when you have to do date and time conversions according to time zones. JODA-Time api is an excellent alternative to do so and provides clear and concise methods to handel date and time in an efficient way.
In this post, I'll show how to convert a raw date time string first into a datetime object and then converting it into a specific time zone related date and time. You can download the api from here .

A word of caution: do not use JODA with SDK date methods as java.utils.Date uses index 0 (jan is indexed as 0 etc) and JODA uses index 1 for months and even for days.

  //the raw date-time value, before formatting  
       String rawValue = "201210310300" ;  
       //output format of date or time  
       String format = "yy/MM/dd";  
       // get the timeZoneID  
       String timeZoneId = "America/Denver";  
       // get the dst value (true/false)  
       //if dst = false , offset = standard or raw offset  
       boolean dst = true;  
       // create a date formatter object to specify raw date-time string is in which format  
        DateTimeFormatter dtf = DateTimeFormat.forPattern("yyyyMMddHHmm");  
       // convert rawValue String into DateTime Object in UTC  
       DateTime oldDateTime = dtf.parseDateTime(rawValue).withZoneRetainFields(DateTimeZone.UTC);  
       //object to store converted date-time  
       DateTime newDateTime = new DateTime();  
       // create a TimeZone object wrt America/Denver  
       TimeZone tz = TimeZone.getTimeZone(timeZoneId);  
       // Use timezone to create a DateTimeZone object  
       DateTimeZone dtz = DateTimeZone.forTimeZone(tz);  
       // calculate the standard offset in hours, -5 in case of America/Denver  
       int standardOffset = dtz.getStandardOffset(oldDateTime.getMillis())/3600000;  
       // / calculate the dst offset in hours, -4 in case of America/Denver  
       int dstOffset = dtz.getOffset(oldDateTime.getMillis())/3600000;  
       // check if dst is enabled and adjust the date and time accordingly  
         newDateTime = oldDateTime.plusHours(dstOffset);  
         newDateTime = oldDateTime.plusHours(standardOffset);  
       // finally convert the new formatted date-time into a string  
       String formattedRawDate = newDateTime.toString("yyyyMMddHHmm");  

Thursday, October 11, 2012

Windows Batch Processing : retry execution of commands

Sometimes while working with windows batch file, you want to re-execute steps if they fail. This can be achieved in many ways. In this post, I'll use a user defined variable, a built in ERRORLEVEL variable and batch labels to achieve the same.  

ERRORLEVEL is a built in DOS variable which is automatically set to 0 if the command executes successfully but there is a catch. In few operating systems, ERRORLEVEL can be set to a negative value if a command is unsuccessful. The ideal way to check and to direct the flow of command execution is by checking IF %ERRORLEVEL% NEQ 0 

Thanks to WebGuru’s Blog for this tip but script written by him wasn't working. 

The batch script is as follows :

 rem set a retry variable initially to 0  
 set retry=0  
 set step=1  
 @echo (%date%%time%) Step 1 : copying file  
 copy "C:\temp\file1" "E:\temp\file1"  
 @echo Errorlevel is %ERRORLEVEL%  
 if %ERRORLEVEL% NEQ 0 goto retry  
 rem re-set the retry number if success  
 set retry=0  
 set step=2  
 @echo (%date%%time%) Step 2 : moveing file  
 ren "E:\temp\file1" "E:\temp\file2"  
 @echo Errorlevel is %ERRORLEVEL%  
 if %ERRORLEVEL% NEQ 0 goto retry  
 rem re-set the retry number if success  
 set retry=0  
 set step=3  
 @echo (%date%%time%) Step 3 : delete previous file  
 rm "C:\temp\file1"  
 @echo Errorlevel is %ERRORLEVEL%  
 rem At the last step, direct the flow to the end of file otherwise it will execute retry block  
 if %ERRORLEVEL% EQU 0 goto eof  
 if %ERRORLEVEL% NEQ 0 goto retry  
 set /a retry=%retry%+1  
 @echo (%date% %time%) There was an error at STEP%step%, retrying again!  
 if %retry% LSS 6 (goto :step%step%)  
 if %retry% EQU 6 (goto :err)  
 @echo (%date% %time%) sorry, this script stopped due to a few unsuccessful retries.  
 @echo (%date% %time%) Success! The script completed successfully!