DateFormat.java 3.3 KB
package com.framework.util;

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

/**
 * 时间 日期 与字符串 转换处理工具类
 * 
 * @author s
 *
 */
public class DateFormat {

	/**
	 * 获取当前年月日字符串 20130708
	 * 
	 * @return
	 */
	public static String getYmd() {
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		return "" + year + (month < 10 ? "0" + month : month) + (day < 10 ? "0" + day : day);
	}

	/**
	 * 把字符串转换成Date
	 * 
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date formatDate(String dateSource, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = simpleDateFormat.parse(dateSource);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 把字Date换成Date
	 * 
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static Date formatDate(Date dateSource, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = simpleDateFormat.parse(formatString(dateSource, format));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 把日期转换成特定字符串
	 * 
	 * @param dateSource
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static String formatString(Date dateSource, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		String str = null;
		str = simpleDateFormat.format(dateSource);
		return str;
	}

	/**
	 * 把日期转换成特定字符串
	 * 
	 * @param dateSource
	 * @param format
	 * @return
	 * @throws ParseException
	 */
	public static String formatString(String dateSource, String format) {
		Date d = formatDate(dateSource, format);
		String str = null;
		str = formatString(d, format);
		return str;
	}

	/**
	 * 查询上一个月时间
	 * 
	 * @return
	 */
	public static Date getBeforeMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -1);
		return formatDate(calendar.getTime(), "yyyy-MM");
	}

	/**
	 * 查询上下个月时间
	 * 
	 * @return
	 */
	public static Date getAfterMonth() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, 1);
		return formatDate(calendar.getTime(), "yyyy-MM");
	}

	/**
	 * 查询上下个月时间
	 * 
	 * @return
	 */
	public static Date getAfterMonth(Date targetDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(targetDate);
		calendar.add(Calendar.MONTH, 1);
		return formatDate(calendar.getTime(), "yyyy-MM");
	}

	/**
	 * 查询月
	 * 
	 * @return
	 */
	public static Date getMonth(Date targetDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(targetDate);
		return formatDate(calendar.getTime(), "yyyy-MM");
	}

	public static void main(String[] args) {
		System.out.println(getAfterMonth(new Date()));
	}

}