at.spardat.enterprise.util.TimeStampUtil Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
// @(#) Id
package at.spardat.enterprise.util;
/**
* Utility methods to convert between an internal encoding of a TimeStamp (to be defined below) and
* java.util.Dates. Note that the maximum precision of java.util.Dates is milliseconds.
* Although an internal encoding might hold nanoseconds, some information might be lost
* when converting an internal encoding to a Date.
*
* The string encoding is a decimally encoded number, followed by a blank, followed
* by a decimally encoded number. The first number is the number of seconds
* since 19700101 (like a java.util.Date, without millisecond part).
* The second number denotes the number of nanoseconds. For example, the
* encoding of the timestamp '2003-02-28 09:53:39.955' is
* '1046422419 955000000'.
*/
public class TimeStampUtil {
/**
* Converts a Date into the internal encoding as defined above.
*
* @return non null string following the encoding defined above.
*/
public static String date2Internal (java.util.Date d) {
if (d == null) return "";
long seconds = d.getTime() / 1000;
int nanos = (int)((d.getTime()%1000) * 1000000);
if (nanos < 0) {
nanos = 1000000000 + nanos;
seconds--;
}
return String.valueOf(seconds) // integral seconds
+ " "
+ String.valueOf(nanos); // nanoseconds
}
/**
* Converts a Timestamp into the internal encoding as defined above.
* @param t
* @return non null string following the encoding defined above.
* @since version_number
* @author s3460
*/
public static String timestamp2Internal (java.sql.Timestamp t) {
if (t == null) return "";
long seconds = t.getTime() / 1000; //miliseconds lost by division
int nanos = t.getNanos(); //Timestamp stores nanos in own component
return String.valueOf(seconds) // integral seconds
+ " "
+ String.valueOf(nanos); // nanoseconds
}
/**
* Converts an internal string to a newly created Date.
*
* @param internal the encoding as defined above.
* @return a date object whose value is equivalent to the provided encoding,
* except that fractional parts of milliseconds are discarded.
* null is returned if the provided argument is not a valid
* encoding.
*/
public static java.util.Date internal2Date (String internal) {
if (internal == null) return null;
int indexOfSpace = internal.indexOf(' ');
if (indexOfSpace == -1) return null;
long seconds = Long.parseLong(internal.substring(0, indexOfSpace));
long nanos = Long.parseLong(internal.substring(indexOfSpace+1));
return new java.util.Date(seconds * 1000 + nanos/1000000);
}
}