package com.yashi.common.utils;
import lombok.extern.slf4j.Slf4j;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
/**
* @Auther:
* @Date: 11:52 2018-10-22
* @Description: 时间处理工具,生成到目前的毫秒
* @Version: 1.0
*/
@Slf4j
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
private static final long ONE_MINUTE = 60;
private static final long ONE_HOUR = 3600;
private static final long ONE_DAY = 86400;
private static final long ONE_MONTH = 2592000;
private static final long ONE_YEAR = 31104000;
public static final String SEARCH_DATE_DAY = "today"; //今天
public static final String SEARCH_DATE_YESTERDAY = "yesterday"; //昨天
public static final String SEARCH_DATE_LATELY_7 = "lately7"; //最近7天
public static final String SEARCH_DATE_LATELY_30 = "lately30"; //最近30天
public static final String SEARCH_DATE_LATELY_365 = "lately365";//最近一年
public static final String SEARCH_DATE_WEEK = "week"; //本周
public static final String SEARCH_DATE_PRE_WEEK = "preWeek"; //上周
public static final String SEARCH_DATE_MONTH = "month"; //本月
public static final String SEARCH_DATE_PRE_MONTH = "preMonth"; //上月
public static final String SEARCH_DATE_YEAR = "year"; //年
public static final String SEARCH_DATE_PRE_YEAR = "preYear"; //上一年
public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMAT_DATE = "yyyy-MM-dd";
public static final String DATE_FORMAT_START = "yyyy-MM-dd 00:00:00";
public static final String DATE_FORMAT_END = "yyyy-MM-dd 23:59:59";
public static final String DATE_FORMAT_MONTH_START = "yyyy-MM-01 00:00:00";
public static final String DATE_FORMAT_YEAR_START = "yyyy-01-01 00:00:00";
public static final String DATE_FORMAT_YEAR_END = "yyyy-12-31 23:59:59";
public static final String yyyyMMdd = "yyyyMMdd";
public static final String yyyy_MM_dd = "yyyy-MM-dd";
public static final String yyyy_MM = "yyyy-MM";
public static final String yyyyYearMMMonthddDay = "yyyy年MM月dd日";
public static final String yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
/**
* 获取当前日期,指定格式
* 描述:<描述函数实现的功能>.
*
* @return
*/
public static Date nowDateTime() {
return strToDate(nowDateTimeStr(), "yyyy-MM-dd HH:mm:ss");
}
/**
* parse a String to Date in a specifies fromat.
*
* @param dateStr
* @param DATE_FORMAT
* @return
* @throws ParseException
*/
public static Date strToDate(String dateStr, String DATE_FORMAT) {
SimpleDateFormat myFormat = new SimpleDateFormat(DATE_FORMAT);
try {
return myFormat.parse(dateStr);
} catch (ParseException e) {
return null;
}
}
/**
* 获取当前日期,指定格式
* 描述:<描述函数实现的功能>.
*
* @return
*/
public static String nowDateTimeStr() {
return nowDate("yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当前日期,指定格式
* 描述:<描述函数实现的功能>.
*
* @return
*/
public static String nowDate(String DATE_FORMAT) {
SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT);
return dft.format(new Date());
}
/**
* 格式化时间
*
* @param date
* @param format
* @return
*/
public static String format(Date date, String format) {
SimpleDateFormat simple = new SimpleDateFormat(format);
return simple.format(date);
}
/**
* 获取当前时间前的分钟时间
*
* @param miunte
* @return
*/
public static String getPastMinute(int miunte) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) - miunte);
Date time = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = format.format(time);
return result;
}
/**
* 获取过去第几天的时间(String)
*
* @param past
* @return
*/
public static String getPastDate(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date today = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = format.format(today);
return result;
}
/**
* 获取过去第几天的时间(Date)
*
* @param past
* @return
*/
public static Date getPastDateTime(int past) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
Date day = calendar.getTime();
return day;
}
/**
* 获取以后第几天的时间
*
* @param later
* @return
*/
public static Date getLaterDate(int later) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + later);
return calendar.getTime();
}
/**
* 获取以后第几天的时间
*
* @param later
* @return
*/
public static String getLaterDateTime(int later) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + later);
Date day = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String result = format.format(day);
return result;
}
/**
* @return yyyy-mm-dd
* 2012-12-25
*/
public static String getDate() {
return getYear() + "-" + getMonth() + "-" + getDay();
}
/**
* @param format
* @return yyyy年MM月dd HH:mm
* MM-dd HH:mm 2012-12-25
*/
public static String getDate(String format) {
SimpleDateFormat simple = new SimpleDateFormat(format);
return simple.format(Calendar.getInstance().getTime());
}
/**
* @return yyyy-MM-dd HH:mm
* 2012-12-29 23:47
*/
public static String getDateAndMinute() {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return simple.format(Calendar.getInstance().getTime());
}
/**
* @return yyyy-MM-dd HH:mm:ss
* 2012-12-29 23:47:36
*/
public static String getFullDate() {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simple.format(Calendar.getInstance().getTime());
}
/**
* 距离今天多久
*
* @param date
* @return
*/
public static String fromToday(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
long time = date.getTime() / 1000;
long now = System.currentTimeMillis() / 1000;
long ago = now - time;
if (ago <= ONE_HOUR) {
return ago / ONE_MINUTE + "分钟前";
} else if (ago <= ONE_DAY) {
return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE)
+ "分钟前";
} else if (ago <= ONE_DAY * 2) {
return "昨天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
+ calendar.get(Calendar.MINUTE) + "分";
} else if (ago <= ONE_DAY * 3) {
return "前天" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
+ calendar.get(Calendar.MINUTE) + "分";
} else if (ago <= ONE_MONTH) {
long day = ago / ONE_DAY;
return day + "天前" + calendar.get(Calendar.HOUR_OF_DAY) + "点"
+ calendar.get(Calendar.MINUTE) + "分";
} else if (ago <= ONE_YEAR) {
long month = ago / ONE_MONTH;
long day = ago % ONE_MONTH / ONE_DAY;
return month + "个月" + day + "天前"
+ calendar.get(Calendar.HOUR_OF_DAY) + "点"
+ calendar.get(Calendar.MINUTE) + "分";
} else {
long year = ago / ONE_YEAR;
int month = calendar.get(Calendar.MONTH) + 1;// JANUARY which is 0 so month+1
return year + "年前" + month + "月" + calendar.get(Calendar.DATE)
+ "日";
}
}
/**
* 距离截止日期还有多长时间
*
* @param date
* @return
*/
public static String fromDeadline(Date date) {
long deadline = date.getTime() / 1000;
long now = (System.currentTimeMillis()) / 1000;
long remain = deadline - now;
if (remain <= ONE_HOUR) {
return "只剩下" + remain / ONE_MINUTE + "分钟";
} else if (remain <= ONE_DAY) {
return "只剩下" + remain / ONE_HOUR + "小时"
+ (remain % ONE_HOUR / ONE_MINUTE) + "分钟";
} else {
long day = remain / ONE_DAY;
long hour = remain % ONE_DAY / ONE_HOUR;
long minute = remain % ONE_DAY % ONE_HOUR / ONE_MINUTE;
return "只剩下" + day + "天" + hour + "小时" + minute + "分钟";
}
}
/**
* 距离今天的绝对时间
*
* @param date
* @return
*/
public static String toToday(Date date) {
long time = date.getTime() / 1000;
long now = (System.currentTimeMillis()) / 1000;
long ago = now - time;
if (ago <= ONE_HOUR) {
return ago / ONE_MINUTE + "分钟";
} else if (ago <= ONE_DAY) {
return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE) + "分钟";
} else if (ago <= ONE_DAY * 2) {
return "昨天" + (ago - ONE_DAY) / ONE_HOUR + "点" + (ago - ONE_DAY)
% ONE_HOUR / ONE_MINUTE + "分";
} else if (ago <= ONE_DAY * 3) {
long hour = ago - ONE_DAY * 2;
return "前天" + hour / ONE_HOUR + "点" + hour % ONE_HOUR / ONE_MINUTE
+ "分";
} else if (ago <= ONE_MONTH) {
long day = ago / ONE_DAY;
long hour = ago % ONE_DAY / ONE_HOUR;
long minute = ago % ONE_DAY % ONE_HOUR / ONE_MINUTE;
return day + "天前" + hour + "点" + minute + "分";
} else if (ago <= ONE_YEAR) {
long month = ago / ONE_MONTH;
long day = ago % ONE_MONTH / ONE_DAY;
long hour = ago % ONE_MONTH % ONE_DAY / ONE_HOUR;
long minute = ago % ONE_MONTH % ONE_DAY % ONE_HOUR / ONE_MINUTE;
return month + "个月" + day + "天" + hour + "点" + minute + "分前";
} else {
long year = ago / ONE_YEAR;
long month = ago % ONE_YEAR / ONE_MONTH;
long day = ago % ONE_YEAR % ONE_MONTH / ONE_DAY;
return year + "年前" + month + "月" + day + "天";
}
}
public static int getIntYear() {
return Calendar.getInstance().get(Calendar.YEAR);
}
public static int getIntMonth() {
return Calendar.getInstance().get(Calendar.MONTH);
}
public static String getYear() {
return Calendar.getInstance().get(Calendar.YEAR) + "";
}
public static String getMonth() {
int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
return month + "";
}
public static String getDay() {
return Calendar.getInstance().get(Calendar.DATE) + "";
}
public static String get24Hour() {
return Calendar.getInstance().get(Calendar.HOUR_OF_DAY) + "";
}
public static String getMinute() {
return Calendar.getInstance().get(Calendar.MINUTE) + "";
}
public static String getSecond() {
return Calendar.getInstance().get(Calendar.SECOND) + "";
}
/**
* 获取当前天开始时间
*
* @param date yyyy-MM-dd
* @return
*/
public static String getBeginDay(Date date) {
String format = format(date, "yyyy-MM-dd");
return format + " 00:00:00";
}
/**
* 获取当前天最后时间
*
* @param date yyyy-MM-dd
* @return
*/
public static String getEndDay(Date date) {
String format = format(date, "yyyy-MM-dd");
return format + " 23:59:59";
}
/**
* 获取当前天开始时间
*
* @param date yyyy-MM-dd
* @return
*/
public static String getBeginDay(String date) {
checkDay(date);
return date + " 00:00:00";
}
/**
* 获取当前天最后时间
*
* @param date yyyy-MM-dd
* @return
*/
public static String getEndDay(String date) {
checkDay(date);
return date + " 23:59:59";
}
/**
* 获取当前月开始时间
*
* @param date yyyy-MM
* @return
*/
public static String getBeginMonth(String date) {
checkMonth(date);
String[] split = date.split("-");
int year = Integer.parseInt(split[0]);
int month = Integer.parseInt(split[1]);
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate localDate = yearMonth.atDay(1);
LocalDateTime startOfDay = localDate.atStartOfDay();
ZonedDateTime zonedDateTime = startOfDay.atZone(ZoneId.of("Asia/Shanghai"));
return format(Date.from(zonedDateTime.toInstant()), "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当前月开始时间
*
* @param date yyyy-MM
* @return
*/
public static String getBeginMonth(Date date) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM");
String formatDate = simple.format(date);
return getBeginMonth(formatDate);
}
/**
* 获取当前月结束时间
*
* @param date yyyy-MM
* @return
*/
public static String getEndMonth(String date) {
checkMonth(date);
String[] split = date.split("-");
int year = Integer.parseInt(split[0]);
int month = Integer.parseInt(split[1]);
YearMonth yearMonth = YearMonth.of(year, month);
LocalDate endOfMonth = yearMonth.atEndOfMonth();
LocalDateTime localDateTime = endOfMonth.atTime(23, 59, 59, 999);
ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai"));
return format(Date.from(zonedDateTime.toInstant()), "yyyy-MM-dd HH:mm:ss");
}
/**
* 获取当前月结束时间
*
* @param date yyyy-MM
* @return
*/
public static String getEndMonth(Date date) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM");
String formatDate = simple.format(date);
return getEndMonth(formatDate);
}
/**
* 获取当前年开始时间
*
* @param date yyyy
* @return
*/
public static String getBeginYear(String date) {
checkYear(date);
checkYear(date);
return date + "-01-01 00:00:00";
}
/**
* 获取当前年开始时间
*
* @param date yyyy
* @return
*/
public static String getEedYear(String date) {
checkYear(date);
return date + "-12-31 23:59:59";
}
// public static void main(String[] args) {
// String day = "2000-2-29";
// String month = "2001-2";
// String year = "22000";
// System.out.println(DateUtil.getBeginDay(day));
// System.out.println(DateUtil.getEndDay(day));
//
// System.out.println("========================================");
//
// System.out.println(DateUtil.getBeginDay(new Date()));
// System.out.println(DateUtil.getEndDay(new Date()));
//
// System.out.println("========================================");
// System.out.println(DateUtil.getBeginMonth(month));
// System.out.println(DateUtil.getEndMonth(month));
//
// System.out.println("========================================");
// System.out.println(DateUtil.getBeginYear(year));
// System.out.println(DateUtil.getEenYear(year));
// }
/**
* 检查日
*
* @param day
*/
public static void checkDay(String day) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
try {
simple.setLenient(false);
simple.parse(day);
} catch (ParseException e) {
throw new RuntimeException("日期格式错误", e);
}
}
/**
* 检查月
*
* @param month
*/
public static void checkMonth(String month) {
SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM");
try {
simple.setLenient(false);
simple.parse(month);
} catch (ParseException e) {
throw new RuntimeException("日期格式错误", e);
}
}
/**
* 检查年
*
* @param year
*/
public static void checkYear(String year) {
int yearInt = Integer.parseInt(year);
if (yearInt > 3000 || yearInt < 1000) {
throw new RuntimeException("日期格式错误");
}
}
/**
* 将yyyyMMddHHmmss 格式的时间字符串转为时间
*
* @param str
*/
public static Date dateStrToDate(String str) {
DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
LocalDateTime localDateTime = LocalDateTime.parse(str, f);
Date date = Date.from(localDateTime.minusDays(7).toInstant(ZoneOffset.ofHours(8)));
return date;
}
//string转date
public static void stringToDate(){
try{
String str="2016-10-24 21:59:06";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.parse(str));
System.out.println(sdf.parse(str).getTime());
}catch(Exception e){
e.printStackTrace();
}
}
/**
* String转化Date格式
* @param date
* @param type
* @return
*/
public static Date parse(String date,String type){
SimpleDateFormat formatter = new SimpleDateFormat(type);
ParsePosition pos = new ParsePosition(0);
Date strtodate = formatter.parse(date, pos);
return strtodate;
}
public static Date getNow() {
Calendar cal = Calendar.getInstance();
Date currDate = cal.getTime();
return currDate;
}
//
// public static Date addDays(Date date, Integer days) {
// Calendar calendar=Calendar.getInstance();
// calendar.setTime(date);
// calendar.add(Calendar.DATE, days);
// return calendar.getTime();
// }
//
// public static Date addMonth(Date date, Integer month) {
// Calendar calendar=Calendar.getInstance();
// calendar.setTime(date);
// calendar.add(Calendar.MONTH, month);
// return calendar.getTime();
// }
/**
* 时间格式处理
* @param date
* @param f
* @return
*/
public static String dateStr(Date date, String f) {
if (date == null) {
return "";
}
SimpleDateFormat format = new SimpleDateFormat(f);
String str = format.format(date);
return str;
}
/**
* 计算时间差,单位分
* @param date1
* @param date2
* @return
*/
public static int minuteBetween(Date date1, Date date2){
DateFormat sdf=new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
Calendar cal = Calendar.getInstance();
try {
Date d1 = sdf.parse(DateUtils.dateStr(date1,yyyy_MM_dd_HH_mm_ss));
Date d2 = sdf.parse(DateUtils.dateStr(date2,yyyy_MM_dd_HH_mm_ss));
cal.setTime(d1);
long time1 = cal.getTimeInMillis();
cal.setTime(d2);
long time2 = cal.getTimeInMillis();
return Integer.parseInt(String.valueOf((time2 - time1) / 60000));
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取指定时间天的开始时间
*
* @param date
* @return
*/
public static Date getDayStartTime(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(date.getTime());
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DATE), 0, 0, 0);
return cal.getTime();
}
/**
* 天数差
* @param date1
* @param date2
* @return
*/
public static int dayBetween(Date date1, Date date2){
DateFormat sdf=new SimpleDateFormat(yyyy_MM_dd_HH_mm_ss);
Calendar cal = Calendar.getInstance();
try {
Date d1 = sdf.parse(DateUtils.dateStr(date1,yyyy_MM_dd_HH_mm_ss));
Date d2 = sdf.parse(DateUtils.dateStr(date2,yyyy_MM_dd_HH_mm_ss));
cal.setTime(d1);
long time1 = cal.getTimeInMillis();
cal.setTime(d2);
long time2 = cal.getTimeInMillis();
return Integer.parseInt(String.valueOf((time2 - time1) / (24 * 60 * 60000)));
} catch (ParseException e) {
e.printStackTrace();
}
return 0;
}
/**
* 处理时间
* @param date
* @param now
* @return
*/
public static String getJyTime(Date date,Date now) {
if(date == null){
return "";
}
int m = minuteBetween(date,now);
Date dateStart = getDayStartTime(date);
Date nowStart = getDayStartTime(now);
int day = dayBetween(dateStart,nowStart);
if(m < 10){
return "刚刚";
}else if (m < 60){
return m +"分钟前";
}else if (day == 0){
return "今天";
}else if (day == 1){
return "昨天";
}else if (day <= 30){
return day+"天前";
}else if (date.getYear() == now.getYear()){
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd");
String dateString = formatter.format(date);
return dateString;
}else{
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(date);
return dateString;
}
}
public static int getTimeMonth(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(2) + 1;
}
public static int getTimeDay(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(5);
}
public static int getTimeYear(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal.get(1);
}
/**
* 获取当前日期,指定格式
* 描述:<描述函数实现的功能>.
*
* @return
*/
public static String nowDateTime(String DATE_FORMAT) {
SimpleDateFormat dft = new SimpleDateFormat(DATE_FORMAT);
return dft.format(new Date());
}
/**
* 指定日期加上天数后的日期
*
* @param num 为增加的天数
* @param newDate 创建时间
* @return
*/
public static final String addDay(String newDate, int num, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
try {
Date currdate = format.parse(newDate);
Calendar ca = Calendar.getInstance();
ca.setTime(currdate);
ca.add(Calendar.DATE, num);
return format.format(ca.getTime());
} catch (ParseException e) {
log.error("转化时间出错,", e);
return null;
}
}
/**
* 指定日期加上天数后的日期
*
* @param num 为增加的天数
* @param newDate 创建时间
* @return
*/
public static final String addDay(Date newDate, int num, String pattern) {
SimpleDateFormat format = new SimpleDateFormat(pattern);
Calendar ca = Calendar.getInstance();
ca.setTime(newDate);
ca.add(Calendar.DATE, num);
return format.format(ca.getTime());
}
/** 获得本周第一天:yyyy-MM-dd HH:mm:ss */
public static String getWeekStartDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.WEEK_OF_MONTH, 0);
c.set(Calendar.DAY_OF_WEEK, 2);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
return startSdf.format(c.getTime());
}
/** 获得本周最后一天:yyyy-MM-dd HH:mm:ss */
public static String getWeekEndDay() {
return addDay(getWeekStartDay(), 7, DATE_FORMAT);
}
/** 获得上周第一天:yyyy-MM-dd HH:mm:ss */
public static String getLastWeekStartDay() {
return addDay(getWeekStartDay(), -7, DATE_FORMAT);
}
/** 获得上周最后一天:yyyy-MM-dd HH:mm:ss */
public static String getLastWeekEndDay() {
return addDay(getLastWeekStartDay(), 7, DATE_FORMAT);
}
/** 获得本月最后一天:yyyy-MM-dd HH:mm:ss */
public static String getMonthEndDay() {
Calendar c = Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
return endSdf.format(c.getTime());
}
/** 获得上月第一天:yyyy-MM-dd HH:mm:ss */
public static String getLastMonthStartDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH, 1);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-MM-01 00:00:00");
return startSdf.format(c.getTime());
}
/** 获得上月最后一天:yyyy-MM-dd HH:mm:ss */
public static String getLastMonthEndDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.MONTH, -1);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
return endSdf.format(c.getTime());
}
/** 获得上年第一天:yyyy-MM-dd HH:mm:ss */
public static String getLastYearStartDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
SimpleDateFormat startSdf = new SimpleDateFormat("yyyy-01-01 00:00:00");
return startSdf.format(c.getTime());
}
/** 获得上年最后一天:yyyy-MM-dd HH:mm:ss */
public static String getLastYearEndDay() {
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -1);
SimpleDateFormat endSdf = new SimpleDateFormat("yyyy-12-31 23:59:59");
return endSdf.format(c.getTime());
}
/**
* 获取指定日期指定格式字符串
*
* @param dateStr
* @param DATE_FORMAT
* @return
* @throws ParseException
*/
public static String appointedDayStrToFormatStr(String dateStr, String STR_DATE_FORMAT, String DATE_FORMAT) {
Date date = DateUtils.strToDate(dateStr, STR_DATE_FORMAT);
return DateUtils.dateToStr(date, DATE_FORMAT);
}
/**
* convert a date to string in a specifies fromat.
*
* @param date
* @param DATE_FORMAT
* @return
*/
public static String dateToStr(Date date, String DATE_FORMAT) {
if (date == null) {
return null;
}
SimpleDateFormat myFormat = new SimpleDateFormat(DATE_FORMAT);
return myFormat.format(date);
}
/**
* 获取当前年,指定格式
* 描述:<描述函数实现的功能>.
*
* @return
*/
public static String nowYear() {
Calendar cal = Calendar.getInstance();
return cal.get(Calendar.YEAR) + "";
}
/**
* 获取上一年,指定格式
* 描述:<描述函数实现的功能>.
*
* @return
*/
public static String lastYear() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -1);
return cal.get(Calendar.YEAR) + "";
}
public static void main(String[] args) {
stringToDate();
}
}
标签:return,String,public,yyyy,日期,static,Calendar,工具 From: https://www.cnblogs.com/cjxiaozhuang/p/17475024.html