java的日期转换工具类,集大成者

package com.lianqiang.tmms.util;

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 CommentDate {
	public static String Commentdat(Date time) throws ParseException {
		SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
		long d1 = new Date().getTime();
		long a = time.getTime();
		long d = a / 1000;
		long c = d1 / 1000;
		long f = c - d;
		String g;
		if (f < 60) {
			// 一分钟内
			g = "刚刚";
		} else if (f < (60 * 60)) {
			// 一小时内
			g = f / (60) + "分钟前";
		} else if (f < (60 * 60 * 24)) {
			// 一天内
			g = f / (60 * 60) + "小时前";
		} else {
			// 一个月后
			g = bartDateFormat.format(time);
		}
		return g;
	}

	// 整数转成时:分:秒
	public static String secToTime(int time) {
		String timeStr = null;
		int hour = 0;
		int minute = 0;
		int second = 0;
		if (time <= 0)
			return "00:00:00";
		else {
			minute = time / 60;
			if (minute < 60) {
				second = time % 60;
				timeStr = "00:" + unitFormat(minute) + ":" + unitFormat(second);
			} else {
				hour = minute / 60;
				if (hour > 99)
					return "99:59:59";
				minute = minute % 60;
				second = time - hour * 3600 - minute * 60;
				timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
			}
		}
		return timeStr;
	}

	public static String unitFormat(int i) {
		String retStr = null;
		if (i >= 0 && i < 10)
			retStr = "0" + Integer.toString(i);
		else
			retStr = "" + i;
		return retStr;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 字符串转时间类型
	 */
	public static Date get_String_date(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date2 = sdf.parse(date);
		// long currentTime = date2.getTime();
		// currentTime +=24*60*60*1000-1000;//加23小时59分59秒
		// Date date3=new Date(currentTime);
		return date2;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 字符串转化为当天23点
	 */
	public static Date get_String_date_lingchen(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date2 = sdf.parse(date);
		long currentTime = date2.getTime();
		currentTime += 24 * 60 * 60 * 1000 - 1000;// 加23小时59分59秒
		Date date3 = new Date(currentTime);
		return date3;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 带时间的日期转换
	 */
	public static Date get_leave_date(String date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date2 = sdf.parse(date);
		// long currentTime = date2.getTime();
		// currentTime +=24*60*60*1000-1000;//加23小时59分59秒
		// Date date3=new Date(currentTime);
		return date2;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 事件类型转化为字符串
	 */
	public static String get_date_string(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String date2 = sdf.format(date);
		// long currentTime = date2.getTime();
		// currentTime +=24*60*60*1000-1000;//加23小时59分59秒
		// Date date3=new Date(currentTime);
		return date2;
	}

	/**
	 * @ClassName: CommentDate.java
	 * @Function: CommentDate.java
	 * @Description: 获取月份
	 * @date: 2018年10月9日 上午9:05:58
	 */
	public static String get_date_yue_string(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String date2 = sdf.format(date);
		// long currentTime = date2.getTime();
		// currentTime +=24*60*60*1000-1000;//加23小时59分59秒
		// Date date3=new Date(currentTime);
		return date2;
	}

	/**
	 * @ClassName: CommentDate.java
	 * @Function: CommentDate.java
	 * @Description: 获取年份
	 * @date: 2018年9月14日 下午4:21:30
	 */
	public static String get_date_nian_string(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		String date2 = sdf.format(date);
		return date2;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 转为String 精确到时分秒
	 */
	public static String get_date_string_mmss(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date2 = sdf.format(date);
		// long currentTime = date2.getTime();
		// currentTime +=24*60*60*1000-1000;//加23小时59分59秒
		// Date date3=new Date(currentTime);
		return date2;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 说说专用
	 */
	public static String get_employee_string(Date date) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("MM.dd");
		String date2 = sdf.format(date);
		// long currentTime = date2.getTime();
		// currentTime +=24*60*60*1000-1000;//加23小时59分59秒
		// Date date3=new Date(currentTime);
		return date2;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 获取今天周几
	 */
	public static String getWeekOfDate(Date date) {
		String[] weekOfDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0) {
			w = 0;
		}
		return weekOfDays[w];
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 获取当天的0点
	 */
	public static Date getStartTime() {
		Calendar todayStart = Calendar.getInstance();
		todayStart.set(Calendar.HOUR_OF_DAY, 0);
		todayStart.set(Calendar.MINUTE, 0);
		todayStart.set(Calendar.SECOND, 0);
		todayStart.set(Calendar.MILLISECOND, 0);
		return todayStart.getTime();
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 获取当天的23点
	 */
	public static Date getnowEndTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR_OF_DAY, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 获取本周一的日期
	 */
	public static String getzhouyi() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 获取本周五的日期
	 */
	public static String getzhouwu() {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return dateFormat.format(calendar.getTime());
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 计算日期的相差天数
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * @Title: LogNoteContrller.java
	 * @Package com.jingren.jing.personal.controller.notelog
	 * @Description: TODO 获取年月列表
	 */
	public static List<String> get_nianyuelist() throws ParseException {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -5);
		String before_six = c.get(Calendar.YEAR) + "-" + c.get(Calendar.MONTH);// 六个月前
		ArrayList<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");// 格式化为年月
		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();
		min.setTime(sdf.parse(before_six));
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
		max.setTime(sdf.parse(sdf.format(new Date())));
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}
		// System.out.println(result);
		return result;
	}


	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 年的第一天
	 */
	public static Date getNianStartTime(String nian) throws ParseException {
		Date now = get_String_date(nian + "-1-1");
		return now;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.jingren.jing.util
	 * @Description: TODO 年的最后一天
	 */
	public static Date getNianEndTime(String nian) throws ParseException {
		Date now = get_String_date_lingchen(nian + "-12-31");
		return now;
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.lianqiang.supervisor.util
	 * @Description: TODO 获取当前月第一天
	 */
	public static Date getYueStartTime() throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		// 获取当前月第一天:
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 0);
		c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String first = format.format(c.getTime());
		return get_String_date(first);
	}

	/**
	 * @Title: CommentDate.java
	 * @Package com.lianqiang.supervisor.util
	 * @Description: TODO 获取当前月最后一天
	 */
	public static Date getYueEndtTime() throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
		String last = format.format(ca.getTime());
		return get_String_date_lingchen(last);
	}

	/**
	* @Title: CommentDate.java 
	* @Package com.lianqiang.bid.util 
	* @Description: TODO 判断当前是第几季度
	* 编程千万条,规范第一条,
	* 编程不规范,同事两行泪
	 */
	public static String GetQuarterByDate(Date dt) {
		int m=getSeason(dt);
		switch (m) {
		case 1:
			return "第一季度";
		case 2:
			return "第二季度";
		case 3:
			return "第三季度";
		case 4:
			return "第四季度";
		}
		return null;
	}
	/**
	* @Title: CommentDate.java 
	* @Package com.lianqiang.tmms.util 
	* @Description: TODO 获取季度的数字
	* 编程千万条,规范第一条,
	* 编程不规范,同事两行泪
	 */
	public static int getSeason(Date date) {
		int season = 0;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int month = c.get(Calendar.MONTH);
		switch (month) {
		case Calendar.JANUARY:
		case Calendar.FEBRUARY:
		case Calendar.MARCH:
			season = 1;
			break;
		case Calendar.APRIL:
		case Calendar.MAY:
		case Calendar.JUNE:
			season = 2;
			break;
		case Calendar.JULY:
		case Calendar.AUGUST:
		case Calendar.SEPTEMBER:
			season = 3;
			break;
		case Calendar.OCTOBER:
		case Calendar.NOVEMBER:
		case Calendar.DECEMBER:
			season = 4;
			break;
		default:
			break;
		}
		return season;
	}
	/**
	* @Title: CommentDate.java 
	* @Package com.lianqiang.tmms.util 
	* @Description: TODO 根据日期计算未来或者过的月份年份天数 
	* 编程千万条,规范第一条,
	* 编程不规范,同事两行泪
	 */
	public static Date lose_time(Integer number){
		Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        // c.add(Calendar.YEAR, number);//年份
        //c.add(Calendar.DATE, number);//天数
        c.add(Calendar.MONTH, number);//月份
        Date m = c.getTime();
		return m;
	}
	/**
	 * @Title: CommentDate.java
	 * @Package com.lianqiang.tmms.util
	 * @Description: TODO 根据日期计算加减小时
	 * 编程千万条,规范第一条,
	 * 编程不规范,同事两行泪
	 */
	public static Date lose_time_hour(Date date,Integer number){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		// c.add(Calendar.YEAR, number);//年份
		//c.add(Calendar.DATE, number);//天数
		c.add(Calendar.HOUR, number);//月份
		Date m = c.getTime();
		return m;
	}
	/**
	* @Title: CommentDate.java 
	* @Package com.lianqiang.tmms.util 
	* @Description: TODO 根据日期计算未来或者过的月份年份天数 
	* 编程千万条,规范第一条,
	* 编程不规范,同事两行泪
	 */
	public static Date lose_new_time(Integer number,Date dt){
		Calendar c = Calendar.getInstance();
        c.setTime(dt);
        // c.add(Calendar.YEAR, number);//年份
        //c.add(Calendar.DATE, number);//天数
        c.add(Calendar.MONTH, number);//月份
        Date m = c.getTime();
		return m;
	}
	/**
	* @Title: CommentDate.java 
	* @Package com.lianqiang.tmms.util 
	* @Description: TODO 计算两个日期的月份 
	* 编程千万条,规范第一条,
	* 编程不规范,同事两行泪
	 */
	public static int get_month_cha(String yue1,String yue2) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(yue1));
        aft.setTime(sdf.parse(yue2));
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
		return Math.abs(month + result);  
	}
	
	public static void main(String[] args) throws Exception {
		Date now = getYueEndtTime();
		//System.out.println(daysBetween(new Date(),get_String_date_lingchen("2020-04-13"))); // 第一天
		System.out.println(lose_time_hour(new Date(), 1));
	}
	
	/**
	* @Title: CommentDate.java 
	* @Package com.lianqiang.tmms.util 
	* @Description: TODO 时间转为时间戳
	 */
	 public static String dateToStamp(String s) throws Exception {
	        String res;//设置时间格式,将该时间格式的时间转换为时间戳
	        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	        Date date = simpleDateFormat.parse(s);
	        long time = date.getTime()/1000;
	        res = String.valueOf(time);
	        return res;
	    }

	 /**
	 * @Title: CommentDate.java 
	 * @Package com.lianqiang.tmms.util 
	 * @Description: TODO 判断白天晚上 
	 * @date 2020年4月20日 上午10:44:59  
	 * 编程千万条,规范第一条,
	 * 编程不规范,同事两行泪
	  */
	 public static String get_day_type(Date st){
	        SimpleDateFormat df = new SimpleDateFormat("HH");
	        String str = df.format(st);
	        int a = Integer.parseInt(str);
	        if (a >= 0 && a <= 6) {
	            System.out.println("凌晨");
	            return "PM";
	        }
	        if (a > 6 && a <= 12) {
	            System.out.println("上午");
	            return "AM";
	        }
	        if (a > 12 && a <= 13) {
	            System.out.println("中午");
	            return "AM";
	        }
	        if (a > 13 && a <= 18) {
	            System.out.println("下午");
	            return "AM";
	        }
	        if (a > 18 && a <= 24) {
	            System.out.println("晚上");
	            return "PM";
	        }
			return null;
	 }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一城烟雨lxf

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值