欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

DateUtils

程序员文章站 2022-05-09 19:56:33
...
package com.fengzhi.web.util;

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

public class DateUtil {

	/**
	 * 
	 * @Title: getMonthFirstDay
	 * @Description: (获取本月第一天的日期:2015-01-01)
	 * @author fyf
	 * @return
	 */
	public static String getMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

		return dateFormat("yyyy-MM-dd", calendar.getTime());
	}

	/**
	 * 
	 * @Title: getMonthFirstDay
	 * @Description: (获取本月第一天的日期:2015-02-01)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getMonthFirstDay(String date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(dateFormat("yyyy-MM-dd", date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

		return dateFormat("yyyy-MM-dd", calendar.getTime());
	}

	/**
	 * 
	 * @Title: getMonthFirstDay
	 * @Description: (获取本月第一天的日期:2015-03-01)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getMonthFirstDay(Date date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));

		return dateFormat("yyyy-MM-dd", calendar.getTime());
	}

	/**
	 * 
	 * @Title: getMonthLastDay
	 * @Description: (获取本月的最后一天:2015-01-31)
	 * @author fyf
	 * @return
	 */
	public static String getMonthLastDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		return dateFormat("yyyy-MM-dd", calendar.getTime());
	}

	/**
	 * 
	 * @Title: getMonthLastDay
	 * @Description: (获取本月的最后一天:2015-02-28)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getMonthLastDay(String date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(dateFormat("yyyy-MM-dd", date));
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		return dateFormat("yyyy-MM-dd", calendar.getTime());
	}

	/**
	 * 
	 * @Title: getMonthLastDay
	 * @Description: (获取本月的最后一天:2015-09-30)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getMonthLastDay(Date date) {
		Calendar calendar = Calendar.getInstance();

		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));

		return dateFormat("yyyy-MM-dd", calendar.getTime());
	}

	/**
	 * 
	 * @Title: dateFormat
	 * @Description: (格式化日期输出格式)
	 * @author fyf
	 * @param format
	 *            (yyyy-MM-dd)/(yyyy-MM-dd HH:mm:ss)
	 * @param date
	 * 
	 * @return (2015-01-01)/(2015-02-02 10:20:30)
	 */
	public static String dateFormat(String format, Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		return sdf.format(date);
	}

	/**
	 * 
	 * @Title: dateFormat
	 * @Description: (格式化日期输出格式)
	 * @author fyf
	 * @param format
	 * @param date
	 * @return
	 */
	public static Date dateFormat(String format, String date) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		try {
			return sdf.parse(date);
		} catch (ParseException e) {
			return new Date();
		}
	}

	/**
	 * 
	 * @Title: getYear
	 * @Description: (获取当前日期:年)
	 * @author fyf
	 * @return
	 */
	public static int getYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 
	 * @Title: getMonthOfYear
	 * @Description: (获取当前日期:月)
	 * @author fyf
	 * @return
	 */
	public static int getMonthOfYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MONTH);
	}

	/**
	 * 
	 * @Title: getDayOfMonth
	 * @Description: (获取当前日期:日)
	 * @author fyf
	 * @return
	 */
	public static int getDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 
	 * @Title: getHourOfDay
	 * @Description: (获取当前时间:时)
	 * @author fyf
	 * @return
	 */
	public static int getHourOfDay() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/**
	 * 
	 * @Title: getMinute
	 * @Description: (获取当前时间:分)
	 * @author fyf
	 * @return
	 */
	public static int getMinute() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MINUTE);
	}

	/**
	 * 
	 * @Title: getSecond
	 * @Description: (获取当前时间:秒)
	 * @author fyf
	 * @return
	 */
	public static int getSecond() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.SECOND);
	}

	/**
	 * 
	 * @Title: getMilliSecond
	 * @Description: (获取当前时间:毫秒)
	 * @author fyf
	 * @return
	 */
	public static int getMilliSecond() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MILLISECOND);
	}

	/**
	 * 
	 * @Title: getDayOfYear
	 * @Description: (获取当前日期属于当年的第几天)
	 * @author fyf
	 * @return
	 */
	public static int getDayOfYear() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 
	 * @Title: getDayOfYearFromString
	 * @Description: (获取该日期是当年的第几天)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static int getDayOfYearFromString(String date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getYYYY_MM_ddFromString(date));

		return calendar.get(Calendar.DAY_OF_YEAR);
	}

	/**
	 * 
	 * @Title: getDayOfWeekFromString
	 * @Description: (获取该日期是星期几)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getDayOfWeekFromString(String date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getYYYY_MM_ddFromString(date));

		switch (calendar.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.SUNDAY:
			return "星期日";
		case Calendar.MONDAY:
			return "星期一";
		case Calendar.TUESDAY:
			return "星期二";
		case Calendar.WEDNESDAY:
			return "星期三";
		case Calendar.THURSDAY:
			return "星期四";
		case Calendar.FRIDAY:
			return "星期五";
		case Calendar.SATURDAY:
			return "星期六";
		}

		return null;
	}

	/**
	 * 
	 * @Title: getDayOfWeek
	 * @Description: (获取当前日期是本周星期几)
	 * @author fyf
	 * @return
	 */
	public static String getDayOfWeek() {
		Calendar calendar = Calendar.getInstance();

		switch (calendar.get(Calendar.DAY_OF_WEEK)) {
		case Calendar.SUNDAY:
			return "星期日";
		case Calendar.MONDAY:
			return "星期一";
		case Calendar.TUESDAY:
			return "星期二";
		case Calendar.WEDNESDAY:
			return "星期三";
		case Calendar.THURSDAY:
			return "星期四";
		case Calendar.FRIDAY:
			return "星期五";
		case Calendar.SATURDAY:
			return "星期六";
		}

		return null;
	}

	/**
	 * 
	 * @Title: getWeekOfMonth
	 * @Description: (获取当前日期是本月的第几周)
	 * @author fyf
	 * @return
	 */
	public static String getWeekOfMonth() {
		Calendar calendar = Calendar.getInstance();

		switch (calendar.get(Calendar.WEEK_OF_MONTH)) {
		case 1:
			return "第一周";
		case 2:
			return "第二周";
		case 3:
			return "第三周";
		case 4:
			return "第四周";
		case 5:
			return "第五周";
		}

		return null;
	}

	/**
	 * 
	 * @Title: getWeekOfMonthFromeDate
	 * @Description: (获取该日期是当年当月的第几周)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getWeekOfMonthFromeDate(String date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getYYYY_MM_ddFromString(date));

		switch (calendar.get(Calendar.WEEK_OF_MONTH)) {
		case 1:
			return "第一周";
		case 2:
			return "第二周";
		case 3:
			return "第三周";
		case 4:
			return "第四周";
		case 5:
			return "第五周";
		}

		return null;
	}

	/**
	 * 
	 * @Title: getWeekOfYear
	 * @Description: (获取当前日期是当年的第几周)
	 * @author fyf
	 * @return
	 */
	public static String getWeekOfYear() {
		Calendar calendar = Calendar.getInstance();

		return "第" + (calendar.get(Calendar.WEEK_OF_YEAR)) + "周";
	}

	/**
	 * 
	 * @Title: getWeekOfYearFromString
	 * @Description: (获取该日期是当年的第几周)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getWeekOfYearFromString(String date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(getYYYY_MM_ddFromString(date));

		return "第" + (calendar.get(Calendar.WEEK_OF_YEAR)) + "周";
	}

	/**
	 * 
	 * @Title: getYYYY_MM_dd
	 * @Description: (获取日期:2015-01-01)
	 * @author fyf
	 * @return
	 */
	public static String getYYYY_MM_dd() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(new Date());
	}

	/**
	 * 
	 * @Title: getYYYY_MM_ddFromDate
	 * @Description: (转换日期格式 to String :2015-06-16)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getYYYY_MM_ddFromDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (date == null)
			return getYYYY_MM_dd();

		return sdf.format(date);
	}

	/**
	 * 
	 * @Title: getYYYY_MM_ddFromString
	 * @Description: (转换日期格式 to Date :2015-06-16)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static Date getYYYY_MM_ddFromString(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		if (date == null || "".equals(date))
			date = getYYYY_MM_dd();

		try {
			return sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @Title: getY_m_dHms
	 * @Description: (获取当前日期时间:2015-06-16 14:07:06)
	 * @author fyf
	 * @return
	 */
	public static String getY_m_dHms() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date());
	}

	/**
	 * 
	 * @Title: getY_m_dHmsFromDate
	 * @Description: (转换日期时间格式 to String :2015-06-16 14:07:06)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getY_m_dHmsFromDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		if (date == null)
			return getY_m_dHms();

		return sdf.format(date);
	}

	/**
	 * 
	 * @Title: getY_m_dHmsFromString
	 * @Description: (转换日期时间格式 to Date :2015-06-16 14:07:06)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static Date getY_m_dHmsFromString(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		if (date == null || "".equals(date))
			date = getY_m_dHms();

		try {
			return sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @Title: getYmdHmsSSS
	 * @Description: (获取当前日期时间:20150616141020101)
	 * @author fyf
	 * @return
	 */
	public static String getYmdHmsSSS() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sdf.format(new Date());
	}

	/**
	 * 
	 * @Title: getYmdHmsSSSFromDate
	 * @Description: (转换日期时间格式 to String :20150616141020101)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static String getYmdHmsSSSFromDate(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

		if (date == null)
			return getYmdHmsSSS();

		return sdf.format(date);
	}

	/**
	 * 
	 * @Title: getYmdHmsSSSFromString
	 * @Description: (转换日期时间格式 to Date:20150616141020101)
	 * @author fyf
	 * @param date
	 * @return
	 */
	public static Date getYmdHmsSSSFromString(String date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");

		if (date == null || "".equals(date))
			date = getYmdHmsSSS();

		try {
			return sdf.parse(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 
	 * @Title: getMondayOfWeek
	 * @Description: (根据日期获取当周的星期一的日期)
	 * @return
	 */
	public static String getMondayDateFromString(String date) {
		// 0:表示本周; 1:表示下周;-1:表示上周
		int n = 0;
		Calendar cal = Calendar.getInstance();

		cal.setTime(getYYYY_MM_ddFromString(date));
		// 美国星期日作为每周的第一天(即:国内的本周星期天为美国的下周)
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.add(Calendar.DATE, n * 7);
		// 想转星期几, Calendar就引用星期几(星期三:Calendar.WEDNESDAY)
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

		return getYYYY_MM_ddFromDate(cal.getTime());
	}

	/**
	 * 
	 * @Title: getMondayOfWeek
	 * @Description: (根据日期获取当周的星期一的日期)
	 * @return
	 */
	public static String getMondayDateFromDate(Date date) {
		// 0:表示本周; 1:表示下周;-1:表示上周
		int n = 0;
		Calendar cal = Calendar.getInstance();

		if (date == null)
			date = new Date();

		cal.setTime(date);
		// 美国星期日作为每周的第一天(即:国内的本周星期天为美国的下周)
		cal.add(Calendar.DAY_OF_MONTH, -1);
		cal.add(Calendar.DATE, n * 7);
		// 想转星期几, Calendar就引用星期几(星期三:Calendar.WEDNESDAY)
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

		return getYYYY_MM_ddFromDate(cal.getTime());
	}

	public static String getSundayOfWeek(String date) {
		// 0:表示本周; 1:表示下周;-1:表示上周
		int n = 0;
		Calendar cal = Calendar.getInstance();
		cal.setTime(getYYYY_MM_ddFromString(date));
		// 美国星期天为一周第一天(即:国内的本周星期天为美国的下周)
		cal.add(Calendar.DAY_OF_MONTH, 6);
		cal.add(Calendar.DATE, n * 7);
		// 想转星期几, Calendar就引用星期几(星期三:Calendar.WEDNESDAY)
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

		try {
			date = getYYYY_MM_ddFromDate(cal.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}

		return date;
	}

}

 

  • DateUtils
            
    
    博客分类: Java Java日期时间 
  • 大小: 170.2 KB