Calendar.getInstance()获取指定时间点(定时)

xiaoxiao2021-02-27  139

    使用Calendar.getInstance()不仅能获取当前的时间,还能指定需要获取的时间点,在项目应用中达到

定时的作用,下面是常用的一些指定时间点使用:

   

package com.data.cockpit.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 日期工具类 * @author sixmonth * @date 2019年6月5日 * */ public class DateUtils { public static void main(String[]args){ System.out.println("时间为:\n"+getDate1() +"\n"+getDate2() +"\n"+getDate3() +"\n"+getDate4() +"\n"+getDate5() +"\n"+getMonthFirstDay(2019,-1) +"\n"+getNestLastMin() +"\n"+getLastDay("2019-05-01") +"\n"+getMonthLastDay(2019,-1)); } /* * Calendar.HOUR_OF_DAY 24小时制 * Calendar.HOUR 12小时制 */ /** * 获取当天0点时间 * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getDate1(){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 0);//控制时 cal.set(Calendar.MINUTE, 0);//控制分 cal.set(Calendar.SECOND, 0);//控制秒 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(cal.getTime()); } /** * 获取当天12点时间 * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getDate2(){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR_OF_DAY, 12); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(cal.getTime()); } /** * 获取本周一0点时间 * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getDate3(){ Calendar cal = Calendar.getInstance(); // 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题 int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天 if (1 == dayWeek) { cal.add(Calendar.DAY_OF_MONTH, -1); } cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0); cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(cal.getTime()); } /** * 获取本月第一天0点时间 * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getDate4(){ Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMinimum(Calendar.DAY_OF_MONTH)); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(cal.getTime()); } /** * 获得本月最后一天24点时间 * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getDate5(){ Calendar cal = Calendar.getInstance(); cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0); cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH)); cal.set(Calendar.HOUR_OF_DAY, 24); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(cal.getTime()); } /** * 获取指定年月的第一天,如果参数格式错误,则重置成当前月份的第一天 * @param year * @param month * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getMonthFirstDay(int year, int month) { Calendar cal = Calendar.getInstance(); if(month>0&&month<=12&&year>0) { //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //设置日历中月份的最小天数 cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DATE)); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); }else { //设置当前年月日 cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH)); //将日时间设置成本月第一天 cal.set(Calendar.DAY_OF_MONTH,cal.getMinimum(Calendar.DAY_OF_MONTH)); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } } /** * 获取指定年月的最后一天,如果参数格式错误,则重置成当前月份的最后一天 * @param year * @param month * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getMonthLastDay(int year, int month) { Calendar cal = Calendar.getInstance(); if(month>0&&month<=12&&year>0) { //设置年份 cal.set(Calendar.YEAR, year); //设置月份 cal.set(Calendar.MONTH, month-1); //设置日历中月份的最大天数 cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DATE)); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); }else { //设置当前年月日 cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH)); //将日时间设置成本月最后一天 cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH)); //格式化日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(cal.getTime()); } } /** * 获取当前时间之:前1分钟的时间 * @param min 自定义间隔分钟,比如:-1 * @return * @author sixmonth * @date 2019年6月5日 * */ public static String getNestLastMin(int min) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.MINUTE, min); //数字参数可修改,+表示n分钟之后,-表示n分钟之前的时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println("前一分钟:"+sdf.format(cal.getTime())); return sdf.format(cal.getTime()); } /** * 获取当前时间之:前一周的时间 * @param pattern 自定义格式化输出时间,比如:"yyyy-MM-dd HH:mm:ss" * @param day 自定义间隔天数,比如:-7 * @return * @author sixmonth * @date 2019年6月6日 * */ public static String getNestLastWeek(String pattern,int day) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.DATE, day); //数字参数可修改,+表示n天之后,-表示n天之前的时间 SimpleDateFormat sdf = new SimpleDateFormat(pattern); System.out.println("前七天时间:"+sdf.format(cal.getTime())); return sdf.format(cal.getTime()); } /** * 获取当前时间之:前一个月的时间 * @param pattern 自定义格式化输出时间,比如:"yyyy-MM-dd HH:mm:ss" * @param mon 时间间隔,比如:-1/+1 * @return * @author sixmonth * @date 2019年6月6日 * */ public static String getNestLastMonth(String pattern,int mon) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.MONTH, mon); //数字参数可修改,+表示n月之后,-表示n月之前的时间 SimpleDateFormat sdf = new SimpleDateFormat(pattern); System.out.println("前一个月时间:"+sdf.format(cal.getTime())); return sdf.format(cal.getTime()); } /** * 获取当前时间之:前一年的时间 * @param pattern 自定义格式化输出时间,比如:"yyyy-MM-dd HH:mm:ss" * @param year 时间间隔,比如:-1/+1 * @return * @author sixmonth * @date 2019年6月6日 * */ public static String getNestLastYear(String pattern,int year) { Calendar cal = Calendar.getInstance(); cal.setTime(new Date()); cal.add(Calendar.YEAR, -1); //数字参数可修改,+表示n年之后,-表示n年之前的时间 SimpleDateFormat sdf = new SimpleDateFormat(pattern); System.out.println("前一年时间:"+sdf.format(cal.getTime())); return sdf.format(cal.getTime()); } /** * 根据指定日期,获取前一天或者后一天时间 * @param date * @param day 时间间隔,比如:-1/+1 * @return * @throws ParseException * @author sixmonth * @date 2019年6月6日 * */ public static String getLastDay(String date,int day){ Calendar cal = Calendar.getInstance(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dateTem; try { dateTem = sdf.parse(date); cal.setTime(dateTem); cal.add(Calendar.DATE, day); //数字参数可修改,+表示n天之后,-表示n天之前的时间 System.out.println("前一天时间:"+sdf.format(cal.getTime())); } catch (ParseException e) { e.printStackTrace(); } return sdf.format(cal.getTime()); } }

   

    控制台输出如下:

 

    参考链接:http://blog.csdn.net/holee_bk/article/details/53456630

 

转载请注明原文地址: https://www.6miu.com/read-13875.html

最新回复(0)