java中时间的转换工具类
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author 小袁兄弟
* @Date: 2021/1/2 15:29
* @QQ:1758906065
* @Aphorism:
* 苦心人天不负,自助者天助!
* 实践是检验真理的唯一标准。
*/
/**
* 时间转换的工具
*/
public class DateUtil {
/**
* 自定义格式转换工具
* @param date 时间
* @param format 格式
* @return 转换后的字符串
*/
public static String formatDate(Date date, String format){
String result = "";
SimpleDateFormat sdf = new SimpleDateFormat(format);
if(date!=null){
result = sdf.format( date );
}
return result;
}
/**
* 将String转换成Date类型
* @param str 时间
* @param format 格式
* @return
* @throws ParseException
*/
public static Date formatString(String str,String format) throws ParseException {
if(StringUtil.isEmpty( str )){
return null;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.parse( str );
}
/**
* String类型转成自己想要的格式
* @param str
* @param format
* @return
* @throws ParseException
*/
public static String StringDateString(String str,String format) throws ParseException {
if(StringUtil.isEmpty( str )){
return null;
}
// 这里将String转换成Date类型
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date date = sdf.parse(str);
String result = "";
// 这里讲Date转换成String类型
SimpleDateFormat sdf1 = new SimpleDateFormat(format);
if(date!=null){
result = sdf1.format( date );
}
return result;
}
/**
* 转换成 yyyy MM dd hh:mm:ss 格式
* @return
*/
public static String getCurrentDateStr(){
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy MM dd hh:mm:ss");
return sdf.format( date );
}
}
直接将该方法定义在项目当中,在要使用的地方,直接引用就可以了,该工具主要用在前后端交互时时间的转换
下面是一个判断字符串的工具类
/**
* @Author 小袁兄弟
* @Date: 2021/1/2 15:29
* @QQ:1758906065
* @Aphorism:
* 苦心人天不负,自助者天助!
* 实践是检验真理的唯一标准。
*/
public class StringUtil {
/**
* 判断是否是空
* @param str 传入的参数
* @return true:为空 false:不为空
*/
public static boolean isEmpty(String str){
if("".equals( str ) || str == null){
return true;
}else {
return false;
}
}
/**
* 判断是否不是空
* @param str
* @return @return true:不为空 false:为空
*/
public static boolean isNotEmpty(String str){
if(!"".equals( str ) && str!=null){
return true;
}else {
return false;
}
}
}