blob: edc9aa8beb272db80da5766fd88fff93e4c740fb [file] [log] [blame]
/**********************************************************************************************
* Copyright (c) 2008 empolis GmbH and brox IT Solutions 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:
* Juergen Schumacher (empolis GmbH) - initial API and implementation
* Andreas Weber (Attensity Europe GmbH) - renamed LiteralFormatHelper -> ValueFormatHelper
**********************************************************************************************/
package org.eclipse.smila.datamodel;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* helper class for formatting and parsing Values. all methods synchronize on the used local formatter object, so you
* can use the shared instance. Using multiple instances may improve performance, though, because of less
* synchronization.
*
* @author jschumacher
*
*/
public class ValueFormatHelper {
/**
* shared global helper instance.
*/
public static final ValueFormatHelper INSTANCE = new ValueFormatHelper();
/** The max. length of strings to be parsed as date. */
private static final int DATE_LENGTH = 10;
/** The max. length of strings to be parsed as date time. */
private static final int DATE_TIME_LENGTH = 24;
/**
* formatter to create and parse standard string representations of Date values: "yyyy-MM-dd".
*/
private final DateFormat _formatDate = new SimpleDateFormat("yyyy-MM-dd");
/**
* formatter to create and parse standard string representations of DateTime values: "yyyy-MM-dd'T'HH:mm:ssZ".
*/
private final DateFormat _formatDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
/**
* create local instance.
*/
public ValueFormatHelper() {
// nothing to do
}
/**
* format value as Date string.
*
* @param value
* a date value
* @return formatted date.
*/
public String formatDate(final Date value) {
synchronized (_formatDate) {
return _formatDate.format(value);
}
}
/**
* format value as DateTime string.
*
* @param value
* a datetime value
* @return formatted datetime string
*/
public String formatDateTime(final Date value) {
synchronized (_formatDateTime) {
return _formatDateTime.format(value);
}
}
/**
* parse a date string.
*
* @param dateString
* a date string
* @return parsed Date
* @throws ParseException
* string has wrong format
*/
public Date parseDate(final String dateString) throws ParseException {
if (dateString.length() == DATE_LENGTH) {
synchronized (_formatDate) {
return _formatDate.parse(dateString);
}
} else {
throw new ParseException("Length of date string '" + dateString + "' exceeds maximum date length of "
+ DATE_LENGTH, DATE_LENGTH);
}
}
/**
* parse datetime string.
*
* @param dateTimeString
* a datetime string
* @return parsed Date
* @throws ParseException
* string has wrong format
*/
public Date parseDateTime(final String dateTimeString) throws ParseException {
if (dateTimeString.length() == DATE_TIME_LENGTH) {
synchronized (_formatDateTime) {
return _formatDateTime.parse(dateTimeString);
}
} else {
throw new ParseException("Length of datetime string '" + dateTimeString + "' exceeds maximum date length of "
+ DATE_TIME_LENGTH, DATE_TIME_LENGTH);
}
}
/**
* try to parse the string as a date or datetime value. if it doesn't match, create a string value.
*
* @param possibleDateTime
* a string to check for date or datetime format.
* @param f
* data factory to use to create the value
* @return if the string is in datetime format: a datetime value. if the string is in date format: a date value. Else:
* a string value.
*/
public Value tryDateTimeFormats(final String possibleDateTime, final DataFactory f) {
try {
switch (possibleDateTime.length()) {
case DATE_LENGTH:
Date date;
synchronized (_formatDate) {
date = _formatDate.parse(possibleDateTime);
}
return f.createDateValue(date);
case DATE_TIME_LENGTH:
Date dateTime;
synchronized (_formatDate) {
dateTime = _formatDateTime.parse(possibleDateTime);
}
return f.createDateTimeValue(dateTime);
default:
break;
}
} catch (final ParseException ex) {
; // Ok, then fallback to string
}
return f.createStringValue(possibleDateTime);
}
}