Monday, September 30, 2013

The Function That Changes Global Time to Local Time in Java

The time zone is the issue when you make global service.

There are four time zone in U.S.
PSF:  Pacific Standard Time
MST: Mountain Standard Time
CST: Central Standard Time
EST: Eastern Standard Time

The function of transformation from UTC/GMT to local time

Sometimes you need to transform from standard time to local time.
You can use the function bellow.

 // Local Time -> UTC/GMT Time  
 public static long convertLocalTimeToUTC(long pv_localDateTime)  
 {  
   long lv_UTCTime = pv_localDateTime;  
     
   TimeZone z = TimeZone.getDefault();  
   //int offset = z.getRawOffset(); // The offset not includes daylight savings time  
   int offset = z.getOffset(pv_localDateTime); // The offset includes daylight savings time  
   lv_UTCTime = pv_localDateTime - offset;  
   return lv_UTCTime;  
 }  
   
 // UTC/GMT Time -> Local Time  
 public static long convertUTCToLocalTime(long pv_UTCDateTime)  
 {  
   long lv_localDateTime = pv_UTCDateTime;  
     
   TimeZone z = TimeZone.getDefault();  
   //int offset = z.getRawOffset(); // The offset not includes daylight savings time  
   int offset = z.getOffset(pv_UTCDateTime); // The offset includes daylight savings time  
     
   lv_localDateTime = pv_UTCDateTime + offset;  
     
   return lv_localDateTime;  
 }  
   

It is very easy to understand.
You carefully check that the parameter with long type is return value from getDate() method in Date format.


Transform from UTC/GMT to local time using Date Format

Actually there is a need to change the form of the type such as "20120713064755".
This is the solution using SimpleDateFormat.

 import java.text.ParseException;  
 import java.text.SimpleDateFormat;  
 import java.util.Date;  
 import java.util.TimeZone;  
   
 public class ConverTimeZone {  
   
      public static void main(String[] args) {  
           // changes from UTC to local time  
           //String utcTime = "20120713184755";  
           String utcTime = "20120713064755";  
             
           String localTime = convertUtcToLocal(utcTime);  
             
           System.out.println("GMT/UTC: " + utcTime + ", local time: " + localTime);  
      }  
   
      /**  
       * the function that changes from UTC to local time   
       * @author xmlmanager  
       * @param utcTime GMT/UTC (format: 20120713064755)  
       * @return String local time   
       */  
      private static String convertUtcToLocal(String utcTime) {  
           String localTime = "";  
             
           // declare date format   
           SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");  
             
           try {  
                // changes from UTC to Date format  
                Date dateUtcTime = dateFormat.parse(utcTime);  
                  
                // changes from UTC Date format to the time of long type  
                long longUtcTime = dateUtcTime.getTime();  
                  
                // calculate the difference through TimeZone (if it is summer time, use getOffset than getRawOffset)  
                TimeZone zone = TimeZone.getDefault();  
                int offset = zone.getOffset(longUtcTime);  
                long longLocalTime = longUtcTime + offset;  
                  
                // changes from local time of long type to Date format  
                Date dateLocalTime = new Date();  
                dateLocalTime.setTime(longLocalTime);  
                  
                // return the string of local time  
                localTime = dateFormat.format(dateLocalTime);  
                  
           } catch (ParseException e) {  
                e.printStackTrace();  
           }             
             
           return localTime;  
      }  
 }  
   

That's all!!

Sunday, September 29, 2013

The Tip of Programming Comment

There are two type of comments in java and C.
"//" is the inline comment and "/* .. */" is the block comment.

Sometimes mixing the two type of comments is very useful.

If you use like this, the first line is comment and second line runs.
/*
This is comment.
/*/
This runs.
//*/

When you add "/" in front of the "/*" at the beginning, it changes.

//*
This runs.
/*/
This is comment.
//*/

I used usually it when I need the test account.