text:  10, 200348 *49 * To change the template for this generated file go to50 * Window - Preferences - Java - Code Generation - Code and Comments51 */52 package org.mr.core.util;53 54 import java.util.GregorianCalendar ;55 import java.util.Timer ;56 import java.util.TimerTask ;57 58 /**59 * This class return the current time in millisecond and the current time with a GMT60 * offset. Using System.currentTimeMillis() slows down a process, here we do it every 61 * second and other thread get the time from here.62 * @author Amir Shevat63 */64 public class SystemTime {65 private static long time = System.currentTimeMillis();66 67 /**68 * Use this method to get the time instead System.currentTimeMillis()69 * @return same as System.currentTimeMillis() but in a 1000 milli resolution70 */71 public static long currentTimeMillis(){72 return time;73 }74 /**75 * Use this method to get the current time in milliseconds + the offset from 76 * GMT.77 * @return same as System.currentTimeMillis() but in a 1000 milli resolution78 * + offset79 */80 public static long gmtCurrentTimeMillis(){81 return time;82 }83 84 /**85 * Initiate the SystemTime. Must be done before using the SystemTime methods.86 */87 public static void init(){88 Timer time = new Timer (true);89 90 GregorianCalendar cal = new GregorianCalendar ();91 // overwrite the run method to call the sync92 TimerTask timerTask = new TimerTask (){93 public void run(){94 SystemTime.sync();95 }96 };97 98 time.schedule(timerTask,1000,1000);99 }//init100 101 /**102 * Updates the SystemTime time with the System.currentTimeMillis()103 */104 public static void sync(){105 time = System.currentTimeMillis(); 106 }107 108 109 }//SystemTime110
  Java API By Example, From Geeks To Geeks. | Our Blog | Conditions of Use | About Us_ |