Java日期
主要是对日期进行相关操作,使用到的函数是Calendar,SimpleDateFormat(非线程安全,尽量避免使用)
对日期操作:Calendar
日期转换:FastDateFormat
/*实现提取前一天的日期*/
public class Main {
public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
public static String dateToString(Date date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
}
public static Date stringToDate(String date, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
try {
return sdf.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String dateSubOneDate(String date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(stringToDate(date, DATE_FORMAT_YYYY_MM_DD));
calendar.add(Calendar.DATE, -1);//当前时间减去一天,即一天前的时间
return dateToString(calendar.getTime(), DATE_FORMAT_YYYY_MM_DD);
}
public static void main(String[] args) throws ParseException {
System.out.println(dateSubOneDate(dateToString(new Date(), DATE_FORMAT_YYYY_MM_DD)));
}
}
/*得到今天的起始和结束日期*/
public static Date getTodayStartTime(Date date) {
Calendar todayStart = Calendar.getInstance();
todayStart.setTime(date);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime();
}
public static Date getTodayEndTime(Date date) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTime(date);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime();
}
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
public class DateUtils {
private static final Logger log = LoggerFactory.getLogger(DateUtils.class);
public static Date getTodayStartTime(Date date) {
Calendar todayStart = Calendar.getInstance();
todayStart.setTime(date);
todayStart.set(Calendar.HOUR_OF_DAY, 0);
todayStart.set(Calendar.MINUTE, 0);
todayStart.set(Calendar.SECOND, 0);
todayStart.set(Calendar.MILLISECOND, 0);
return todayStart.getTime();
}
public static Date getTodayEndTime(Date date) {
Calendar todayEnd = Calendar.getInstance();
todayEnd.setTime(date);
todayEnd.set(Calendar.HOUR_OF_DAY, 23);
todayEnd.set(Calendar.MINUTE, 59);
todayEnd.set(Calendar.SECOND, 59);
todayEnd.set(Calendar.MILLISECOND, 999);
return todayEnd.getTime();
}
public static Date addDay(Date date, int day) {
Calendar today = Calendar.getInstance();
today.setTime(date);
today.add(Calendar.DAY_OF_MONTH, day);
return today.getTime();
}
public static Date parseStrToDate(String dateStr) {
Date date;
try {
date = org.apache.commons.lang3.time.DateUtils.parseDate(dateStr, "yyyyMMdd");
} catch (ParseException e) {
return null;
}
return date;
}
// public static String formatDateToStr(Date date) {
// FastDateFormat fdf = FastDateFormat.getInstance("yyyy年MM月dd日 HH:mm:ss");
// return fdf.format(date);
// }
public static String formatDateToStr1(Date date) {
FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMdd");
return fdf.format(date);
}
public static String formatDateToStr2(Date date) {
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
return fdf.format(date);
}
public static String formatDateToStr3(Date date) {
FastDateFormat fdf = FastDateFormat.getInstance("yyyy年MM月dd日");
return fdf.format(date);
}
public static String formatDateToStr4(long time) {
FastDateFormat fdf = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
return fdf.format(time);
}
private static String testDate(String remark) {
if (!StringUtils.isEmpty(remark) && remark.contains("_bill_")) {
String tr = remark.replace("|", "");
return tr.substring(tr.indexOf("B") + 1, tr.indexOf("_"));
}
return "";
}
public static String convertDate(String dateString) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
String dateTime = null;
try {
LocalDateTime ldt = LocalDateTime.parse(dateString, dtf);
DateTimeFormatter fa = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
dateTime = ldt.format(fa);
} catch (Exception e) {
return dateString;
}
return dateTime;
}
public static String getDateStr(String dateStr) {
if (StringUtils.isEmpty(dateStr)) {
Date currentDate = new Date();
Date date = new Date(currentDate.getTime() - 24 * 60 * 60 * 1000L);
dateStr = DateFormatUtils.format(date, "yyyyMMdd");
}
return dateStr;
}
}
import org.junit.Test;
import java.util.Date;
import static org.junit.Assert.*;
/**
* @author v-huyuqiao
* @desc TODO
* @create 2020/8/10 16:13
**/
public class DateUtilsTest {
@Test
public void allDateTest(){
System.out.println(DateUtils.getTodayStartTime(new Date())); //20200810
System.out.println(DateUtils.getTodayEndTime(new Date())); // Mon Aug 10 23:59:59 CST 2020
System.out.println(DateUtils.addDay(new Date(), 1)); //Tue Aug 11 16:08:03 CST 2020
System.out.println(DateUtils.formatDateToStr1(new Date())); //20200810
System.out.println(DateUtils.formatDateToStr2(new Date())); //2020-08-10 16:08:03
System.out.println(DateUtils.formatDateToStr3(new Date())); //2020年08月10日
System.out.println(DateUtils.formatDateToStr4(System.currentTimeMillis())); //时间戳变成时间:2020-08-10 16:08:03
System.out.println(DateUtils.convertDate("2020-08-10 00:00:00")); //2020-08-10 00:00:00
System.out.println(DateUtils.getDateStr(null)); //20200809
}
}