java日期处理工具类
本人日常开发中整理的日期处理工具类,不说废话 直接上代码.
package com.hknc.marketapp.utils;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @Auther: tj
* @Date: 2020/12/9 15:48
* @Description:
*/
public class GetDateUtil {
/**
* 查询今天礼拜几
* @return
*/
public static String getWeek() {
String week = "";
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
if (w == 1) {
week = "1";
} else if (w == 2) {
week = "2";
} else if (w == 3) {
week = "3";
} else if (w == 4) {
week = "4";
} else if (w == 5) {
week = "5";
} else if (w == 6) {
week = "6";
} else if (w == 0) {
week = "7";
}
return week;
}
/**
* 获取今天结束12点
*
* @return Data
*/
public static Date getTodayEnd() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
Calendar thisTime = Calendar.getInstance();
Date time = thisTime.getTime();
String str = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(str);
return date;
}
/**
* 获取今天开始凌晨
*
* @return Data
*/
public static Date getTodayStart() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), 0, 0, 0);
Calendar thisTime = Calendar.getInstance();
Date time = thisTime.getTime();
String str = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(str);
return date;
}
/**
* 得到指定日期的一天的的最后时刻23:59:59
*
* @param date
* @return
*/
public static Date getFinallyDate(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format1 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = format.format(date);
temp += " 23:59:59";
try {
return format1.parse(temp);
} catch (Exception e) {
return null;
}
}
/**
* 得到指定日期的一天的开始时刻00:00:00
*
* @param date
* @return
*/
public static Date getStartDate(Date date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat format1 = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String temp = format.format(date);
temp += " 00:00:00";
try {
return format1.parse(temp);
} catch (Exception e) {
return null;
}
}
/**
* 获取本周的第一天
*
* @return String
**/
public static String getWeekStart() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.WEEK_OF_MONTH, 0);
cal.set(Calendar.DAY_OF_WEEK, 2);
Date time = cal.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
}
/**
* String 转LongDate
* @param time
* @return
*/
public static Date stringToLongDate(String time) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dateTime = null;
try {
dateTime = simpleDateFormat.parse(time);
} catch (ParseException e) {
e.printStackTrace();
}
return dateTime;
}
/**
* 日期转时间戳
* @param date
* @return
*/
public static Long dataToTime(Date date) {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"HH:mm:ss");
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
//取到日期的时分秒
String time = sdf.format(date);
//当天日期的 年月日
String day = sdf2.format(new Date());
//组成一个新的日期字符串
String newday = day +" "+ time;
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = null;
try {
date1 = sdf3.parse(newday);
}catch (Exception e){
}
//返回时间戳
return date1.getTime();
}
/**
* 获取某天的24小时
* @param today
* @return
*/
public static List<String> day24String(String today){
List<String> list = new ArrayList<>();
StringBuilder stringBuilder = new StringBuilder();
list.add(today+" 00:00:00 ");
list.add(today+" 00:59:59");
for (Integer i = 1; i <=23; i++){
String iStr = i.toString();
if (i<10){
iStr = "0"+ i;
}
list.add(today +" "+ iStr+":59:59 ");
}
return list;
}
/**
* Stinrg 转 date
*
* @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;
}
/**
* Stinrg 转 date
*
* @param strDate
* @return
*/
public static Date strToDateYdm(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;
}
/**
* date 转String
*
* @param dateDate
* @return
*/
public static String dateToStr(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateString = formatter.format(dateDate);
return dateString;
}
/**
* date 转String
*
* @param dateDate
* @return
*/
public static String dateToStrLong(java.util.Date dateDate) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String dateString = formatter.format(dateDate);
return dateString;
}
public static Long dateTime(Date date) throws ParseException {
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"HH:mm:ss");
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String time = sdf.format(date);
String day = sdf2.format(new Date());
String newday = day +" "+ time;
System.out.println(time);
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdf3.parse(newday);
System.out.println("字符串==="+date1);
System.out.println(date1.getTime());
System.out.println(new Date().getTime());
return date1.getTime();
}
/**
* 日期转成年月日
* @param date
* @return
*/
public static Date ymdDate(Date date){
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String d = sdf2.format(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
try {
date1 = sdf.parse(d);
}catch (Exception e){
}
return date1;
}
/**
* 获取当天12点的时间戳
* @return
*/
public static Long get12Hour(){
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String format = sdf.format(new Date());
String time = format +" "+ "12:00:00";
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = null;
try {
date1= sdf1.parse(time);
}catch (Exception e){
}
return date1.getTime();
}
/**
* 获取本月开始日期
*
* @return String
**/
public static Date getMonthStart() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MONTH, 0);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date time = cal.getTime();
String str = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(str);
return date;
}
/**
* 获取本月最后一天
*
* @return String
**/
public static Date getMonthEnd() throws ParseException {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
Date time = cal.getTime();
String str = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(str);
return date;
}
/**
* 获取本年的第一天
*
* @return String
**/
public static String getYearStart() {
return new SimpleDateFormat("yyyy").format(new Date()) + "-01-01";
}
public static Date getYearStartDate() throws ParseException {
String yearStart = getYearStart() + " 00:00:00";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTiem = simpleDateFormat.parse(yearStart);
return startTiem;
}
/**
* 获取本年的最后一天
*
* @return String
**/
public static String getYearEnd() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, calendar.getActualMaximum(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
Date currYearLast = calendar.getTime();
return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast) + " 23:59:59";
}
public static Date getYearEndDate() throws ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String yearEnd = getYearEnd();
Date endTiem = simpleDateFormat.parse(yearEnd);
return endTiem;
}
/**
* date转localdate
* @param date
* @return
*/
public static LocalDate dateToLocalDate(Date date){
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return localDate;
}
/**
* localDate 转date
* @param localDate
* @return
*/
public static Date localDate2Date(LocalDate localDate) {
if(null == localDate) {
return null;
}
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
return Date.from(zonedDateTime.toInstant());
}
/**
* 开始时间到结束时间 按天遍历
* @param start
* @param end
* @return
*/
public static List<LocalDate> collectLocalDates(LocalDate start,LocalDate end){
return Stream.iterate(start, localDate -> localDate.plusDays(1))
.limit(ChronoUnit.DAYS.between(start, end) + 1)
.collect(Collectors.toList());
};
public static void main(String[] args) throws ParseException {
String s = "2021-01-01 08:30:00";
String s1 = "2021-01-07 17:31:59";
java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String format = sdf2.format(new Date());
String s3 = format +" "+ "12:00:00";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse(s);
// Date date1 = sdf.parse(s3);
// System.out.println(date.getTime());
// System.out.println(date1.getTime()+"===");
// dateTime(date);
Date parse = sdf.parse(s1);
LocalDate start = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate end = parse.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
List<LocalDate> localDates = collectLocalDates(start, end);
for (LocalDate localDate : localDates) {
System.out.println(localDate);
}
}
}
安利一个小的Java工具类库 Hutool 包,里面有很多封装好的工具类,非常好用, 直接上链接
https://www.hutool.cn/docs/#/