`

MessageFormat

    博客分类:
  • java
 
阅读更多
package net.demo.test.date;

import java.text.MessageFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class Callout {
	private static String CALLOUT_START_TIME = "08:00";
	private static String CALLOUT_END_TIME = "20:00";

	private static Callout instance = new Callout();
	private boolean inited = false;

	private Callout() {
		if (!inited) {
			init();
			inited = true;
		}
	}

	public static Callout getInstance() {
		return instance;
	}

	public static void init() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd {0}:00");
		sdf.setLenient(false);
		Date date = new Date(System.currentTimeMillis());
		String timeTemplate = sdf.format(date);

		String defaultPeriod = "08:00-20:00";
		// String CalloutPeriod =
		// SystemConfigCache.getConfigValue("ivr_can_Callout_period",
		// defaultPeriod);
		String[] periods = defaultPeriod.split("-");

		try {
			Callout.CALLOUT_START_TIME = periods[0];
			Callout.CALLOUT_END_TIME = periods[1];
		} catch (Exception e) {
			Callout.CALLOUT_START_TIME = "08:00";
			Callout.CALLOUT_END_TIME = "20:00";
			return;
		}

		String startTime = MessageFormat.format(timeTemplate,
				Callout.CALLOUT_START_TIME);
		String endTime = MessageFormat.format(timeTemplate,
				Callout.CALLOUT_END_TIME);

		SimpleDateFormat sdd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdd.setLenient(false);
		try {
			long start = sdd.parse(startTime).getTime();
			long end = sdd.parse(endTime).getTime();
			if (start > end) {
				throw new ParseException("start time after end time", -1);
			}

		} catch (ParseException e) {
			Callout.CALLOUT_START_TIME = "08:00";
			Callout.CALLOUT_END_TIME = "20:00";
		}
	}

	public static Date getCurrentTime() {
		Date date = new Date(System.currentTimeMillis());
		return date;
	}

	public static Date getStartTime() {

		return getDate(Callout.CALLOUT_START_TIME);
	}

	public static Date getEndTime() {

		return getDate(Callout.CALLOUT_END_TIME);
	}

	public static Date getDate(String time) {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd {0}:000");
		String template = sdf.format(date);
		String result = MessageFormat.format(template, time);

		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setLenient(false);
		try {
			date = sdf.parse(result);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return date;
	}

	public static void main(String[] args) {
		boolean flag = canCallout();
		System.out.println(flag);

		//
		flag = example();
		System.out.println(flag);
	}

	public static boolean canCallout() {
		long start = getStartTime().getTime();
		long end = getEndTime().getTime();
		long current = getCurrentTime().getTime();

		if (start <= current && end > current) {
			return true;
		}
		return false;
	}

	public void test() {
		GregorianCalendar c = new GregorianCalendar(Locale.getDefault());
		c.setLenient(false);
		c.setTime(new Date());
	}

	/**
	 * Example
	 */
	public static boolean example() {
		String DEFAULT_PERIOD = "08:00-20:00";
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		String currentTime = sdf.format(date);
		// getSystemConfig();
		String[] interval = DEFAULT_PERIOD.split("-");
		if (currentTime.compareTo(interval[0]) > 0
				&& currentTime.compareTo(interval[1]) < 0) {
			return true;
		}
		return false;
	}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics