package et.weixin.common.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 获取时间
*
* @author hmk
*
*/
public class DateFormat {
/**
* 获得今天具体时间
*
* @return
*/
public static String getTodayTime() {
Date date = new Date();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String todayTime = formate.format(date);
return todayTime;
}
/**
* 获得今天日期
*
* @return
*/
public static String getTodatDate() {
Date date = new Date();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
String todayDate = formate.format(date);
return todayDate;
}
/**
* 获得昨天日期
*
* @return
*/
public static String getYesterdayDate() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, -1);
date = calendar.getTime();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");
String yesterdayDate = formate.format(date);
return yesterdayDate;
}
/**
* 获得昨天具体时间
*
* @return
*/
public static String getYesterdayTime() {
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(calendar.DATE, -1);
date = calendar.getTime();
SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String yesterdayTime = formate.format(date);
return yesterdayTime;
}
/**
* 获得当前时间前30分钟
*
* @return
*/
public static String getBeforeFiveSecondTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//有效时间间隔 5秒钟
int valid_time = 30;
//获得目前时间的秒钟
int valid = cd.get(Calendar.MINUTE);
System.out.println(valid);
valid = valid-valid_time;
cd.set(Calendar.MINUTE, valid);
Date dt = cd.getTime();
String beforeFiveSecondTime = df.format(dt);
return beforeFiveSecondTime;
}
public static String getNowMonth() {
Calendar cd = new GregorianCalendar();
Date d = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
String nowMonth = df.format(d);
System.out.println(nowMonth);
return nowMonth;
}
public static String getLastMonth() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
int validMonth=cd.get(Calendar.MONTH)-1;
cd.set(Calendar.MONTH, validMonth);
Date dt = cd.getTime();
String lastMonth = df.format(dt);
return lastMonth;
}
//获取当天的开始时间
public static String getTodayBeginTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.set(Calendar.HOUR_OF_DAY, 0);
cd.set(Calendar.MINUTE, 0);
cd.set(Calendar.SECOND, 0);
Date dt = cd.getTime();
String todayBeginTime = df.format(dt);
System.out.println(todayBeginTime);
return todayBeginTime;
}
//获取当天的结束时间
public static String getTodayEndTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.set(Calendar.HOUR_OF_DAY, 23);
cd.set(Calendar.MINUTE, 59);
cd.set(Calendar.SECOND, 59);
Date dt = cd.getTime();
String todayEndTime = df.format(dt);
System.out.println(todayEndTime);
return todayEndTime;
}
//获取前一天的开始时间
public static String getYesterdayBeginTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int yesterday=cd.get(Calendar.DAY_OF_MONTH)-1;
cd.set(Calendar.DAY_OF_MONTH, yesterday);
cd.set(Calendar.HOUR_OF_DAY, 0);
cd.set(Calendar.MINUTE, 0);
cd.set(Calendar.SECOND, 0);
Date dt = cd.getTime();
String yesterdayBeginTime = df.format(dt);
System.out.println(yesterdayBeginTime);
return yesterdayBeginTime;
}
//获取前一天的结束时间
public static String getYesterdayEndTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
int yesterday=cd.get(Calendar.DAY_OF_MONTH)-1;
cd.set(Calendar.DAY_OF_MONTH, yesterday);
cd.set(Calendar.HOUR_OF_DAY, 23);
cd.set(Calendar.MINUTE, 59);
cd.set(Calendar.SECOND, 59);
Date dt = cd.getTime();
String yesterdayEndTime = df.format(dt);
System.out.println(yesterdayEndTime);
return yesterdayEndTime;
}
//获得当前月的第一天开始时间
public static String getNowMonthBeginTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.add(Calendar.MONTH, 0);
cd.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
cd.set(Calendar.HOUR_OF_DAY, 0);
cd.set(Calendar.MINUTE, 0);
cd.set(Calendar.SECOND, 0);
Date dt = cd.getTime();
String nowMonthBeginTime = df.format(dt);
System.out.println(nowMonthBeginTime);
return nowMonthBeginTime;
}
//获得当前月的第一天开始日期
public static String getNowMonthBeginDate() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cd.add(Calendar.MONTH, 0);
cd.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
Date dt = cd.getTime();
String nowMonthBeginDate = df.format(dt);
System.out.println(nowMonthBeginDate);
return nowMonthBeginDate;
}
//获得当前月最后一天结束时间
public static String getNowMonthEndTime() {
Calendar ca = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
ca.set(Calendar.HOUR_OF_DAY, 23);
ca.set(Calendar.MINUTE, 59);
ca.set(Calendar.SECOND, 59);
String nowmonthendtime = df.format(ca.getTime());
System.out.println(nowmonthendtime);
return nowmonthendtime;
}
//获得当前月最后一天结束日期
public static String getNowMonthEndDate() {
Calendar ca = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String nowmonthenddate = df.format(ca.getTime());
System.out.println(nowmonthenddate);
return nowmonthenddate;
}
//获得前一月的第一天开始时间
public static String getLastMonthBeginTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.add(Calendar.MONTH, -1);
cd.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
cd.set(Calendar.HOUR_OF_DAY, 0);
cd.set(Calendar.MINUTE, 0);
cd.set(Calendar.SECOND, 0);
Date dt = cd.getTime();
String lastMonthBeginTime = df.format(dt);
System.out.println(lastMonthBeginTime);
return lastMonthBeginTime;
}
//获得前一月的第一天开始日期
public static String getLastMonthBeginDate() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cd.add(Calendar.MONTH, -1);
cd.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
Date dt = cd.getTime();
String lastMonthBeginDate = df.format(dt);
System.out.println(lastMonthBeginDate);
return lastMonthBeginDate;
}
//获得前一月的最后一天结束时间
public static String getLastMonthEndTime() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.set(Calendar.DAY_OF_MONTH,0);
cd.set(Calendar.HOUR_OF_DAY, 23);
cd.set(Calendar.MINUTE, 59);
cd.set(Calendar.SECOND, 59);
Date dt = cd.getTime();
String lastMonthEndTime = df.format(dt);
System.out.println(lastMonthEndTime);
return lastMonthEndTime;
}
//获得前一月的最后一天结束时间
public static String getLastMonthEndDate() {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
cd.set(Calendar.DAY_OF_MONTH,0);
Date dt = cd.getTime();
String lastMonthEndDate = df.format(dt);
System.out.println(lastMonthEndDate);
return lastMonthEndDate;
}
//获得当前时间后X天之后的结束时间
public static String getAfterDaysTime(Integer dateLen) {
Calendar cd = new GregorianCalendar();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cd.add(Calendar.DATE, dateLen);
cd.set(Calendar.HOUR_OF_DAY, 23);
cd.set(Calendar.MINUTE, 59);
cd.set(Calendar.SECOND, 59);
Date dt = cd.getTime();
String afterThirtyDaysDate = df.format(dt);
System.out.println(afterThirtyDaysDate);
return afterThirtyDaysDate;
}
//获得本周开始时间
public static String getNowWeekBeginTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cds = Calendar.getInstance();
cds.setFirstDayOfWeek(Calendar.MONDAY);
cds.set(Calendar.HOUR_OF_DAY, 0);
cds.set(Calendar.MINUTE, 0);
cds.set(Calendar.SECOND, 0);
cds.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String thisweekstart = df.format(cds.getTime());
System.out.println("本周第一天:"+thisweekstart);
return thisweekstart;
}
//获得本周结束时间
public static String getNowWeekEndTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cde = Calendar.getInstance();
cde.setFirstDayOfWeek(Calendar.MONDAY);
cde.set(Calendar.HOUR_OF_DAY, 23);
cde.set(Calendar.MINUTE, 59);
cde.set(Calendar.SECOND, 59);
cde.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
String thisweekend = df.format(cde.getTime());
System.out.println("本周最后天:"+thisweekend);
return thisweekend;
}
//获得上周开始时间
public static String getLastWeekBeginTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cdl = Calendar.getInstance();
cdl.setFirstDayOfWeek(Calendar.MONDAY);
cdl.add(Calendar.WEEK_OF_MONTH, -1);
cdl.set(Calendar.DAY_OF_WEEK, 1);
cdl.set(Calendar.HOUR_OF_DAY, 0);
cdl.set(Calendar.MINUTE, 0);
cdl.set(Calendar.SECOND, 0);
cdl.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
String lastweekstart = df.format(cdl.getTime());
System.out.println("上周第一天:"+lastweekstart);
return lastweekstart;
}
//获得上周结束时间
public static String getLastWeekEndTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cdle = Calendar.getInstance();
cdle.setFirstDayOfWeek(Calendar.MONDAY);
cdle.add(Calendar.WEEK_OF_MONTH, -1);
cdle.set(Calendar.DAY_OF_WEEK, 1);
cdle.set(Calendar.HOUR_OF_DAY, 23);
cdle.set(Calendar.MINUTE, 59);
cdle.set(Calendar.SECOND, 59);
String lastweekend = df.format(cdle.getTime());
System.out.println("上周最后天:"+lastweekend);
return lastweekend;
}
//获得3个月前时间
public static String getThreeMonthAgoTime() {
Date date = new Date();
Calendar cald = Calendar.getInstance();
cald.add(cald.MONTH, -3);
date = cald.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String threeMonthAgoTime = df.format(date);
return threeMonthAgoTime;
}
//获得3个月前日期
public static String getThreeMonthAgoDate() {
Date date = new Date();
Calendar cald = Calendar.getInstance();
cald.add(cald.MONTH, -3);
date = cald.getTime();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String threeMonthAgoDate = df.format(date);
return threeMonthAgoDate;
}
public static void main(String[] args) {
DateFormat formate = new DateFormat();
//String todayDate = formate.getTodatDate();
//String todayTime = formate.getTodayTime();
//String yesterdayDate = formate.getYesterdayDate();
//String yesterdayTime = formate.getYesterdayTime();
//String BeforeFiveSecondTime=formate.getBeforeFiveSecondTime();
//String afterThirtyDaysDate=formate.getAfterDaysTime(30);
//System.out.println(BeforeFiveSecondTime);
//System.out.println(todayTime);
//System.out.println(yesterdayDate);
//System.out.println(yesterdayTime);
//System.out.println(BeforeFiveSecondTime);
String startdate = formate.getTodatDate();
System.out.println(startdate);
}
}