`
jilong-liang
  • 浏览: 469795 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类

Java的DateHelper时间处理类

    博客分类:
  • Java
阅读更多
package com.org.java7.core.test;

import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * @Author:jilongliang
 * @Date :2012-8-19
 * @Project:JAVA7
 * @Description:时间处理类
 */
@SuppressWarnings("all")
public class DateHelper {
	/**
	 * 字符串转换为java.util.Date<br>
	 * 支持格式为 yyyy.MM.dd G 'at' hh:mm:ss z Example:'2002-1-1 AD at 22:10:59 PSD'<br>
	 * yy/MM/dd HH:mm:ss 如 '2002/1/1 17:55:00'<br>
	 * yy/MM/dd HH:mm:ss pm 如 '2002/1/1 17:55:00 pm'<br>
	 * yy-MM-dd HH:mm:ss 如 '2002-1-1 17:55:00' <br>
	 * yy-MM-dd HH:mm:ss am 如 '2002-1-1 17:55:00 am' <br>
	 * 
	 * @param time
	 *            String 字符串<br>
	 * @return Date 日期<br>
	 */
	public static Date getStringToDate(String time) {
		SimpleDateFormat formatter;
		int tempPos = time.indexOf("AD");
		time = time.trim();
		formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
		if (tempPos > -1) {
			time = time.substring(0, tempPos) + "公元"
					+ time.substring(tempPos + "AD".length());// china
			formatter = new SimpleDateFormat("yyyy.MM.dd G 'at' hh:mm:ss z");
		}
		tempPos = time.indexOf("-");
		if (tempPos > -1 && (time.indexOf(" ") < 0)) {
			formatter = new SimpleDateFormat("yyyyMMddHHmmssZ");
		} else if ((time.indexOf("/") > -1) && (time.indexOf(" ") > -1)) {
			formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		} else if ((time.indexOf("-") > -1) && (time.indexOf(" ") > -1)) {
			formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else if ((time.indexOf("/") > -1) && (time.indexOf("am") > -1)
				|| (time.indexOf("pm") > -1)) {
			formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
		} else if ((time.indexOf("-") > -1) && (time.indexOf("am") > -1)
				|| (time.indexOf("pm") > -1)) {
			formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
		}
		ParsePosition pos = new ParsePosition(0);
		Date date = formatter.parse(time, pos);
		return date;
	}
	/**
	 * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss'(24小时制)<br>
	 * 如Sat May 11 17:24:21 CST 2002 to '2002-05-11 17:24:21'<br>
	 * 
	 * @param time
	 *            Date 日期<br>
	 * @return String 字符串<br>
	 */
	public static String getDateToString(Date time) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String Time = formatter.format(time);
		return Time;
	}
	/**
	 * 将java.util.Date 格式转换为字符串格式'yyyy-MM-dd HH:mm:ss a'(12小时制)<br>
	 * 如Sat May 11 17:23:22 CST 2002 to '2002-05-11 05:23:22 下午'<br>
	 * 
	 * @param time
	 *            Date 日期<br>
	 * @param x
	 *            int 任意整数如:1<br>
	 * @return String 字符串<br>
	 */
	public static String getDateToString(Date time, int x) {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss a");
		String date = formatter.format(time);
		return date;
	}
	/**
	 * 取系统当前时间:返回只值为如下形式 2002-10-30 20:24:39
	 * 
	 * @return String
	 */
	public static String getNow() {
		return getDateToString(new Date());
	}
	/**
	 * 取系统当前时间:返回只值为如下形式 2002-10-30 08:28:56 下午
	 * 
	 * @param hour
	 *            为任意整数
	 * @return String
	 */
	public static String getNow(int hour) {
		return getDateToString(new Date(), hour);
	}
	/**
	 * 获取小时
	 * 
	 * @return
	 */
	public static String getHour() {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("H");
		String hour = formatter.format(new Date());
		return hour;
	}
	/**
	 * 获取当前日日期返回 <return>Day</return>
	 */
	public static String getDay() {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("d");
		String day = formatter.format(new Date());
		return day;
	}
	/**
	 * 获取周
	 * @return
	 */
	public static String getWeek() {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("E");
		String week = formatter.format(new Date());
		return week;
	}
	/**
	 * 获取上周的第一天
	 * 
	 * @return
	 */
	public static String getBeforeWeekFirstDay() {
		SimpleDateFormat sdf = new SimpleDateFormat();
		Date d = new Date();
		return null;

	}
	/**
	 * 获取月份
	 * 
	 * @return
	 */
	public static String getMonth() {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("M");
		String month = formatter.format(new Date());
		return month;
	}
	/**
	 * 获取年
	 * 
	 * @return
	 */
	public static String getYear() {
		SimpleDateFormat formatter;
		formatter = new SimpleDateFormat("yyyy");
		String year = formatter.format(new Date());
		return year;
	}
	/**
	 * 对日期格式的转换成("yyyy-MM-dd)格式的方法
	 * 
	 * @param str
	 * @return
	 */
	public static java.sql.Date Convert(String str) {
		SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
		try {
			java.util.Date d = sdf.parse(str);
			java.sql.Date d1 = new java.sql.Date(d.getTime());
			return d1;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	/**
	 * 获取当前年、月、日:
	 * 
	 * @return
	 */
	public static int getYYMMDD() {
		Date date = new Date();
		int year = date.getYear() + 1900;// thisYear = 2003
		int month = date.getMonth() + 1;// thisMonth = 5
		int day = date.getDate();// thisDate = 30
		return year + month + day;
	}

	/**
	 * 取系统当前时间:返回值为如下形式 2002-10-30
	 * 
	 * @return String
	 */
	public static String getYYYY_MM_DD() {
		return getDateToString(new Date()).substring(0, 10);
	}

	/**
	 * 取系统给定时间:返回值为如下形式 2002-10-30
	 * 
	 * @return String
	 */
	public static String getYYYY_MM_DD(String date) {
		return date.substring(0, 10);
	}

	/**
	 * 在jsp页面中的日期格式和sqlserver中的日期格式不一样,怎样统一? 
	 * 在页面上显示输出时,用下面的函数处理一下
	 * 
	 * @param date
	 * @return
	 */
	public static String getFromateDate(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
		String strDate = formatter.format(date);
		return strDate;
	}
	
	/**
	 * 获取当前时间是本年的第几周
	 * @return
	 */
	public static String getWeeK_OF_Year(){
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int week=cal.get(Calendar.WEEK_OF_YEAR );
		return  "当日是本年的第"+week+"周";
	}
	/**
	 * 获取当日是本年的的第几天
	 * @return
	 */
	public static String getDAY_OF_YEAR(){
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int day=cal.get(Calendar.DAY_OF_YEAR );
		return  "当日是本年的第"+day+"天";
	}
	/**
	 * 获取本周是在本个月的第几周
	 * @return
	 */
	public static String getDAY_OF_WEEK_IN_MONTH(){
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		/*
		 * 这里这个值可以完全看JDK里面调用一下
		 * 或者点一下调用运行看看结果,看看里面的
		 * English说明就知道它是干嘛的
		 */
		int week=cal.get(Calendar.DAY_OF_WEEK_IN_MONTH);
		return  "本月第"+week+"周";
	}
	
	/**
	 *阳历转阴历农历:http://zuoming.iteye.com/blog/1554001
	 * GregorianCalendar使用: http://zmx.iteye.com/blog/409465
	 * GregorianCalendar 类提供处理日期的方法。
	 * 一个有用的方法是add().使用add()方法,你能够增加象年
	 * 月数,天数到日期对象中。要使用add()方法,你必须提供要增加的字段
	 * 要增加的数量。一些有用的字段是DATE, MONTH, YEAR, 和 WEEK_OF_YEAR
	 * 下面的程序使用add()方法计算未来80天的一个日期.
	 * 在Jules的<环球80天>是一个重要的数字,使用这个程序可以计算
	 * Phileas Fogg从出发的那一天1872年10月2日后80天的日期:
	 */
	public static void getGregorianCalendarDate(){
		GregorianCalendar worldTour = new GregorianCalendar(1872, Calendar.OCTOBER, 2);
		worldTour.add(GregorianCalendar.DATE, 80);
		Date d = worldTour.getTime();
		DateFormat df = DateFormat.getDateInstance();
		String s = df.format(d);
		System.out.println("80 day trip will end " + s);
	}
	
	/**
	 * 用来处理时间转换格式的方法
	 * @param formate
	 * @param time
	 * @return
	 */
	private static String getConvertDateFormat(String formate, Date time) {
		SimpleDateFormat dateFormat=new SimpleDateFormat(formate);
		String date=dateFormat.format(time);
		return date;
	}
	
	/**  
	 * 得到本月的第一天  
	 * @return  
	 */  
	public static String getCurrentMonthFirstDay() { 
	    Calendar calendar = Calendar.getInstance();   
	    calendar.set(Calendar.DAY_OF_MONTH, calendar   
	            .getActualMinimum(Calendar.DAY_OF_MONTH));   
	    return getConvertDateFormat("yyyy-MM-dd", calendar.getTime());   
	}   
	  
	/**  
	 * 得到本月的最后一天  
	 *   
	 * @return  
	 */  
	public static String getCurrentMonthLastDay() {   
	    Calendar calendar = Calendar.getInstance();   
	    calendar.set(Calendar.DAY_OF_MONTH, calendar   
	            .getActualMaximum(Calendar.DAY_OF_MONTH));   
	    return getConvertDateFormat("yyyy-MM-dd", calendar.getTime());   
	}   
	/**
	 * 
	 * 获取上个月的第一天
	 * 
	 * @return
	 */
	public static String getBeforeMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONTH); // 上个月月份
		int day = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数

		if (month == 0) {
			year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}

	/**
	 * 获取上个月的最一天
	 * 
	 * @return
	 */
	public static String getBeforeMonthLastDay() {
		//实例一个日历单例对象
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONTH); // 上个月月份
		// int day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数
		int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数

		if (month == 0) {
			//year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}
	
	/**
	 * 
	 * 获取下月的第一天
	 * 
	 * @return
	 */
	public static String getNextMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONDAY)+2; // 下个月月份
		/*
		 * 如果是这样的加一的话代表本月的第一天
		 * int month = cal.get(Calendar.MONDAY)+1; 
		 * int month = cal.get(Calendar.MONTH)+1
		 */
		int day = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数

		if (month == 0) {
			year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String Day = year + "-" + month + "-" + day;
		return Day;
	}

	/**
	 * 获取下个月的最一天
	 * 
	 * @return
	 */
	public static String getNextMonthLastDay() {
		//实例一个日历单例对象
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONDAY)+2; // 下个月份
		// int day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数
		int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数

		if (month == 0) {
			//year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}

	/**
	 * 本地时区输出当前日期 GMT时间
	 */
	public static String getLocalDate() {
		Date date = new Date();
		return date.toLocaleString();// date.toGMTString();
	}
	/**
	 * 判断客户端输入的是闰年Leap还是平年Average
	 * @return
	 */
	public static String getLeapOrAverage (int year){
		
		if((year%4==0 && year%100!=0)||year%400==0){
			return year+"闰年";
		}else{
			return year+"平年";
		}
	}
	/**
	 * 测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		 
		System.out.println(new DateHelper().getWeeK_OF_Year());

	}
}

 

package com.org.common.date;

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

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 *@Author:jilongliang
 *@Date:2013-7-27
 *@Version:1.0
 *@Description:时间处理类继承commons-lang3-3.1.jar
 */
public class DateUtil extends org.apache.commons.lang3.time.DateUtils{
	private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss" };

	/**
	 * 将微信消息中的CreateTime转换成标准格式的时间(yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param createTime
	 *            消息创建时间
	 * @return
	 */
	public static String formatTime(String createTime) {
		// 将微信传入的CreateTime转换成long类型,再乘以1000
		long msgCreateTime = Long.parseLong(createTime) * 1000L;
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date(msgCreateTime));
	}
	/** 
     * 获取前/后n天日期(M月d日) 
     *  
     * @return 
     */  
    public static String getMonthDay(int diff) {  
        DateFormat df = new SimpleDateFormat("M月d日");  
        Calendar c = Calendar.getInstance();  
        c.add(Calendar.DAY_OF_YEAR, diff);  
        return df.format(c.getTime());  
    }  
	/**
	 * 获取时间
	 * @return
	 */
	public static long getCreateTime(){
		return new Date().getTime();
	}
	
	/**
	 * 得到当前日期字符串 格式(yyyy-MM-dd)
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}

	/**
	 * 得到当前时间字符串 格式(HH:mm:ss)
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式(yyyy)
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式(MM)
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式(dd)
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式(E)星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}
	
	/**
	 * 日期型字符串转化为日期 格式("yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss" )
	 */
	public static Date parseDate(String str) {
		try {
			return parseDate(str, parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}
	
	/**
	 * s1对比s2 两个日期的大小
	 * @param s1                     日期 字符串
	 * @param s2                     日期 字符串
	 * @param pattern   格式
	 * @return          返回 负数s1小 ,返回0两数相等,返回正整数s1大
	 */
	public static int compare(String s1, String s2, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(df.parse(s1));
			c2.setTime(df.parse(s2));
		} catch (Exception e) {
			System.err.println("日期格式不正确");
		}
		return c1.compareTo(c2); 
	}
	
	/********************新增时间-------------------------******************/
	
	/**
	 * 用来处理时间转换格式的方法
	 * @param formate
	 * @param time
	 * @return
	 */
	private static String getConvertDateFormat(String formate, Date time) {
		SimpleDateFormat dateFormat=new SimpleDateFormat(formate);
		String date=dateFormat.format(time);
		return date;
	}
	/**  
	 * 得到本月的第一天  
	 * @return  
	 */  
	public static String getCurrentMonthFirstDay() { 
	    Calendar calendar = Calendar.getInstance();   
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));   
	    return getConvertDateFormat("yyyy-MM-dd", calendar.getTime());   
	}   
	  
	/**  
	 * 得到本月的最后一天  
	 *   
	 * @return  
	 */  
	public static String getCurrentMonthLastDay() {   
	    Calendar calendar = Calendar.getInstance();   
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));   
	    return getConvertDateFormat("yyyy-MM-dd", calendar.getTime());   
	}   
	/**
	 * 
	 * 获取上个月的第一天
	 * 
	 * @return
	 */
	public static String getBeforeMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONTH); // 上个月月份
		int day = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数

		if (month == 0) {
			year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}

	/**
	 * 获取上个月的最一天
	 * 
	 * @return
	 */
	public static String getBeforeMonthLastDay() {
		//实例一个日历单例对象
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONTH); // 上个月月份
		// int day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数
		int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数

		if (month == 0) {
			//year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}
	
	/**
	 * 
	 * 获取下月的第一天
	 * 
	 * @return
	 */
	public static String getNextMonthFirstDay() {
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONDAY)+2; // 下个月月份
		/*
		 * 如果是这样的加一的话代表本月的第一天
		 * int month = cal.get(Calendar.MONDAY)+1; 
		 * int month = cal.get(Calendar.MONTH)+1
		 */
		int day = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数

		if (month == 0) {
			year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String Day = year + "-" + month + "-" + day;
		return Day;
	}

	/**
	 * 获取下个月的最一天
	 * 
	 * @return
	 */
	public static String getNextMonthLastDay() {
		//实例一个日历单例对象
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int year = 0;
		int month = cal.get(Calendar.MONDAY)+2; // 下个月份
		// int day1 = cal.getActualMinimum(Calendar.DAY_OF_MONTH);//起始天数
		int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH); // 结束天数

		if (month == 0) {
			//year = cal.get(Calendar.YEAR) - 1;
			month = 12;
		} else {
			year = cal.get(Calendar.YEAR);
		}
		String endDay = year + "-" + month + "-" + day;
		return endDay;
	}

	/**
	 * 本地时区输出当前日期 GMT时间
	 */
	public static String getLocalDate() {
		Date date = new Date();
		return date.toLocaleString();// date.toGMTString();
	}
	/**
	 * 判断客户端输入的是闰年Leap还是平年Average
	 * @return
	 */
	public static String getLeapOrAverage (int year){
		
		if((year%4==0 && year%100!=0)||year%400==0){
			return year+"闰年";
		}else{
			return year+"平年";
		}
	}
	/**
	 * 
	 * @return
	 */
	public static String getYYYYMMddHHmmss(){
		DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//String currentTime=df.format(new Date());
		Calendar calendar = Calendar.getInstance();
		
		return df.format(calendar.getTime());
		
	}
	
	/**
	 * 传入的日期按照指定格式进行格式化
	 * @param date
	 * @param format(yyyy-MM-DD或yyyy/MM/DD或yyyy-MM-dd HH:mm:ss)
	 * @return String
	 */
	public static String formatDate(Date date, String format) {
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		return fmt.format(date);
	}
	/**
	 * 获取当前时间是本年的第几周
	 * @return
	 */
	public static String getWeeK_OF_Year(){
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int week=cal.get(Calendar.WEEK_OF_YEAR );
		return  "当日是本年的第"+week+"周";
	}
	/**
	 * 获取当日是本年的的第几天
	 * @return
	 */
	public static String getDAY_OF_YEAR(){
		Calendar cal = Calendar.getInstance();
		Date date = new Date();
		cal.setTime(date);
		int day=cal.get(Calendar.DAY_OF_YEAR );
		return  "当日是本年的第"+day+"天";
	}
	
	/**
	 * 可以根据设置时分秒去执行
	 * @param hour执行的小时
	 * @param Minute执行的分钟
	 * @param Second执行的秒
	 * @return
	 */
	public static Date scheduleTimer(Integer Hour,Integer Minute,Integer Second) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, Hour); // 凌晨1点
		calendar.set(Calendar.MINUTE, Minute);//分
		calendar.set(Calendar.SECOND, Second);//秒
		Date date = calendar.getTime(); // 第一次执行定时任务的时间
		// 如果第一次执行定时任务的时间 小于当前的时间
		// 此时要在 第一次执行定时任务的时间加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
		if (date.before(new Date())) {
			date = addDay(date, 1);
		}
		return date;
	}
	/***
	  * 增加或减少天数  
	  * @param date
	  * @param num
	  * @return
	  */
    public static Date addDay(Date date, int num) {  
        Calendar startDT = Calendar.getInstance();  
        startDT.setTime(date);  
        startDT.add(Calendar.DAY_OF_MONTH, num);  
        return startDT.getTime();  
    }  
    
	public static void main(String[] args) {
		System.out.println(getDate("yyyy-MM-dd"));
	}
}

 

 

 

 

  • 大小: 276.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics