当前位置 : 主页 > 编程语言 > c++ >

常用的日期转换工具

来源:互联网 收集:自由互联 发布时间:2021-06-30
常用的日期转换工具 package cn.toysuperman.operator.utils;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;public clas
常用的日期转换工具
package cn.toysuperman.operator.utils;

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

public class DateUtil {
	public final static SimpleDateFormat day_sdf = new SimpleDateFormat("yyyy-MM-dd");
	public final static SimpleDateFormat time_sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat time_stamp_sdf = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat day_hour_sdf= new SimpleDateFormat("MM-dd HH:mm");


	public static String getTimeFormat(Date date) {
		return time_sdf.format(date);
	}

	public static String getDayFormat(Date date) {
		return day_sdf.format(date);
	}

	public static String getTimeStampFormat(Date date) {
		return time_stamp_sdf.format(date);
	}

	
	public static String getDayHourFormat(Date date) {
		return day_hour_sdf.format(date);
	}
	
	
	
	public static Date getBeforedays(int beforeDays) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, beforeDays);// 得到前N天
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		Date date = calendar.getTime();
		return date;
	}

	public static Date getToday() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		// calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, 0);
		Date date = calendar.getTime();
		return date;
	}

	/**
	 * 获取8位不重复随机码(取当前时间戳转化为十六进制)
	 * 
	 * @return
	 */
	public static String getHexUniqueId(Date date) {
		long time = date.getTime();
		return Integer.toHexString((int) time);
	}

	/**
	 * 比较两个时间相差多少秒
	 * 
	 */
	public static long diffSecond(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime()) / 1000;
	}

	/**
	 * 比较两个时间相差多少秒
	 * 
	 */
	public static long diffMillSecond(Date date1, Date date2) {
		return (date1.getTime() - date2.getTime());
	}

	/**
	 * 根据日期获得星期
	 * 
	 * @param date
	 * @return
	 */
	public static String getWeekOfDate(Date date) {
		// String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四",
		// "星期五","星期六" };
		String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDaysCode[intWeek];
	}

	/**
	 * 根据日期获得星期
	 * 
	 * @param date
	 * @return
	 */
	public static String getWeekNameOfDate(Date date) {
		String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		// String[] weekDaysCode = { "0", "1", "2", "3", "4", "5", "6" };
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		return weekDaysName[intWeek];
	}

	/**
	 * 获得指定日期前/后周日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getSpecialDay(Date date, int days) {

		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);
		calendar.add(Calendar.DATE, days);// -7:前一周 7:后一周
		return calendar.getTime();

	}

	/**
	 * 获得周一的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getMonday(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);

		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

		return calendar.getTime();

	}

	/**
	 * 获得周三的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getWednesday(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);

		calendar.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);

		return calendar.getTime();

	}

	/**
	 * 获得周五的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFriday(Date date) {

		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);
		calendar.setTime(date);

		calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);

		return calendar.getTime();
	}

	/**
	 * 获得周日的日期
	 * 
	 * @param date
	 * @return
	 */
	public static Date getSunday(Date date) {

		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(date);

		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);

		return cal.getTime();
	}

	/**
	 * 当前日期前几天或者后几天的日期
	 * 
	 * @param n
	 * @return
	 */
	public static Date afterNDay(int n) {

		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(new Date());

		cal.add(Calendar.DATE, n);

		return cal.getTime();

	}

	/**
	 * 判断两个日期是否在同一周
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameWeekDates(Date date1, Date date2) {
		Calendar cal1 = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		cal1.setFirstDayOfWeek(Calendar.MONDAY);
		cal2.setFirstDayOfWeek(Calendar.MONDAY);
		cal1.setTime(date1);
		cal2.setTime(date2);
		int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
		if (0 == subYear) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (1 == subYear && 11 == cal2.get(Calendar.MONTH)) {
			// 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		} else if (-1 == subYear && 11 == cal1.get(Calendar.MONTH)) {
			if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2.get(Calendar.WEEK_OF_YEAR))
				return true;
		}
		return false;
	}

	// 取得日期是某年的第几周
	public static int getWeekOfYear(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTime(date);
		int week_of_year = cal.get(Calendar.WEEK_OF_YEAR);
		return week_of_year;
	}

	// 取得某个月有多少天
	public static int getDaysOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		int days_of_month = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		return days_of_month;
	}

	// 取得两个日期之间的相差多少天
	public static long getDaysBetween(Date date0, Date date1) {
		long daysBetween = (date0.getTime() - date1.getTime()) / 86400000;// 86400000=3600*24*1000
																			// 用立即数,减少乘法计算的开销
		return daysBetween;
	}

	public static Date getZeroClock(Date date) {
		try {
			String zeroClock = DateUtil.day_sdf.format(date);
			return DateUtil.day_sdf.parse(zeroClock);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getDayOfMonth(Date date) {
		int intWeek = 1;
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setFirstDayOfWeek(Calendar.MONDAY);
			calendar.setTime(date);
			intWeek = calendar.get(Calendar.DAY_OF_MONTH);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return intWeek;
	}

	public static Date getFirstDayOfMonth(Date date) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-");
			return day_sdf.parse(sdf.format(date) + "01");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date getLastDayOfMonth(Date date) {
		try {
			return getSpecialDay(getFirstDayOfNextMonth(date), -1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int getLengthOfMonth(Date date) {
		try {
			return getDayOfMonth(getLastDayOfMonth(date));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public static Date getFirstDayOfNextMonth(Date date) {
		try {
			SimpleDateFormat sdf_t = new SimpleDateFormat("yyyy-M-dd");
			SimpleDateFormat sdf_y = new SimpleDateFormat("yyyy");
			SimpleDateFormat sdf_m = new SimpleDateFormat("MM");
			int year = new Integer(sdf_y.format(date));
			int next_month = new Integer(sdf_m.format(date)) + 1;
			if (next_month > 12) {
				year++;
				next_month = 1;
			}
			return sdf_t.parse(year + "-" + next_month + "-01");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	
	/**
	 * 获取指定日期(dateStr)之前的beforeDays天的日期列表
	 * 
	 * @param dateStr
	 * @param beforeDays
	 * @return
	 */
	public static List
 
   getDayListBefore(String dateStr,
			int beforeDays) {
		List
  
    list = new ArrayList
   
    (); Date date = null; try { date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr); } catch (ParseException e) { e.printStackTrace(); } for (int i = beforeDays; i > 0; i--) { Calendar cal = Calendar.getInstance(); cal.setTime(date); int day = cal.get(Calendar.DATE); cal.set(Calendar.DATE, day - i); String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(cal .getTime()); list.add(dayBefore); } return list; } public static List
    
      getDayListBetween(Date start,Date end) { List
     
       list = new ArrayList
      
       (); int beforeDays = (int) getDaysBetween(start, end); for (int i = -beforeDays; i >= 0; i--) { Calendar cal = Calendar.getInstance(); cal.setTime(end); int day = cal.get(Calendar.DATE); cal.set(Calendar.DATE, day - i); String dayBefore = day_sdf.format(cal.getTime()); list.add(dayBefore); } return list; } /* 获取时间差 */ public static String getTimesToNow(String date){ SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String now = format.format(new Date()); String returnText = null; try { long from = format.parse(date).getTime(); long to = format.parse(now).getTime(); int days = (int) ((to - from)/(1000 * 60 * 60 * 24)); if(days == 0){//一天以内,以分钟或者小时显示 int hours = (int) ((to - from)/(1000 * 60 * 60)); if(hours == 0){ int minutes = (int) ((to - from)/(1000 * 60)); if(minutes == 0){ returnText = "刚刚"; }else{ returnText = minutes + "分钟前..."; } }else{ returnText = hours + "小时前..."; } } else if(days == 1){ returnText = "昨天"; }else{ returnText = days + "天前..."; } } catch (ParseException e) { e.printStackTrace(); } return returnText; } public static void main(String[] args) throws ParseException { /* * List
       
         list = DateUtil.getSpecialDayListBefore( * DateUtil.day_sdf.format(new Date()), 7); for (String day : list) { * System.out.println(day); } * System.out.println(DateUtil.day_sdf.parse(list.get(0)).getClass()); */ /* * String weekday = getWeekOfDate(getSunday(new Date())); * System.out.println("weekday : "+ (double)1/2); * System.out.println("sunday : "+ getSunday(new Date())); * System.out.println("monday : "+ getMonday(new Date())); * System.out.println("monday : "+ getSpecialDay(getMonday(new * Date()),7)); */ /* SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd"); String end = "2016-08-01"; String start = "2016-08-31"; Date s = sim.parse(start); Date e = sim.parse(end);*/ // System.out.println(getFirstDayOfMonth(e)); // System.out.println(getFirstDayOfNextMonth(s)); // // System.out.println(DateUtil.getDaysBetween(e,s)); /* List
        
          list =getDayListBetween(e,s); for(String str:list){ System.out.println(str); }*/ System.err.println(getTimesToNow("2017-02-15 12:00")); } }
        
       
      
     
    
   
  
 
网友评论