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

net.sourceforge.jweb.jstl.fn.FormatUtil Maven / Gradle / Ivy

Go to download

本项目主要弥补在使用mybatis3+springmvc+jquery easyui快速搭建web应用系统是遇到的框架不足. 主要工作包括: 1,扩展了ApplicationContextAware,通过单例注入spring容器,提供spring容器外的bean获取方法 2,扩展了apache shiro框架,统一了安全结构 3,扩展了mybatis3拦截器,在两个拦截器中自动完成分页注入,实现内存分页。 4,分页设计数据库方言 5,提供了一个easyuigrid的模型 6,提供了java泛型的jstl 7, xml工具包等一些小工具

The newest version!
package net.sourceforge.jweb.jstl.fn;

import java.text.ChoiceFormat;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.text.MessageFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import net.sourceforge.jweb.util.ExceptionBuilder;

import org.apache.log4j.Logger;

public class FormatUtil {
	private final static Logger LOGGER = Logger.getLogger(FormatUtil.class);

	private FormatUtil() {
	}

	public static final String format(Format f, Object obj) {
		if (f == null || obj == null)
			return "";
		try {
			return f.format(obj);
		} catch (Exception e) {
			LOGGER.warn("Can not format obj=" + obj + " from " + f);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static final String formatDate(Date f, String format) {
		if (f == null || format == null)
			return "";
		try {
			return new SimpleDateFormat(format).format(f);
		} catch (Exception e) {
			LOGGER.warn("Can not format obj=" + f + " by " + format);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static Object parse(Format f, String source) {
		if (f == null || source == null)
			return null;
		try {
			return f.parseObject(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String transformDate(String source, String sourceFormat, String destFormat) {
		if (sourceFormat == null || source == null || sourceFormat == null)
			return "";
		try {
			DateFormat sourceF = new SimpleDateFormat(sourceFormat);
			DateFormat destF = new SimpleDateFormat(destFormat);
			return destF.format(sourceF.parse(source));
		} catch (Exception e) {
			LOGGER.warn("Can not transform obj=" + source + " from " + sourceFormat + " to " + destFormat);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static String transformDecimal(String source, String sourceFormat, String destFormat) {
		if (sourceFormat == null || source == null || sourceFormat == null)
			return "";
		try {
			NumberFormat sourceF = new DecimalFormat(sourceFormat);
			NumberFormat destF = new DecimalFormat(destFormat);
			return destF.format(sourceF.parse(source));
		} catch (Exception e) {
			LOGGER.warn("Can not transform obj=" + source + " from " + sourceFormat + " to " + destFormat);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}

	public static String transformChoice(String source, String sourceFormat, String destFormat) {
		if (sourceFormat == null || source == null || sourceFormat == null)
			return "";
		try {
			ChoiceFormat sourceF = new ChoiceFormat(sourceFormat);
			NumberFormat destF = new ChoiceFormat(destFormat);
			return destF.format(sourceF.parse(source));
		} catch (Exception e) {
			LOGGER.warn("Can not transform obj=" + source + " from " + sourceFormat + " to " + destFormat);
			LOGGER.warn(ExceptionBuilder.buildStack(e));
			return "";
		}
	}
	// -------instance constructor and also in java.text getInstance----

	public static Format getDecimalInstance() {
		return new DecimalFormat();
	}

	public static Format getDecimalInstance(String pattern) {
		return new DecimalFormat(pattern);
	}

	public static Format getDecimalInstance(String pattern, DecimalFormatSymbols s) {
		return new DecimalFormat(pattern, s);
	}

	public static DecimalFormatSymbols newDecimalFormatSymbols() {
		return new DecimalFormatSymbols();
	}

	public static DecimalFormatSymbols newDecimalFormatSymbols(Locale l) {
		return new DecimalFormatSymbols(l);
	}

	public static Format getChoiceInstance(double[] limits, String[] formats) {
		return new ChoiceFormat(limits, formats);
	}

	public static Format getChoiceInstance(String newPattern) {
		return new ChoiceFormat(newPattern);
	}

	public static Format getSimpleDateInstance(String newPattern) {
		return new SimpleDateFormat(newPattern);
	}

	public static Format getSimpleDateInstance() {
		return new SimpleDateFormat();
	}

	public static Format getSimpleDateInstance(String pattern, DateFormatSymbols formatSymbols) {
		return new SimpleDateFormat();
	}

	public static Format getSimpleDateInstance(String pattern, Locale locale) {
		return new SimpleDateFormat(pattern, locale);
	}

	public static DateFormatSymbols newDateFormatSymbols() {
		return new DateFormatSymbols();
	}

	public static DateFormatSymbols newDateFormatSymbols(Locale l) {
		return new DateFormatSymbols(l);
	}

	public static Format getMessageInstance(String pattern, Locale locale) {
		return new MessageFormat(pattern, locale);
	}

	public static Format getMessageInstance(String pattern) {
		return new MessageFormat(pattern);
	}

	public static Date parseDate(String source, String format) {
		try {
			return new SimpleDateFormat(format).parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[]) {
		System.out.println(transformDate("23:30:00", "hh:mm:ss", "h:mm aaa"));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy