import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
/**
* 时间工具类
*
* @time 2020年5月11日 下午3:55:18
*/
public class DateUtil {
/**
* YYYY_MM_ddHHmmss字符串转 YYYYMMdd
*
* @param Str
* @return
*/
public static String getYYYYMMddStr(String Str) {
String needStr = "";
try {
Date date = getDateYYYY_MM_ddHHmmss(Str);
needStr = getStrYYYYMMdd(date);
} catch (Exception e) {
// TODO: handle exception
}
return needStr;
}
/**
* YYYYMMdd字符串转 YYYY-MM-dd
*
* @param Str
* @return
*/
public static String getYYYY_MM_ddStr(String Str) {
String needStr = "";
try {
Date date = getDateYYYYMMdd(Str);
needStr = getDateStr(date, "yyy-MM-dd");
} catch (Exception e) {
// TODO: handle exception
}
return needStr;
}
/**
* YYYYMMdd字符串转 月份
*
* @param Str
* @return
*/
public static String getMonthStr(String Str) {
String[] months = {"一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月"};
String needStr = "";
try {
Date date = getDateYYYYMMdd(Str);
needStr = getDateStr(date, "yyy-MM-dd");
} catch (Exception e) {
// TODO: handle exception
}
Integer monthIndex = Integer.parseInt(needStr.split("-")[1]) - 1;
return months[monthIndex];
}
public static String getDateStr(Date date, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
public static String getStrYYYYMMdd(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.format(date);
}
public static String getStrYYYYMMddHHmmss(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return df.format(date);
}
public static String getStrYYYY_MM_ddHHmmss(Date date) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
public static String getCurrentDateTime() {
Date date = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
public static Date getDateYYYYMMdd(String str) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
return df.parse(str);
}
public static Date getDateYYYYMMddHHmmss(String str) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
return df.parse(str);
}
public static Date getDateYYYY_MM_ddHHmmss(String str) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.parse(str);
}
public static Date getDate(Date date) throws ParseException {
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sDate = sdf2.format(date);
//Date date1 = getDateYYYY_MM_ddHHmmss(sDate);
Date date1 = Timestamp.valueOf(sDate);
return date1;
}
public static String getFetureDate(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");
String result = format.format(today);
return result;
}
public static ArrayList<String> getFetureDateAll(int intervals) {
ArrayList<String> fetureDaysList = new ArrayList<>();
for (int i = 0; i < intervals; i++) {
fetureDaysList.add(getFetureDate(i));
}
return fetureDaysList;
}
public static String dateToWeek(String datetime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance();
Date date;
try {
date = sdf.parse(datetime);
cal.setTime(date);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
return weekDays[w];
}
public static String getOffsetStr(String format, Integer offsetDay) {
//获取多少天之前的
Calendar calendar1 = Calendar.getInstance();
SimpleDateFormat sdf1 = new SimpleDateFormat(format);
calendar1.add(Calendar.DATE, offsetDay);
String agoDate = sdf1.format(calendar1.getTime());
return agoDate;
}
/**
* 获得当天0点时间
*
* @return
*/
public static Date 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 cal.getTime();
}
public static String getTimesMorningStr() {
Date date = getTimesMorning();
return getStrYYYY_MM_ddHHmmss(date);
}
/**
* 获得本周一0点时间
*
* @return
*/
public static Date getTimesWeekMorning() {
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_WEEK, Calendar.MONDAY);
return cal.getTime();
}
public static String getTimesWeekMorningStr() {
Date date = getTimesWeekMorning();
return getStrYYYY_MM_ddHHmmss(date);
}
/**
* 获得本月第一天0点时间
*
* @return
*/
public static Date getTimesMonthMorning() {
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));
return cal.getTime();
}
public static String getTimesMonthMorningStr() {
Date date = getTimesMonthMorning();
return getStrYYYY_MM_ddHHmmss(date);
}
/**
* 获取本季度零点
*
* @return
*/
public static Date getCurrentQuarterStartTime() {
Calendar c = Calendar.getInstance();
int currentMonth = c.get(Calendar.MONTH) + 1;
SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = null;
try {
if (currentMonth >= 1 && currentMonth <= 3)
c.set(Calendar.MONTH, 0);
else if (currentMonth >= 4 && currentMonth <= 6)
c.set(Calendar.MONTH, 3);
else if (currentMonth >= 7 && currentMonth <= 9)
c.set(Calendar.MONTH, 4);
else if (currentMonth >= 10 && currentMonth <= 12)
c.set(Calendar.MONTH, 9);
c.set(Calendar.DATE, 1);
now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
} catch (Exception e) {
e.printStackTrace();
}
return now;
}
public static String getCurrentQuarterStartTimeStr() {
Date date = getStartDayOfQuarter();
return getStrYYYY_MM_ddHHmmss(date);
}
/**
* 本年度开始时间
*
* @return
*/
public static Date getCurrentYearStartTime() {
Calendar currCal = Calendar.getInstance();
int currentYear = currCal.get(Calendar.YEAR);
return getYearFirst(currentYear);
}
public static String getCurrentYearStartTimeStr() {
Date date = getCurrentYearStartTime();
return getStrYYYY_MM_ddHHmmss(date);
}
/**
* 获取某年第一天日期
*
* @param year 年份
* @return Date
*/
public static Date getYearFirst(int year) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, year);
Date currYearFirst = calendar.getTime();
return currYearFirst;
}
/**
* 获取某季度的第一天
* @return
*/
public static Date getStartDayOfQuarter() {
LocalDate resDate = LocalDate.now();
Month month = resDate.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
resDate = LocalDate.of(resDate.getYear(), firstMonthOfQuarter, 1);
return Date.from(resDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
}
public static void main(String[] args) {
String result = getCurrentQuarterStartTimeStr();
System.out.println(result);
}
}
还没有评论,来说两句吧...