import org.apache.commons.lang3.StringUtils;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
/**
* 将dateTime类型的日期转换成年月日时分的形式
* @author admin
*
*/
public class DateTimeUtil {
/**
* Long类型的时间戳转换成date类型的时间
* @param time
* @return
*/
public static Date timeMiao(Long time){
Date date = new Date(time);
return date;
}
/**
* 获取当前时间毫秒数
* @return
*/
public static String getNowMilliseconds(){
Date currentTime = new Date();
long time = currentTime.getTime();
String s = String.valueOf(time);
return s;
}
/**
* 将毫秒数转换成String类型的yyyy-MM-dd HH-mm-ss时间格式
* @param sstime
* @return
* @throws ParseException
*/
public static Date ssTimeTogetYear(Long sstime) throws ParseException{
Date date2 = new Date();
date2.setTime(sstime);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format2 = format.format(date2);
Date date=format.parse(format2);
return date;
}
/**
* 将毫秒数转换成String类型的yyyy-MM-dd HH-mm-ss时间格式
* @param sstime
* @return
* @throws ParseException
*/
public static Date ssTimeByShortDate(Long sstime) throws ParseException{
Date date2 = new Date();
date2.setTime(sstime);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String format2 = format.format(date2);
Date date=format.parse(format2);
return date;
}
/**
* 获取当前年份(String类型)
* @return
*/
public static String getYear(){
Calendar a=Calendar.getInstance();
int strYear = a.get(Calendar.YEAR);
String year = String.valueOf(strYear);
return year;
}
/**
* 获取两个日期之间的所有日期(yyyy-MM-dd)
* 注释部分:这里是获取两个日期之间的所有日期(不包涵begin和end)
* @Description TODO
* @param begin
* @param end
* @return
*/
public static List<Date> getBetweenDates(Date begin, Date end) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Date> result = new ArrayList<Date>();
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
/* Calendar tempEnd = Calendar.getInstance();
tempStart.add(Calendar.DAY_OF_YEAR, 1);
tempEnd.setTime(end);
while (tempStart.before(tempEnd)) {
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
}*/
while(begin.getTime()<=end.getTime()){
result.add(tempStart.getTime());
tempStart.add(Calendar.DAY_OF_YEAR, 1);
begin = tempStart.getTime();
}
return result;
}
/**
* yyyy-MM-dd HH:mm:ss转换成yyyy年MM月dd日 HH时mm分ss秒
* @param date
* @return
*/
public static String encodeDownloadFilename(Date date){
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
return df.format(date);
}
/**
* yyyy-MM-dd HH:mm:ss转换成yyyy年MM月dd日 HH时mm分
* @param date
* @return
*/
public static String encodeMin(Date date){
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
return df.format(date);
}
/**
* yyyy-MM-dd转换成yyyy年MM月dd日
* @param date
* @return
*/
public static String encodeDay(Date date){
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
return df.format(date);
}
/**
* 获取当前时间月(String)
* @return
*/
public static String systemTime(){
Date day=new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
return df.format(day);
}
/**
* 获取当前时间(Date)
* @return
*/
public static String systemTimeDate(){
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = df.format(new Date());
return format;
}
/**
* 获取时间(day为0时为当前时间)
*
* @return返回短时间格式 yyyy-MM-dd
*/
public static Date getSqlDate(int day) {
//System.currentTimeMillis()
Date sqlDate = new java.sql.Date(System.currentTimeMillis());
return sqlDate;
}
/**
* 获取现在时间 (此方法获取当前时间会抛出异常,建议使用getNow()方法)
* @throws ParseException
*
* @return返回长时间格式 yyyy-MM-dd HH:mm:ss
*/
public static Date getNowDate() throws ParseException {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
Date parse = formatter.parse(dateString);
// ParsePosition pos = new ParsePosition(19);
// Date currentTime_2 = formatter.parse(dateString, pos);
return parse;
}
/**
* 获取现在时间
*
* @return返回短时间格式 yyyy-MM-dd
*/
public static Date getNowDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
Date dat = DateTimeUtil.strToDate(dateString);
return dat;
}
/**
* 获取现在时间
*
* @return返回字符串格式 yyyy-MM-dd HH:mm:ss
*/
public static String getStringDate() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取现在时间
*
* @return 返回短时间字符串格式yyyy-MM-dd
*/
public static String getStringDateShort() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 获取时间 小时:分;秒 HH:mm:ss
*
* @return
*/
public static String getTimeShort() {
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date currentTime = new Date();
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
*
* @param strDate
* @return
*/
public static Date strToDateLong(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
*
* @param dateDate
* @return
*/
public static String dateToStrLong(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式时间转换为字符串 yyyy-MM-dd
*
* @param dateDate
* @param
* @return
*/
public static String dateToStr(Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd
*
* @param strDate
* @return
*/
public static Date strToDate(String strDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(strDate, pos);
return strtodate;
}
/**
* 得到现在时间
*
* @return
*/
public static Date getNow() {
Date currentTime = new Date();
return currentTime;
}
/**
* 提取一个月中的最后一天
*
* @param day
* @return
*/
public static Date getLastDate(long day) {
Date date = new Date();
long date_3_hm = date.getTime() - 3600000 * 34 * day;
Date date_3_hm_date = new Date(date_3_hm);
return date_3_hm_date;
}
/**
* 得到现在时间
*
* @return 字符串 yyyyMMdd HHmmss
*/
public static String getStringToday() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 得到现在小时
*/
public static String getHour() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String hour;
hour = dateString.substring(11, 13);
return hour;
}
/**
* 得到现在分钟
*
* @return
*/
public static String getTime() {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(currentTime);
String min;
min = dateString.substring(14, 16);
return min;
}
/**
* 根据用户传入的时间表示格式,返回当前时间的格式 如果是yyyyMMdd,注意字母y不能大写。
*
* @param sformat
* yyyyMMddhhmmss
* @return
*/
public static String getUserDate(String sformat) {
Date currentTime = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(sformat);
String dateString = formatter.format(currentTime);
return dateString;
}
/**
* 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式,返回字符型的分钟
*/
public static String getTwoHour(String st1, String st2) {
String[] kk = null;
String[] jj = null;
kk = st1.split(":");
jj = st2.split(":");
if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0])){
return "0";
} else {
double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1])
/ 60;
double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1])
/ 60;
if ((y - u) > 0) {
return y - u + "";
}else {
return "0";
}
}
}
/**
* 得到二个日期间的间隔天数
*/
public static String getTwoDay(String sj1, String sj2) {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
long day = 0;
try {
Date date = myFormatter.parse(sj1);
Date mydate = myFormatter.parse(sj2);
day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
} catch (Exception e) {
return "";
}
return day + "";
}
/**
* 时间前推或后推分钟,其中JJ表示分钟.
*/
public static String getPreTime(String sj1, String jj) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String mydate1 = "";
try {
Date date1 = format.parse(sj1);
long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
date1.setTime(Time * 1000);
mydate1 = format.format(date1);
} catch (Exception e) {
}
return mydate1;
}
/**
* 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
*/
public static String getNextDay(String nowdate, String delay) {
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String mdate = "";
Date d = strToDate(nowdate);
long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24
* 60 * 60;
d.setTime(myTime * 1000);
mdate = format.format(d);
return mdate;
} catch (Exception e) {
return "";
}
}
/**
* 判断是否润年
*
* @param ddate
* @return
*/
public static boolean isLeapYear(String ddate) {
/**
* 详细设计: 1.被400整除是闰年,否则: 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
* 3.能被4整除同时能被100整除则不是闰年
*/
Date d = strToDate(ddate);
GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
gc.setTime(d);
int year = gc.get(Calendar.YEAR);
if ((year % 400) == 0){
return true;
} else if ((year % 4) == 0) {
if ((year % 100) == 0){
return false;
}else {
return true;
}
} else {
return false;
}
}
/**
* 返回美国时间格式 26 Apr 2006
*
* @param str
* @return
*/
public static String getEDate(String str) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(str, pos);
String j = strtodate.toString();
String[] k = j.split(" ");
return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
}
/**
* 获取一个月的最后一天
*
* @param dat
* @return
*/
public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
String str = dat.substring(0, 8);
String month = dat.substring(5, 7);
int mon = Integer.parseInt(month);
if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8
|| mon == 10 || mon == 12) {
str += "31";
} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
str += "30";
} else {
if (isLeapYear(dat)) {
str += "29";
} else {
str += "28";
}
}
return str;
}
/**
* 判断二个时间是否在同一个周
*
* @param date1
* @param date2
* @return
*/
public static boolean isSameWeekDates(Date date1, Date date2) {
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
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;
}
/**
* 产生周序列,即得到当前时间所在的年度是第几周
*
* @return
*/
public static String getSeqWeek() {
Calendar c = Calendar.getInstance(Locale.CHINA);
String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
if (week.length() == 1){
week = "0" + week;
}
String year = Integer.toString(c.get(Calendar.YEAR));
return year + week;
}
/**
* 获得一个日期所在的周的星期几的日期
*
* @param sdate 长类型yyyy-MM-dd HH:mm:ss
* @param num 星期一:1,.....星期六:6,星期天:0
* @return 短类型yyyy-MM-dd 如:2017-12-11
*/
public static String getWeek(String sdate, String num) {
// 再转换为时间
Date dd = strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(dd);
switch (num) {
case "1":
// 返回星期一所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
break;
case "2":
// 返回星期二所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
break;
case "3":
// 返回星期三所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
break;
case "4":
// 返回星期四所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
break;
case "5":
// 返回星期五所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
break;
case "6":
// 返回星期六所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
break;
case "0":
// 返回星期日所在的日期
c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
break;
default:
break;
}
return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
}
/**
* 根据一个日期,返回是星期几的字符串
*
* @param sdate
* @return
*/
public static String getWeek(String sdate) {
// 再转换为时间
Date date = strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
// int hour=c.get(Calendar.DAY_OF_WEEK);
// hour中存的就是星期几了,其范围 1~7
// 1=星期日 7=星期六,其他类推
return new SimpleDateFormat("EEEE").format(c.getTime());
}
public static String getWeekStr(String sdate) {
String str = "";
str = getWeek(sdate);
switch (str) {
case "1":
str = "星期日";
break;
case "2":
str = "星期一";
break;
case "3":
str = "星期二";
break;
case "4":
str = "星期三";
break;
case "5":
str = "星期四";
break;
case "6":
str = "星期五";
break;
case "7":
str = "星期六";
break;
default:
break;
}
return str;
}
/**
* 两个时间之间的天数
*
* @param date1
* @param date2
* @return
* @throws ParseException
*/
public static long getDays(String date1, String date2) throws ParseException {
if (date1 == null || "".equals(date1)) {
return 0;
}
if (date2 == null || "".equals(date2)) {
return 0;
}
// 转换为标准时间
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
Date date = myFormatter.parse(date1);
Date mydate = myFormatter.parse(date2);
long day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
/**
* 形成如下的日历 , 根据传入的一个时间返回一个结构 星期日 星期一 星期二 星期三 星期四 星期五 星期六 下面是当月的各个时间
* 此函数返回该日历第一行星期日所在的日期
*
* @param sdate
* @return
*/
public static String getNowMonth(String sdate) {
// 取该时间所在月的一号
sdate = sdate.substring(0, 8) + "01";
// 得到这个月的1号是星期几
Date date = strToDate(sdate);
Calendar c = Calendar.getInstance();
c.setTime(date);
int u = c.get(Calendar.DAY_OF_WEEK);
String newday = getNextDay(sdate, (1 - u) + "");
return newday;
}
/**
* 取得数据库主键 生成格式为yyyymmddhhmmss+k位随机数
*
* @param k
* 表示是取几位随机数,可以自己定
*/
public static String getNo(int k) {
return getUserDate("yyyyMMddhhmmss") + getRandom(k);
}
/**
* 返回一个随机数
*
* @param i
* @return
*/
public static String getRandom(int i) {
Random jjj = new Random();
// int suiJiShu = jjj.nextInt(9);
if (i == 0) {
return "";
}
String jj = "";
for (int k = 0; k < i; k++) {
jj = jj + jjj.nextInt(9);
}
return jj;
}
//获取两个时间之间的时间差(小时)
public static long getDatePoor(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return hour;
}
//获取两个时间之间的时间差(天)
public static long getDatePoorDay(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day;
}
/**
* 获取当前年月日(格式:yyyy年MM月dd日)
* @return
*/
public static String getNowDateChina(){
Date d = new Date();
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
String s = df.format(d);
return s;
}
/**
* 比较两个日期的先后顺序
* @param date1(前面的日期)
* @param date2(后面的日期)
* @return error:比较错误,1:date2比date1晚.0:date2与date1日期相等.-1:date2比date1早
*/
public static String thenTwoDate(Date date1,Date date2){
String then="error";
if (date2.getTime()-date1.getTime() >0) {
then = "1";
}else if (date2.getTime()-date1.getTime() ==0) {
then = "0";
} else {
then = "-1";
}
return then;
}
/**
* 返回前一天日期
* @param
* @param
* @return
*/
public static Date beforeDate(){
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
return date;
}
/**
* 获取当前月份
* @return
*/
public static int getMonth(){
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int i=calendar.get(Calendar.MONTH)+1;
return i;
}
/**
* 返回前一天日期YYYY-MM-dd
* @param
* @param
* @return
*/
public static Date beforeDateType(){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
String format = sdf.format(date);
Date date1 = DateTimeUtil.strToDate(format);
return date1;
}
/**
* 获取本周所有日期
* @return
*/
public static String nowWeekAll(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
// 今天是一周中的第几天
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK );
if (c.getFirstDayOfWeek() == Calendar.SUNDAY) {
c.add(Calendar.DAY_OF_MONTH, 1);
}
// 计算一周开始的日期
c.add(Calendar.DAY_OF_MONTH, -dayOfWeek);
StringBuffer stringBuffer = new StringBuffer();
for (int i=1;i<=7;i++) {
c.add(Calendar.DAY_OF_MONTH, 1);
String format = sdf.format(c.getTime());
stringBuffer.append("'"+format+"'");
if(i!=7){
stringBuffer.append(",");
}
}
return stringBuffer.toString();
}
/**
* 字符串格式转Date
* @param time
* @param format
* @return
*/
public static Date strToDate(String time,String format){
Date date=null;
DateFormat format1 = new SimpleDateFormat(format);
try {
date = format1.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
/**
* 获取本月所有日期
* @return
*/
public static String getAllTheDateOftheMonth() {
StringBuffer stringBuffer = new StringBuffer();
Date now = DateTimeUtil.getNow();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.DATE, 1);
int month = cal.get(Calendar.MONTH);
while(cal.get(Calendar.MONTH) == month){
String format = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 1);
stringBuffer.append("'"+format+"'");
stringBuffer.append(",");
}
String substring = stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
return substring;
}
/**
* 获取两个日期之间所有日期的String类型
* @return
*/
public static String getBetweenDatesString(String beginStr, String endStr) {
Date begin = DateTimeUtil.strToDate(beginStr);
Date end = DateTimeUtil.strToDate(endStr);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<Date> betweenDates = DateTimeUtil.getBetweenDates(begin, end);
StringBuffer stringBuffer = new StringBuffer();
int i = 0;
for (Date betweenDate : betweenDates) {
i++;
String format = sdf.format(betweenDate);
stringBuffer.append("'"+format+"'");
if(betweenDates.size()!=i){
stringBuffer.append(",");
}
}
return stringBuffer.toString();
}
/**
* 返回昨天 MM月dd日
* @return
*/
public static String returnMMdd(){
Date date = DateTimeUtil.beforeDateType();
DateFormat df = new SimpleDateFormat("MM月dd日");
return df.format(date);
}
/**
* 返回前一天日期YYYY-MM-dd
* @return
*/
public static String beforeStringDate(){
Date date = DateTimeUtil.beforeDateType();
String s = DateTimeUtil.dateToStr(date);
return s;
}
/**
* 获取当前日期前一天的本周所有日期
* @return
*/
public static String beforeWeekAll(){
Calendar cal = Calendar.getInstance();
Date date = DateTimeUtil.beforeDateType();
cal.setTime(date);
int d = 0;
if(cal.get(Calendar.DAY_OF_WEEK)==1){
d = -6;
}else{
d = 2-cal.get(Calendar.DAY_OF_WEEK);
}
cal.add(Calendar.DAY_OF_WEEK, d);
//所在周开始日期
String format1 = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
//所在周结束日期
String format2 = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
String betweenDatesString = DateTimeUtil.getBetweenDatesString(format1, format2);
return betweenDatesString;
}
/**
* 获取当前日期前一天的本月所有日期
* @return
*/
public static String beforeMonthAll(){
Date date = DateTimeUtil.beforeDateType();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
int first = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, first);
Date day_first = cal.getTime();
String str_day_first = DateTimeUtil.dateToStr(day_first);
int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
Date day_last = cal.getTime();
String str_day_last = DateTimeUtil.dateToStr(day_last);
String betweenDatesString = DateTimeUtil.getBetweenDatesString(str_day_first, str_day_last);
return betweenDatesString;
}
/**
* 获得当天0点时间
* @return
*/
public static long getTimesmorning(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (Long) (cal.getTimeInMillis());
}
/**
* 获得当天24点时间
* @return
*/
public static long getTimesnight(){
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
return (Long) (cal.getTimeInMillis());
}
/**
* 获得某天24点时间,返回Date
* @return
*/
public static Date getTime24(Date parameterDate) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(parameterDate);
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
long timeInMillis = cal.getTimeInMillis();
Date date = ssTimeTogetYear(timeInMillis);
return date;
}
/**
* 获得某天0点时间,返回Date
* @return
*/
public static Date getTime00(Date parameterDate) throws ParseException {
Calendar cal = Calendar.getInstance();
cal.setTime(parameterDate);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
Long timeInMillis = (cal.getTimeInMillis());
Date date = ssTimeTogetYear(timeInMillis);
return date;
}
/**
* 获得当天24点时间,返回Date
* @return
*/
public static Date getTime24() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 24);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
long timeInMillis = cal.getTimeInMillis();
Date date = ssTimeTogetYear(timeInMillis);
return date;
}
/**
* 获得当天0点时间,返回Date
* @return
*/
public static Date getTime00() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.MILLISECOND, 0);
Long timeInMillis = (cal.getTimeInMillis());
Date date = ssTimeTogetYear(timeInMillis);
return date;
}
/**
* 获得指定日期的前一天
* @param specifiedDay
* @return
* @throws Exception
*/
public static String getSpecifiedDayBefore(String specifiedDay){
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
Date date=null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayBefore;
}
/**
* 获取指定日期后一天的日期
* @param specifiedDay
* @return
*/
public static String getSpecifiedDayAfter(String specifiedDay){
Calendar c = Calendar.getInstance();
Date date = null;
try {
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
} catch (ParseException e) {
e.printStackTrace();
}
c.setTime(date);
int day1 = c.get(Calendar.DATE);
c.set(Calendar.DATE, day1 + 1);
String dayAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
return dayAfter;
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd 00:00:00
*
* @param strDate00
* @return
*/
public static Date strToDate00(String strDate00) {
String s = strDate00.substring(0,10) + " 00:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(s, pos);
return strtodate;
}
/**
* 将短时间格式字符串转换为时间 yyyy-MM-dd 23:59:59
*
* @param strDate
* @return
*/
public static Date strToDate24(String strDate) {
String s = strDate.substring(0,10) + " 23:59:59";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(s, pos);
return strtodate;
}
public static Long newDateToLong(){
Date date = new Date();
long time = date.getTime();
return time;
}
/**
* 测试字符串是否是数字
* @param string
* @return
*/
public static boolean isNumber(String string) {
if (string == null){
return false;
}else {
Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
return pattern.matcher(string).matches();
}
}
/**
* 获取指定年份的yyyy-01-01 00:00:00
*
* @param strDateyyyy
* @return
*/
public static Date strToDateyyyy(String strDateyyyy) {
String s = strDateyyyy.substring(0,4) + "-01-01 00:00:00";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(s, pos);
return strtodate;
}
/**
* 获取当前年份指定的几年前的1月1日0时0分0秒
*
* @param year 由当前年份开始计算,如2019年的3年前为2017年
* @return
*/
public static String getNowYearToYear(Integer year) {
String s = dateToStrLong(strToDateyyyy(String.valueOf(Integer.valueOf(getYear()) - year+1)));
return s;
}
/**
* 获取两个日期之间的所有年份(yyyy),如果为当年则获取月份
* 注释部分:这里是获取两个日期之间的所有日期(不包涵begin和end)
* @Description TODO
* @param begin
* @param end
* @return
*/
public static HashMap<String, Object> betweenYear(Date begin, Date end) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
HashMap<String, Object> stringListHashMap = new HashMap<>();
List<String> result = new ArrayList<>();
String dateToYearBegin = getDateToYear(begin);
String dateToYearEnd = getDateToYear(end);
if(dateToYearBegin.equals(dateToYearEnd)){
Integer beginMonth = Integer.valueOf(getDateToMonth(begin));
Integer endMonth = Integer.valueOf(getDateToMonth(end));
Calendar tempStartMonth = Calendar.getInstance();
tempStartMonth.setTime(begin);
while (beginMonth <= endMonth){
result.add(String.valueOf(tempStartMonth.get(Calendar.MONTH)+1));
tempStartMonth.add(Calendar.MONTH, 1);
beginMonth += 1;
}
stringListHashMap.put("IsMonth",true);
}else {
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(begin);
while(begin.getYear()<=end.getYear()){
result.add(String.valueOf(tempStart.getWeekYear()));
tempStart.add(Calendar.YEAR, 1);
begin = tempStart.getTime();
}
stringListHashMap.put("IsMonth",false);
}
stringListHashMap.put("list_String",result);
return stringListHashMap;
}
/**
* 获取指定日期的年份
* @Description TODO
* @return
*/
public static String getDateToYear(Date date) {
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(date);
int i = tempStart.get(Calendar.YEAR);
String s = String.valueOf(i);
return s;
}
/**
* 获取指定日期的月份
* @Description TODO
* @return
*/
public static String getDateToMonth(Date date) {
Calendar tempStart = Calendar.getInstance();
tempStart.setTime(date);
int i = tempStart.get(Calendar.MONTH);
String s = String.valueOf(i);
return s;
}
/**
* 获取某年某月的第一天日期的0时0分0秒
* @param year
* @param month
* @return
*/
public static Date getStartMonthDate(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
Date time = calendar.getTime();
String s = dateToStr(time);
Date date = strToDate00(s);
return date;
}
/**
* 获取某年某月的最后一天日期23时59分59秒
* @param year
* @param month
* @return
*/
public static Date getEndMonthDate(int year, int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, 1);
int day = calendar.getActualMaximum(5);
calendar.set(year, month - 1, day);
Date time = calendar.getTime();
String s = dateToStr(time);
Date date = strToDate24(s);
return date;
}
/**
* 获取传入年份的第一天
* @param year
* @return
*/
public static Date getStartYearDate(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, 0, 1);
Date time = calendar.getTime();
String s = dateToStr(time);
Date date = strToDate00(s);
return date;
}
/**
* 获取传入年份的最后一天
* @param year
* @return
*/
public static Date getEndYearDate(int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, 0, 1);
int day = calendar.getActualMaximum(5);
calendar.set(year, 11, day);
Date time = calendar.getTime();
String s = dateToStr(time);
Date date = strToDate24(s);
return date;
}
/**
* 获取传入日期所在月的第一天
* @param date
* @return
*/
public static Date getFirstDayDateOfMonth(Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
return cal.getTime();
}
/**
* 获取传入日期所在月的最后一天
* @param date
* @return
*/
public static Date getLastDayOfMonth(Date date) {
final Calendar cal = Calendar.getInstance();
cal.setTime(date);
final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
cal.set(Calendar.DAY_OF_MONTH, last);
return cal.getTime();
}
/**
* 获取传入日期所在月的最后一天
* @param date 传入String yyyy-MM-dd 00:00:00
* @return
*/
public static String getFirstDayDateOfMonthString(String date) {
Date firstDayDateOfMonth = getFirstDayDateOfMonth(strToDateLong(date));
String s = dateToStrLong(firstDayDateOfMonth);
Date date1 = strToDate00(s);
String s1 = dateToStrLong(date1);
return s1;
}
/**
* 获取传入日期所在月的第一天
* @param date 传入String yyyy-MM-dd HH:mm:ss
* @return
*/
public static String getLastDayOfMonthString(String date) {
Date lastDayOfMonth = getLastDayOfMonth(strToDateLong(date));
String s = dateToStrLong(lastDayOfMonth);
Date date1 = strToDate00(s);
String s1 = dateToStrLong(date1);
return s1;
}
/**
* 获取实际之间的月份
* @param minDate
* @param maxDate
* @return 返回格式yyyy-MM集合
* @throws ParseException
*/
public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
ArrayList<String> result = new ArrayList<String>();
//格式化为年月
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
Calendar min = Calendar.getInstance();
Calendar max = Calendar.getInstance();
min.setTime(sdf.parse(minDate));
min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);
max.setTime(sdf.parse(maxDate));
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);
}
return result;
}
public static void main(String[] args) throws ParseException {
/*System.out.println(dateToStrLong(getNow()));
System.out.println(dateToStrLong(strToDate00("2019-09-16")));*/
/*System.out.println(getNowYearToYear(3));
String join = StringUtils.join(betweenYear(strToDateLong("2016-09-16 00:00:00"),strToDateLong("2019-09-16 00:00:00")).toArray(), ",");
System.out.println(join);*/
/*System.out.println(betweenYear(strToDateLong("2012-10-16 00:00:00"),strToDateLong("2016-12-16 00:00:00")));
System.out.println(getDateToYear(strToDateLong("2012-10-16 00:00:00")));
System.out.println(dateToStrLong(getStartMonthDate(2019,9)));
System.out.println(dateToStrLong(getEndMonthDate(2019,9)));*/
System.out.println(getFirstDayDateOfMonthString("2019-09-19 12:11:00"));
System.out.println(getLastDayOfMonthString("2019-09-19 12:11:00"));
List<String> monthBetween = getMonthBetween("2018-09-19 00:00:00", "2019-12-19 00:00:00");
for (String s : monthBetween) {
String[] split = s.split("-");
Date startMonthDate = getStartMonthDate(Integer.valueOf(split[0]),Integer.valueOf(split[1]));
Date endMonthDate = getEndMonthDate(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
System.out.println(DateTimeUtil.dateToStrLong(startMonthDate)+"********"+dateToStrLong(endMonthDate));
}
}
}