[Schmitzm-commits] r1852 - in trunk/schmitzm-core/src/main/java/de/schmitzm: lang net/mail

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Feb 2 13:39:13 CET 2012


Author: mojays
Date: 2012-02-02 13:39:13 +0100 (Thu, 02 Feb 2012)
New Revision: 1852

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
Log:
LangUtil: new method getCurrentTimeFormatted(DateFormat)
LangUtil: new method getGmtDate(.) to shift a date to GMT

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-01 09:56:05 UTC (rev 1851)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-02-02 12:39:13 UTC (rev 1852)
@@ -197,7 +197,18 @@
 		return dateStr;
 	}
 
-	/**
+    /**
+     * Returns the current time as formatted string.
+     * 
+     * @param formatter
+     *            date format (if {@code null}, the default format is used)
+     * @see #DEFAULT_DATE_TIME_FORMATTER
+     */
+    public static String getCurrentTimeFormatted(DateFormat formatter) {
+        return formatter.format(new Date(System.currentTimeMillis()));
+    }
+
+    /**
 	 * Returns the current time as formatted string.
 	 * 
 	 * @param format
@@ -208,7 +219,7 @@
 		DateFormat formatter = DEFAULT_DATE_TIME_FORMATTER;
 		if (formatPattern != null)
 			formatter = new SimpleDateFormat(formatPattern);
-		return formatter.format(new Date(System.currentTimeMillis()));
+		return getCurrentTimeFormatted(formatter);
 	}
 
 	/**
@@ -239,6 +250,29 @@
 
 		return combinedDateCal.getTime();
 	}
+	
+	/**
+	 * Returns a {@link Date} which represents the given day and time in GMT time zone.<br>
+	 * <b>For example:</b><br>
+	 * The given date is 01.01.2011, 00:00 (in CET). Then this method returns 01.01.2011, 01:00, because
+	 * the timezone offset (+1 hour + possible daylight saving time offset) is added. In other
+	 * words: This method returns the date (in current timezone), when GMT represents the moment of
+	 * the given date.<br>
+	 * <b>Field of application:</b><br>
+	 * Excel does not handle timezones, so it will always show an exported date in GMT (java internal
+	 * timestamp). For birthdates or many other "fixed" moments this is not the expected way!
+	 * This method converts e.g. the birthdate (in the current timezone) so that it represents the
+	 * exact birthdate in GMT.
+	 * @param date a date in any timezone 
+	 * @return the date (in current timezone), when GMT represents this day and time
+	 */
+	public static Date getGmtDate(Date date) {
+      GregorianCalendar c = new GregorianCalendar();
+      c.setTime(date);
+      c.add(Calendar.MILLISECOND, c.get(Calendar.ZONE_OFFSET));
+      c.add(Calendar.MILLISECOND, c.get(Calendar.DST_OFFSET));
+      return c.getTime();
+	}
 
 	/**
 	 * Increases a date by milliseconds.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2012-02-01 09:56:05 UTC (rev 1851)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java	2012-02-02 12:39:13 UTC (rev 1852)
@@ -237,7 +237,7 @@
 
 		StringBuffer mailBody = new StringBuffer();
 		mailBody.append("\n").append("Date/Time:    ")
-				.append(LangUtil.getCurrentTimeFormatted(null)).append("\n")
+				.append(LangUtil.getCurrentTimeFormatted((String)null)).append("\n")
 				.append("Exception:    ").append(exceptionMess).append("\n")
 				.append("Java version: ")
 				.append(System.getProperty("java.version")).append("\n");



More information about the Schmitzm-commits mailing list