[Schmitzm-commits] r1853 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Feb 2 15:20:49 CET 2012


Author: mojays
Date: 2012-02-02 15:20:48 +0100 (Thu, 02 Feb 2012)
New Revision: 1853

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
LangUtil: new methods getSignumStr(.) to return +/- for a numeric value
LangUtil: new methods getTimezoneDescription(.)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-02-02 12:39:13 UTC (rev 1852)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-02-02 14:20:48 UTC (rev 1853)
@@ -39,6 +39,8 @@
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.text.DateFormat;
+import java.text.DecimalFormat;
+import java.text.NumberFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 import java.util.Arrays;
@@ -49,6 +51,7 @@
 import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.Locale;
+import java.util.TimeZone;
 import java.util.TreeSet;
 import java.util.Vector;
 import java.util.concurrent.TimeUnit;
@@ -95,6 +98,9 @@
 	 */
 	public static final DateFormat DEFAULT_DATE_TIME_FORMATTER = new SimpleDateFormat("dd.MM.yyyy [HH:mm:ss]");
 
+	/** Format for 2-digit hour information with leading zero. */
+	public static final NumberFormat HOUR_FORMAT = new DecimalFormat("00");
+	
 	/**
 	 * Liefert <code>true</code> wenn der String nur Nullen, Leerzeichen, Kommata und/oder Punkte enthält.
 	 */
@@ -134,7 +140,85 @@
 			return "";
 		return string.trim();
 	}
+	
+	   /**
+     * Returns "+" for positive values and "-" for negative values. 
+     * @param value a value
+     * @return empty string if value is {@code null}, {@code NaN} or {@code 0.0}
+     */
+    public static String getSignumStr(Number value) {
+      return getSignumStr(value, "");
+    }
+	
+	/**
+	 * Returns "+" for positive values and "-" for negative values. 
+	 * @param value a value
+	 * @param zeroSign sign to show for 0.0
+	 * @return empty string if value is {@code null} or {@code NaN} 
+	 */
+	public static String getSignumStr(Number value, String zeroSign) {
+	  if ( value == null || Double.isNaN(value.doubleValue()) )
+	    return "";
+	  double v = value.doubleValue();
+	  if ( v == 0.0 )
+	    return zeroSign;
+	  return v < 0 ? "-" : "+";
+	}
+	
+    /**
+     * Returns a timezone description including difference to GMT for the default time zone.
+     * @param style    style for time zone description ({@link TimeZone#LONG} or 
+     *                 {@link TimeZone#SHORT})
+     * @see TimeZone#getDisplayName(boolean, int, Locale)
+     */
+    public static String getTimezoneDescription(int style) {
+      return getTimezoneDescription(null, style);
+    }
 
+    /**
+     * Returns a timezone description including difference to GMT for the default time zone.
+     * @param style    style for time zone description ({@link TimeZone#LONG} or 
+     *                 {@link TimeZone#SHORT})
+     * @see TimeZone#getDisplayName(boolean, int, Locale)
+     */
+    public static String getTimezoneDescription(TimeZone timezone, int style) {
+      return getTimezoneDescription(null, null, style);
+    }
+
+    /**
+	 * Returns a timezone description, including difference to GMT.
+	 * @param timezone a time zone (if {@code null} the default timezone is used)
+	 * @param date     special date to return the timezone for (daylight saving in GMT offset)
+	 * @param style    style for time zone description ({@link TimeZone#LONG} or 
+	 *                 {@link TimeZone#SHORT})
+	 * @see TimeZone#getDisplayName(boolean, int, Locale)
+	 */
+	public static String getTimezoneDescription(TimeZone timezone, Date date, int style) {
+	  if ( timezone == null )
+	    timezone = TimeZone.getDefault();
+	  if ( date == null )
+	    date = new Date();
+
+	  // check whether daylight saving time must taken into account
+	  boolean inclDST = timezone.inDaylightTime(date);
+	  
+	  // construct description
+	  String timezoneStr = timezone.getDisplayName(inclDST,style);
+	  // for long description, show short term too
+	  if ( style == TimeZone.LONG )
+	    timezoneStr += " / " + timezone.getDisplayName(inclDST, TimeZone.SHORT);
+
+	  // calculate offset to GMT
+	  long   zoneOffsetHours = timezone.getOffset(date.getTime())/LangUtil.HOUR_MILLIS;
+      String zoneOffsetStr = "GMT"
+          + LangUtil.getSignumStr(zoneOffsetHours,"+")
+          + HOUR_FORMAT.format(Math.abs(zoneOffsetHours));
+      
+      timezoneStr += " (" + zoneOffsetStr + ")";
+
+      return  timezoneStr;
+	}
+
 	/**
 	 * Completes an incomplete date definition (in european dotted-format {@code dd.MM.yyyy}). Missing dots are also
 	 * added. In this case the method expects that day is given with 2 digits. If month and/or year is not given, these



More information about the Schmitzm-commits mailing list