at.spardat.enterprise.fmt.ATimeStampFmt 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: ATimeStampFmt.java 2093 2007-11-28 14:23:36Z s3460 $
package at.spardat.enterprise.fmt;
import java.text.*;
import java.util.Locale;
import java.util.Date;
import at.spardat.enterprise.util.*;
/**
* Converts between internal and external format of ATimeStamps. The implementation
* of java.text.DateFormat is used internally.
*
* The internal string format 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
* internal encoding of the timestamp '2003-02-28 09:53:39.955' is
* '1046422419 955000000'.
*
* A warning: Do not let the user input timestamps. Although this formatter handles it,
* it is almost impossible for the user to provide a long, locale dependent input pattern
* he does not know in advance.
*/
public class ATimeStampFmt extends IFmt {
/**
* @see "java.text.DateFormat.SHORT"
*/
public static final int SHORT = ADateFmt.SHORT;
/**
* @see "java.text.DateFormat.MEDIUM"
*/
public static final int MEDIUM = ADateFmt.MEDIUM;
/**
* @see "java.text.DateFormat.LONG"
*/
public static final int LONG = ADateFmt.LONG;
/**
* @see "java.text.DateFormat.FULL"
*/
public static final int FULL = ADateFmt.FULL;
// the DateFormat used to format and parse timestamps
private DateFormat dateFormat_;
/**
* Returns a localized formatter instance parametrized with styles
*
* @param styleDate one of SHORT, MEDIUM, LONG, FULL or the IFmt-styles.
* @param styleTime one of SHORT, MEDIUM, LONG, FULL or the IFmt-styles.
* @param l the locale
* @return a instance of IFmt.
*/
static public ATimeStampFmt getInstance (int styleDate, int styleTime, Locale l) {
return new ATimeStampFmt (styleDate, styleTime, l);
}
/**
* Returns a non localized formatter where you have to provide the pattern yourself.
*
* @param pattern a pattern as defined in java.text.SimpleDateFormat.
* @param l Locale
* @return a instance of ATimeStampFmt.
*/
static public ATimeStampFmt getInstance (String pattern, Locale l) {
return new ATimeStampFmt (pattern, l, 0);
}
/**
* Returns a non localized formatter where you have to provide the pattern yourself.
*
* @param pattern a pattern as defined in java.text.SimpleDateFormat.
* @param style may be MANDATORY
* @param l Locale
* @return a instance of ATimeStampFmt.
*/
static public ATimeStampFmt getInstance (String pattern, int style, Locale l) {
return new ATimeStampFmt (pattern, l, style);
}
/**
* Constructor for ATimeStampFmt.
*
* @param styleDate one of SHORT, MEDIUM, LONG, FULL or the IFmt-styles
* @param styleTime one of SHORT, MEDIUM, LONG, FULL or the IFmt-styles
* @param l the locale
*/
public ATimeStampFmt (int styleDate, int styleTime, Locale l) {
if ((styleDate & MANDATORY) != 0 || (styleTime & MANDATORY) != 0) setMandatory(true);
int javaDateStyle = DateFormat.MEDIUM;
int javaTimeStyle = DateFormat.MEDIUM;
if ((styleDate & SHORT) != 0) javaDateStyle = DateFormat.SHORT;
else if ((styleDate & LONG) != 0) javaDateStyle = DateFormat.LONG;
else if ((styleDate & FULL) != 0) javaDateStyle = DateFormat.FULL;
if ((styleTime & SHORT) != 0) javaTimeStyle = DateFormat.SHORT;
else if ((styleTime & LONG) != 0) javaTimeStyle = DateFormat.LONG;
else if ((styleTime & FULL) != 0) javaTimeStyle = DateFormat.FULL;
dateFormat_ = DateFormat.getDateTimeInstance (javaDateStyle, javaTimeStyle, l);
dateFormat_.setLenient(false);
}
/**
* Constructs a ATimeStampFmt using a java.text.SimpleDateFormat pattern.
*
* @param pattern a pattern as defined in java.text.SimpleDateFormat.
* @param l Locale
* @param style may be MANDATORY
*/
public ATimeStampFmt (String pattern, Locale l, int style) {
style_ = style;
dateFormat_ = new SimpleDateFormat(pattern, l);
dateFormat_.setLenient(false);
}
/**
* @see at.spardat.enterprise.fmt.IFmt#parse(String)
*/
public String parse (String external) throws FmtParseException {
String internal = parse2 (external);
checkMandatory(internal);
return internal;
}
/**
* Does the parse-work without the mandatory-check
*/
private String parse2 (String external) throws FmtParseException {
// trim blanks on both sides
if (external == null) return "";
external = external.trim();
if (external.length() == 0) return "";
else {
ParsePosition pp = new ParsePosition(0);
Date date = null;
date = dateFormat_.parse(external, pp);
if (date == null || (pp.getIndex() != external.length() && pp.getIndex() > 0)) {
throw new FmtParseException ("ATSSyntax", getSampleTS());
}
return TimeStampUtil.date2Internal(date);
}
}
// returns a timestamp template
private String getSampleTS () {
return dateFormat_.format(new Date());
}
/**
* @see at.spardat.enterprise.fmt.IFmt#format(String)
*/
public String format (String internal) {
if (internal == null || internal.length() == 0) return "";
return dateFormat_.format(TimeStampUtil.internal2Date(internal));
}
/**
* @see at.spardat.enterprise.fmt.IFmt#maxLenOfExternal()
*/
public int maxLenOfExternal () {
return 50;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isLegalExternalChar(char)
*/
public boolean isLegalExternalChar (char aChar) {
return true;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isLegalInternal(String)
*/
public boolean isLegalInternal (String internal) {
return TimeStampUtil.internal2Date(internal) != null;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isOneWay()
*/
public boolean isOneWay() {
return false;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#mayBeAppliedTo(byte)
*/
public boolean mayBeAppliedTo (byte type) {
return type == Types.T_TIMESTAMP;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#clone()
*/
public Object clone() {
Object toReturn = super.clone();
((ATimeStampFmt)toReturn).dateFormat_ = (DateFormat)dateFormat_.clone();
return toReturn;
}
}