All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.template.DateAttributeRenderer Maven / Gradle / Ivy

/**
 * Creative commons Attribution-NonCommercial license.
 *
 * http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
 *
 * NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
 */
package base.template;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import base.json.Json;
import org.stringtemplate.v4.AttributeRenderer;

public class DateAttributeRenderer implements AttributeRenderer {

	private static final DateFormat AUSTRALIAN_FORMAT = new SimpleDateFormat("d/M/yyyy h:mma");
	private static final DateFormat MSB_FORMAT = new SimpleDateFormat("yyyyMMddHHmm");
	private static final DateFormat LOG_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final DateFormat RSS_FORMAT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
	private static final DateFormat ATOM_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");

	public String toString(Object o, String format, Locale locale) {
		if(format == null || format.length() == 0) {
			return AUSTRALIAN_FORMAT.format((Date)o);
		}
		if(format.equals("json")) {
			return Json.toDateTime((Date) o);
		}
		if(format.equals("yyyyMMddHHmm")) {
			return MSB_FORMAT.format((Date) o);
		}
		if(format.equals("log")) {
			return LOG_FORMAT.format((Date) o);
		}
		if(format.equals("atom")) {
			return ATOM_FORMAT.format((Date)o);
		}
		if(format.equals("rss")) {
			return RSS_FORMAT.format((Date)o);
		}
		return AUSTRALIAN_FORMAT.format((Date)o);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy