/**
* 取得当前的日期时间字符串YYYYMMDD24HHMISS
*
* @return String 取得当前的日期时间字符串YYYYMMDD24HHMISS
*/
public static String getDateTime24String() {
String format = "yyyyMMddHHmmss";
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date());
}
/**
* 取得指定时间日期时间字符串
* @param date yyyy-MM-dd HH:mm:ss格式
*
* @return String yyyyMMdd24HHmmss 格式
*/
public static String getDateTimeChangeString(String date) throws ParseException {
SimpleDateFormat sdfBefore = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date1 = sdfBefore.parse(date);
SimpleDateFormat sdfAfter = new SimpleDateFormat("yyyyMMddHHmmss");
return sdfAfter.format(date1);
}
debug时在idea上没有进入相应的代码位置,在编译好的代码上打断点,显示的是一把小叉,代码不会跳到相应的代码位置上。
解决的办法:把代码重新编译,不行的话可能的原因有jar包版本的更新后旧的jar包版本删除,导致代码执行的是原来的jar包。在编译后的代码上删除旧版本的jar即可解决。
2023-03-07 21:45:55 start:
DateUtil工具类:
package xyz.haijin.weblog.common.util;
import xyz.haijin.constant.CommonConstant;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* @author haijin
* @Date 2021/7/21
*/
public class DateUtil {
/**
* 文本转换成时间
*
* @param text 时间串
* @param formatPattern 转化的格式
*/
public static Date parse(String text, String formatPattern) {
if (text == null || "".equals(text.trim())) {
return null;
}
SimpleDateFormat f = new SimpleDateFormat(formatPattern);
try {
return f.parse(text);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 取得指定时间日期时间字符串
* @param date yyyy-MM-dd HH:mm:ss格式
*
* @return String yyyyMMdd24HHmmss 格式
*/
public static String getDateTimeYmdhmsChangeString(String date,String format) throws ParseException {
SimpleDateFormat sdfBefore = new SimpleDateFormat(CommonConstant.YMDHMS);
Date date1 = sdfBefore.parse(date);
SimpleDateFormat sdfAfter = new SimpleDateFormat(format);
return sdfAfter.format(date1);
}
/**
* 将日期类型转换为字符串 默认格式为yyyy-MM-dd HH:mm:ss
* @param date 日期类型
* @return 日期字符串
*/
public static String getDateDefaultFormat(Date date) {
return getDateByFormat(date,CommonConstant.YMDHMS);
}
/**
* 根据格式生成指定的日期格式的字符串
* @param date 日期对象
* @param format 日期格式
* @return 日期字符串
*/
public static String getDateByFormat(Date date,String format) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.format(date);
}
/**
* 文本转换成时间
*
* @param text 时间文本字符串
*/
public static Date parse(String text) {
return parse(text,CommonConstant.YMDHMS);
}
/**
* 文本转换成时间戳
*
* @param time 时间戳
* @param formatPattern 转换格式
*/
public static Date date(Long time, String formatPattern) {
SimpleDateFormat f = new SimpleDateFormat(formatPattern);
String d = f.format(time);
try {
return f.parse(d);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 文本转换成时间戳
*
* @param time 时间戳
*/
public static Date date(Long time) {
return date(time,CommonConstant.YMDHMS);
}
/**
* 获取当前时间
* @return ignore
*/
public static String now(String formatPattern) {
Date date = new Date();
SimpleDateFormat f = new SimpleDateFormat(formatPattern);
return f.format(date);
}
/**
* 获取当前时间
* @return ignore
*/
public static String now() {
return now(CommonConstant.YMDHMS);
}
/**
* 获取当前时间对象
* @return ignore
*/
public static Date nowDate() {
return new Date();
}
/**
* 获取当天日期的最后一秒
* @return ignore
*/
public static Date getCurrentDateEnd() {
return getCurrentDateEnd(nowDate());
}
/**
* 将传入的日期转化为yyyy-MM-dd 23:59:59这种数据
* @param currentDate 传入的日期
* @return ignore
*/
public static Date getCurrentDateEnd(Date currentDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 获取当前日期的下一天
* @param currentDate 当前日期
* @return ignore
*/
public static Date getNextDate(Date currentDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
// 将日期增加一天
calendar.add(Calendar.DAY_OF_YEAR, 1);
return calendar.getTime();
}
/**
* 获取当前日期的上一天
* @param currentDate 当前日期
* @return ignore
*/
public static Date getLastDate(Date currentDate) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(currentDate);
// 将日期增加一天
calendar.add(Calendar.DAY_OF_YEAR, -1);
return calendar.getTime();
}
}
常量CommonConstant:
package xyz.haijin.constant;
/**
* 公共的常量
* @author: haijin
* @Date: 2022/07/16 20:38
*/
public interface CommonConstant {
/**
* yyyy-MM-dd HH:mm:ss
*/
public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
/**
* yyyy-MM-dd
*/
public static final String YMD = "yyyy-MM-dd";
/**
* yyyyMMdd
*/
public static final String YMD1 = "yyyyMMdd";
}
end