DateUtil

package com.utstar.iptvboss.util;

import org.apache.log4j.Logger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {

	private static Logger logger = Logger.getLogger(DateUtil.class.getSimpleName());
	public static final String MONTHFORMAT = "yyyy-MM";
	public static final String DAYFORMAT = "yyyy-MM-dd";
	public static final String SECONDFORMAT = "yyyy-MM-dd HH:mm:ss";

	/**
	 * 格式化日期 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param date
	 * @return
	 */
	public static String Date2Str(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sdf.format(date);
		return str;
	}

	/**
	 * 获取当前日期
	 * 
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurrentTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sdf.format(date);
		return str;
	}

	/**
	 * 编码格式转成GBK
	 * 
	 * @param oldCharacter
	 * @return
	 */
	public static String toGBK(String oldCharacter) {
		try {
			oldCharacter = new String(oldCharacter.getBytes("iso8859-1"), "gbk");
		} catch (Exception e) {
			logger.error(e);
		}
		return oldCharacter;
	}

	public static String toISO8859(String oldCharacter) {
		try {
			oldCharacter = new String(oldCharacter.getBytes("GBK"), "iso8859-1");
		} catch (Exception e) {
			logger.error(e);
		}
		return oldCharacter;
	}

	public static String toUTF8(String oldCharacter) {
		try {
			oldCharacter = new String(oldCharacter.getBytes("iso8859-1"), "UTF-8");
		} catch (Exception e) {
			logger.error(e);
		}
		return oldCharacter;
	}

	public static Date parse(String monthday, String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			 date = sdf.parse(monthday);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String formDate(Date monthday, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(monthday);
	}
	
}

你可能感兴趣的:(utils,java)