1、該類的作用是將格式爲["","",""......]的字符串轉換爲數組,或將數組轉換成格式爲["","",""......]的字符串
import com.clfps.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
*
* * @projectName **-parent
* * @title StingTransfersBetweenArryUtils
* * @package com.**.util
* * @description 該類的作用是將格式爲["","",""......]的字符串轉換爲數組,或將數組轉換成格式爲["","",""......]的字符串
* * @author IT_CREAT
* * @date
* * @version V1.0.0
*
*/
public class StringBetweenArryUtils {
public static List<String> string2ArryList(String s){
if(StringUtils.isEmpty(s)){
return null;
}
String s1 = s.substring(1,s.length()-1);
String[] sArry = s1.split(",");
List<String> strList = new ArrayList<>();
for(String str : sArry){
String str2 = str.substring(1,str.length()-1);
strList.add(str2);
}
return strList;
}
public static String[] string2Arry(String s){
if(StringUtils.isEmpty(s)){
return null;
}
List<String> list = string2ArryList(s);
String[] strs = new String[list.size()];
list.toArray(strs);
return strs;
}
public static String arry2String(String[] strs){
if(null == strs || strs.length ==0){
return null;
}
String str = "[";
int sign = 0;
for(String s:strs){
if(sign < strs.length -1){
str = str + "\""+ s+ "\"" + ",";
}else{
str = str + "\""+ s + "\"";
}
sign++;
}
str = str +"]";
return str;
}
public static String arryList2String(List<String> strList){
if(null == strList || strList.size() ==0){
return null;
}
String str = "[";
int sign = 0;
for(String s:strList){
if(sign < strList.size() -1){
str = str + "\""+ s+ "\"" + ",";
}else{
str = str + "\""+ s + "\"";
}
sign++;
}
str = str +"]";
return str;
}
public static void main(String[] args) {
String regex = "['123','','123']";
String[] str = string2Arry(regex);
System.out.println(str.length);
for(String str1:str){
System.out.println(str1);
}
System.out.println(arry2String(str));
System.out.println(arryList2String(java.util.Arrays.asList(str)));
}
}
2、駝峯命名與sql下劃線字段之間的轉換工具
/**
*
* * @projectName **-parent
* * @title HumpNamedTools
* * @package com.**.utils
* * @description 駝峯命名與sql下劃線字段之間的轉換工具
* * @author IT_CREAT
* * @date
* * @version V1.0.0
*
*/
public class HumpNamedTools {
/**
* 將駝峯式命名的字符串轉換爲下劃線小寫方式。如果轉換前的駝峯式命名的字符串爲空,則返回空字符串。</br>
* 例如:helloWorld->hello_word
* @param name 轉換前的駝峯式命名的字符串
* @return 轉換後下劃線小寫方式命名的字符串
*/
public static String hump2LowerColumnName(String name) {
StringBuilder result = new StringBuilder();
if (name != null && name.length() > 0) {
// 循環處理字符
for (int i = 0; i < name.length(); i++) {
String s = name.substring(i, i + 1);
// 在大寫字母前添加下劃線
if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
result.append("_");
}
// 其他字符直接轉成小寫
result.append(s.toLowerCase());
}
}
return result.toString();
}
/**
* 將駝峯式命名的字符串轉換爲下劃線大寫方式。如果轉換前的駝峯式命名的字符串爲空,則返回空字符串。</br>
* 例如:HelloWorld->HELLO_WORLD
* @param name 轉換前的駝峯式命名的字符串
* @return 轉換後下劃線大寫方式命名的字符串
*/
public static String hump2UpperColumnName(String name) {
StringBuilder result = new StringBuilder();
if (name != null && name.length() > 0) {
// 將第一個字符處理成大寫
result.append(name.substring(0, 1).toUpperCase());
// 循環處理其餘字符
for (int i = 1; i < name.length(); i++) {
String s = name.substring(i, i + 1);
// 在大寫字母前添加下劃線
if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
result.append("_");
}
// 其他字符直接轉成大寫
result.append(s.toUpperCase());
}
}
return result.toString();
}
/**
* 將下劃線大寫或小寫方式命名的字符串轉換爲駝峯式。如果轉換前的下劃線大寫方式命名的字符串爲空,則返回空字符串。</br>
* 例如:HELLO_WORLD->HelloWorld
* @param name 轉換前的下劃線大寫或小寫方式命名的字符串
* @return 轉換後的駝峯式命名的字符串
*/
public static String column2HumpName(String name) {
StringBuilder result = new StringBuilder();
// 快速檢查
if (name == null || name.isEmpty()) {
// 沒必要轉換
return "";
} else if (!name.contains("_")) {
// 不含下劃線,僅將首字母小寫
return name.substring(0, 1).toLowerCase() + name.substring(1);
}
// 用下劃線將原始字符串分割
String camels[] = name.split("_");
for (String camel : camels) {
// 跳過原始字符串中開頭、結尾的下換線或雙重下劃線
if (camel.isEmpty()) {
continue;
}
// 處理真正的駝峯片段
if (result.length() == 0) {
// 第一個駝峯片段,全部字母都小寫
result.append(camel.toLowerCase());
} else {
// 其他的駝峯片段,首字母大寫
result.append(camel.substring(0, 1).toUpperCase());
result.append(camel.substring(1).toLowerCase());
}
}
return result.toString();
}
public static void main(String[] args) {
System.out.println(column2HumpName("hello"));
System.out.println(hump2LowerColumnName("hello5Word"));
}
}
3、註解工具類,用於提取類中含有指定註解的屬性和方法
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* * @projectName **-parent
* * @title AnnotationUtil
* * @package com.**.utils
* * @description 註解工具類
* * @author IT_CREAT
* * @date
* * @version V1.0.0
*
*/
public class AnnotationUtil {
/**
* 得到含有指定註解的類的屬性名字的集合,返回list
*
* @param javaBean
* @param annotationClass
* @return
*/
public static List getVarHaveAnnotation(Object javaBean, Class<? extends Annotation> annotationClass) {
List list = new ArrayList();
//變量
Class<?> clazz = javaBean.getClass();
Field[] fs = clazz.getDeclaredFields();
Field[] sufs = clazz.getSuperclass().getDeclaredFields();
for (Field f : fs) {
if (null != annotationClass && f.isAnnotationPresent(annotationClass)) {
list.add(f.getName());
}
}
for (Field f : sufs) {
if (null != annotationClass && f.isAnnotationPresent(annotationClass)) {
list.add(f.getName());
}
}
return list;
}
/**
* 得到含有指定註解的類的方法名字的集合,返回list
*
* @param javaBean
* @param annotationClass
* @return
*/
public static List getMethodHaveAnnotation(Object javaBean, Class<? extends Annotation> annotationClass) {
List list = new ArrayList();
//方法
Class<?> clazz = javaBean.getClass();
Method[] ms = clazz.getDeclaredMethods();
Method[] sums = clazz.getSuperclass().getDeclaredMethods();
for (Method m : ms) {
if (null != annotationClass && m.isAnnotationPresent(annotationClass)) {
list.add(m.getName());
}
}
for (Method m : sums) {
if (null != annotationClass && m.isAnnotationPresent(annotationClass)) {
list.add(m.getName());
}
}
return list;
}
/**
* 得到對象上含有指定註解的屬性名字,返回map,比如:有註解的屬性有name,age,那麼返回的map就是{name:name,age:age}
*
* @param javaBean
* @param annotationClass
* @return
*/
public static Map getVarMapHaveAnnotation(Object javaBean, Class<? extends Annotation> annotationClass) {
Map map = new HashMap();
//變量
Class<?> clazz = javaBean.getClass();
Field[] fs = clazz.getDeclaredFields();
Field[] sufs = clazz.getSuperclass().getDeclaredFields();
for (Field f : fs) {
if (null != annotationClass && f.isAnnotationPresent(annotationClass)) {
map.put(f.getName(), f.getName());
}
}
for (Field f : sufs) {
if (null != annotationClass && f.isAnnotationPresent(annotationClass)) {
map.put(f.getName(), f.getName());
}
}
return map;
}
/**
* 得到對象上含有指定註解的的方法名字,返回map,比如:有註解的屬性有name,age,那麼返回的map就是{name:name,age:age}
*
* @param javaBean
* @param annotationClass
* @return
*/
public static Map getMethodMapHaveAnnotation(Object javaBean, Class<? extends Annotation> annotationClass) {
Map map = new HashMap();
//方法
Class<?> clazz = javaBean.getClass();
Method[] ms = clazz.getDeclaredMethods();
Method[] sums = clazz.getSuperclass().getDeclaredMethods();
for (Method m : ms) {
if (null != annotationClass && m.isAnnotationPresent(annotationClass)) {
map.put(m.getName(), m.getName());
}
}
for (Method m : sums) {
if (null != annotationClass && m.isAnnotationPresent(annotationClass)) {
map.put(m.getName(), m.getName());
}
}
return map;
}
public static void main(String[] args) throws ClassNotFoundException {
Class<?> clazz = Class.forName("javax.persistence.Transient");
System.out.println(clazz.getName());
}
}
4、map和javabean對象相互轉換工具類,同時該工具類還可以在將對象轉爲map時,去除對象中含指定註解的屬性和空值屬性,最後生成自己想要的map,需要引用上面的的註解工具類和駝峯轉換類
package com.clfps.utils;
import org.springframework.util.ObjectUtils;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.*;
/**
*
* * @projectName **-parent
* * @title MapAndJavaBeanUtil
* * @package com.**.utils
* * @description map和javabean對象相互轉換工具類
* * @author IT_CREAT
* * @date
* * @version V1.0.0
*
*/
public class MapAndJavaBeanUtil {
/**
* 從map集合中獲取屬性值
*
* @param map
* @param key
* @param defaultValue
* @param <E>
* @return
*/
@SuppressWarnings({"unchecked", "rawtypes"})
public final static <E> E get(Map map, Object key, E defaultValue) {
Object o = map.get(key);
if (o == null)
return defaultValue;
return (E) o;
}
/**
* Map集合對象轉化成 JavaBean集合對象
*
* @param javaBean JavaBean實例對象
* @param mapList Map數據集對象
* @return
*/
@SuppressWarnings({"rawtypes"})
public static <T> List<T> map2JavaBean(T javaBean, List<Map> mapList) throws Exception {
if (mapList == null || mapList.isEmpty()) {
return null;
}
List<T> objectList = new ArrayList<T>();
T object = null;
for (Map map : mapList) {
if (map != null) {
object = map2JavaBean(javaBean, map);
objectList.add(object);
}
}
return objectList;
}
/**
* Map對象轉化成 JavaBean對象
*
* @param javaBean JavaBean實例對象
* @param map Map對象
* @return
*/
@SuppressWarnings({"rawtypes", "unchecked", "hiding"})
public static <T> T map2JavaBean(T javaBean, Map map) throws Exception {
// 獲取javaBean屬性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
// 創建 JavaBean 對象
Object obj = javaBean.getClass().newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean屬性名
Object propertyValue = null; // javaBean屬性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (map.containsKey(propertyName)) {
propertyValue = map.get(propertyName);
pd.getWriteMethod().invoke(obj, new Object[]{propertyValue});
}
}
return (T) obj;
}
return null;
}
/**
* JavaBean對象轉化成sql字段Map對象,移除指定註解字段,null即爲不移除,同時移除爲空值的字段,需要自己指定,true即爲移除
* @param javaBean 傳入對象
* @param removeNullValue 是否移除空字段
* @param annotationClass 傳入註解的class 如:Transient.class,含有該註解的字段都將不轉換,null值及忽略
* @return
* @throws Exception
*/
public static Map javaBean2SqlMap(Object javaBean,Boolean removeNullValue,Class<? extends Annotation>... annotationClass) throws Exception {
Map map = javaBean2SqlMap(javaBean,annotationClass);
if(removeNullValue == false){
return map;
}
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
if(ObjectUtils.isEmpty(map.get(key))){
iterator.remove();
}
}
return map;
}
/**
* JavaBean對象轉化成Map對象,移除指定註解字段,null即爲不移除,同時移除爲空值的字段,需要自己指定,true即爲移除
*
* @param javaBean 傳入對象
* @param removeNullValue 是否移除空字段
* @param annotationClass 傳入註解的class 如:Transient.class,含有該註解的字段都將不轉換,null值及忽略
* @return
*/
@SuppressWarnings({"rawtypes", "unchecked"})
public static Map javaBean2Map(Object javaBean,Boolean removeNullValue,Class<? extends Annotation>... annotationClass) throws Exception {
Map map = javaBean2Map(javaBean,annotationClass);
if(removeNullValue == false){
return map;
}
Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
if(ObjectUtils.isEmpty(map.get(key))){
iterator.remove();
}
}
return map;
}
/**
* JavaBean對象轉化成Map對象,,移除指定註解字段,null即爲不移除
*
* @param javaBean 傳入對象
* @param annotationClass 傳入註解的class 如:Transient.class
* @return
*/
@SuppressWarnings({"rawtypes", "unchecked"})
private static Map javaBean2Map(Object javaBean,Class<? extends Annotation>... annotationClass) throws Exception {
Map map = new HashMap();
// 獲取javaBean屬性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
//得到這個類中包含這個註解的所有屬性
Map varMap = new HashMap();
if(annotationClass != null){
for(Class<? extends Annotation> an :annotationClass){
Map varMapHaveAnnotation = AnnotationUtil.getVarMapHaveAnnotation(javaBean,an);
Set<String> keys = varMapHaveAnnotation.keySet();
for (String key : keys) {
varMap.put(key,varMapHaveAnnotation.get(key));
}
}
}
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean屬性名
Object propertyValue = null; // javaBean屬性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (!varMap.containsKey(propertyName) && !"class".equals(propertyName)) {
Method readMethod = pd.getReadMethod();
propertyValue = readMethod.invoke(javaBean, new Object[0]);
map.put(propertyName, propertyValue);
}
}
}
return map;
}
/**
* JavaBean對象轉化成sql字段Map對象,移除指定註解字段,null即爲不移除
*
* @param javaBean 傳入對象
* @param annotationClass 傳入註解的class 如:Transient.class
* @return
*/
@SuppressWarnings({"rawtypes", "unchecked"})
private static Map javaBean2SqlMap(Object javaBean,Class<? extends Annotation>... annotationClass) throws Exception {
Map map = new HashMap();
// 獲取javaBean屬性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
//得到這個類中包含這個註解的所有屬性
Map varMap = new HashMap();
if(annotationClass != null){
for(Class<? extends Annotation> an :annotationClass){
Map varMapHaveAnnotation = AnnotationUtil.getVarMapHaveAnnotation(javaBean,an);
Set<String> keys = varMapHaveAnnotation.keySet();
for (String key : keys) {
varMap.put(key,varMapHaveAnnotation.get(key));
}
}
}
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean屬性名
Object propertyValue = null; // javaBean屬性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (!varMap.containsKey(propertyName) && !"class".equals(propertyName)) {
Method readMethod = pd.getReadMethod();
propertyValue = readMethod.invoke(javaBean, new Object[0]);
propertyName = HumpNamedTools.hump2LowerColumnName(propertyName);
map.put(propertyName, propertyValue);
}
}
}
return map;
}
}
5、時間工具類
import org.apache.commons.lang3.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class DateTimeUtils {
public static Date getCurrentDate() {
return getCurrentDateTime();
}
public static Date getCurrentDateTime() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date now = new Date();
String tmp = sdf.format(now);
try {
return sdf.parse(tmp);
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
/**
* 獲得昨天yyyy-MM-dd格式的字符串
*
* @return YYYY-MM-DD格式的
*/
public static String getYesterday() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Calendar cpcalendar = new GregorianCalendar();
cpcalendar.add(Calendar.DATE, -1);
String now = format.format(cpcalendar.getTime());
return now;
}
/**
* 獲得當前日期yyyy-MM-dd格式的字符串
*
* @return YYYY-MM-DD格式的
*/
public static String getCurrentDateString() {
return getCurrentDateTimeString("yyyy-MM-dd");
}
/**
* 獲得當前日期和時間,日期加時分秒,yyyy-MM-dd hh:mm:ss
*
* @return
*/
public static String getCurrentDateTimeString() {
return getCurrentDateTimeString("yyyy-MM-dd HH:mm:ss");
}
public static String getCurrentDateTimeString(String format) {
SimpleDateFormat sdf = new SimpleDateFormat(format);
Date now = new Date();
return sdf.format(now);
}
public static Date parse(String param) {
return parse(param, "yyyy-MM-dd HH:mm:ss");
}
public static Date parseShort(String param) {
return parse(param, "yyyy-MM-dd");
}
public static Date parse(String param, String format) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
date = sdf.parse(param);
} catch (ParseException ex) {
}
return date;
}
public static String formatDateTime(Date date) {
return format(date, "yyyy-MM-dd HH:mm:ss");
}
public static String formatDate(Date date) {
return format(date, "yyyy-MM-dd");
}
public static String format(Date date, String format) {
if (date == null) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
String date_str = "";
try {
date_str = sdf.format(date);
} catch (Exception e) {
}
return date_str;
}
public static String time2String(Date date) {
if (date == null) {
return "";
}
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
String date_str = "";
try {
date_str = sdf.format(date);
} catch (Exception e) {
}
return date_str;
}
/**
* 返回一定時間後的日期
*
* @param date 開始計時的時間
* @param year 增加的年
* @param month 增加的月
* @param day 增加的日
* @return
*/
public static Date getAfterDate(Date date, int year, int month, int day) {
return getAfterDate(date, year, month, day, 0, 0, 0);
}
/**
* 返回一定時間後的日期
*
* @param date 開始計時的時間
* @param year 增加的年
* @param month 增加的月
* @param day 增加的日
* @param hour 增加的小時
* @param minute 增加的分鐘
* @param second 增加的秒
* @return
*/
public static Date getAfterDate(Date date, int year, int month, int day, int hour, int minute, int second) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
if (year != 0) {
cal.add(Calendar.YEAR, year);
}
if (month != 0) {
cal.add(Calendar.MONTH, month);
}
if (day != 0) {
cal.add(Calendar.DATE, day);
}
if (hour != 0) {
cal.add(Calendar.HOUR_OF_DAY, hour);
}
if (minute != 0) {
cal.add(Calendar.MINUTE, minute);
}
if (second != 0) {
cal.add(Calendar.SECOND, second);
}
return cal.getTime();
}
//獲得當前的年月日時分秒
public static int getCurrentYear() {
Calendar cal = new GregorianCalendar();
return cal.get(Calendar.YEAR);
}
public static int getCurrentMonth() {
Calendar cal = new GregorianCalendar();
return cal.get(Calendar.MONTH) + 1;
}
public static int getCurrentDay() {
Calendar cal = new GregorianCalendar();
return cal.get(Calendar.DAY_OF_MONTH);
}
public static int getCurrentHour() {
Calendar cal = new GregorianCalendar();
return cal.get(Calendar.HOUR_OF_DAY);
}
public static int getCurrentMinute() {
Calendar cal = new GregorianCalendar();
return cal.get(Calendar.MINUTE);
}
public static int getCurrentSecond() {
Calendar cal = new GregorianCalendar();
return cal.get(Calendar.SECOND);
}
//獲得一定時間的年月日時分秒
public static int getYear(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.YEAR);
}
public static int getMonth(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MONTH);
}
public static int getDay(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_MONTH);
}
public static int getHour(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.HOUR_OF_DAY);
}
public static int getMinute(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.MINUTE);
}
public static int getSecond(Date date) {
if (date == null) {
date = new Date();
}
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.SECOND);
}
//比較兩個時間的差值
public static long getDaysOfDate(Date d1, Date d2) {
double c = (d1.getTime() - d2.getTime()) / 1000 / 3600 / 24;
return (long) Math.floor(c);
}
public static long getHoursOfDate(Date d1, Date d2) {
double c = (d1.getTime() - d2.getTime()) / 1000 / 3600;
return (long) Math.floor(c);
}
public static long getMinutesOfDate(Date d1, Date d2) {
double c = (d1.getTime() - d2.getTime()) / 1000 / 60;
return (long) Math.floor(c);
}
public static long getSecondsOfDate(Date d1, Date d2) {
double c = (d1.getTime() - d2.getTime()) / 1000;
return (long) Math.floor(c);
}
/**
* 取得當前月的最大天數
*
* @return
*/
public static int getDaysOfMonth() {
return getDaysOfMonth(getCurrentYear(), getCurrentMonth());
}
/**
* 取得一個月的最大天數
*
* @param year
* @param month
* @return
*/
public static int getDaysOfMonth(int year, int month) {
return (int) ((toLongTime(month == 12 ? (year + 1) : year,
month == 12 ? 1 : (month + 1), 1) -
toLongTime(year, month, 1)) / (24 * 60 * 60 * 1000));
}
/**
* 取得下一個月的最大天數
*
* @param year
* @param month
* @return
*/
public static int getDaysOfNextMonth(int year, int month) {
year = month == 12 ? year + 1 : year;
month = month == 12 ? 1 : month + 1;
return getDaysOfMonth(year, month);
}
/**
* 取得上個月的最大天數
*
* @param year
* @param month
* @return
*/
public static int getDaysOfPreMonth(int year, int month) {
year = month == 1 ? year - 1 : year;
month = month == 1 ? 12 : month - 1;
return getDaysOfMonth(year, month);
}
/**
* 取上月第一天
*
* @param dqrq Description of Parameter
* @return String
* @since 2003-0416
*/
public static String getFirstDayOfPreMonth(String dqrq) {
String strDQRQ = dqrq;
String strYear = "";
String strMonth = "";
String strDay = "";
if (strDQRQ == null) {
return "";
}
if (strDQRQ.length() == 8) {
strYear = strDQRQ.substring(0, 4);
strMonth = strDQRQ.substring(4, 6);
strDay = strDQRQ.substring(6, 8);
}
if (strDQRQ.length() == 10) {
strYear = strDQRQ.substring(0, 4);
strMonth = strDQRQ.substring(5, 7);
strDay = strDQRQ.substring(8, 10);
}
int iMonth = Integer.parseInt(strMonth);
int iYear = Integer.parseInt(strYear);
if (iMonth == 1) {
iYear = iYear - 1;
iMonth = 12;
} else if (iMonth > 1) {
iMonth = iMonth - 1;
} else {
return "";
}
if (iMonth < 10) {
strMonth = "0" + iMonth;
} else {
strMonth = "" + iMonth;
}
strDay = "01";
if (strDQRQ.length() == 8) {
return iYear + strMonth + strDay;
} else if (strDQRQ.length() == 10) {
return iYear + "-" + strMonth + "-" + strDay;
} else {
return "";
}
}
/**
* 從給定的 year,mongth,day 得到時間的long值表示
* milliseconds after January 1, 1970 00:00:00 GMT).
*
* @param year 年
* @param month 月
* @param day 日
* @return 給定的 year,mongth,day 得到時間的long值表示
*/
public static long toLongTime(int year, int month, int day) {
return toDate(year, month, day).getTime();
}
/**
* 從年月日得到一個Date對象
*
* @param year 年
* @param month 月
* @param day 日
* @return 得到的Date對象
*/
public static Date toDate(int year, int month, int day) {
Calendar cld = new GregorianCalendar();
cld.clear();
cld.set(Calendar.YEAR, year);
cld.set(Calendar.MONTH, month - 1);
cld.set(Calendar.DAY_OF_MONTH, day);
return cld.getTime();
//.getTime();
}
/**
* 將時間轉換爲時間戳
*
* @param dateTime 具體時間,跟時間格式對應
* @return 時間戳
* @throws ParseException 日期轉換異常
*/
public static String dateToStamp(String dateTime) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = simpleDateFormat.parse(dateTime);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 將時間轉換爲時間戳
*
* @param dateTime 具體時間,跟時間格式對應
* @return 時間戳
* @throws ParseException 日期轉換異常
*/
public static String dateTimeToStamp(String dateTime) throws ParseException {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(dateTime);
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 將時間轉換爲時間戳
*
* @param date 具體時間
* @return 時間戳
* @throws ParseException 日期轉換異常
*/
public static String dateToStamp(Date date) throws ParseException {
if(date == null){
return null;
}
String res;
long ts = date.getTime();
res = String.valueOf(ts);
return res;
}
/**
* 將時間戳轉換爲時間
*
* @param timestamp 時間戳字符串
* @return 時間字符串
*/
public static String stampToDateStr(String timestamp) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
long lt = new Long(timestamp);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 將時間戳轉換爲時間
*
* @param timestamp 時間戳字符串
* @return 時間字符串
*/
public static String stampToDateTimeStr(String timestamp) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
long lt = new Long(timestamp);
Date date = new Date(lt);
res = simpleDateFormat.format(date);
return res;
}
/**
* 將時間戳轉換爲時間
*
* @param timestamp 時間戳字符串
* @return 時間字符串
*/
public static Date stampToDateTime(String timestamp) {
if(StringUtils.isBlank(timestamp)){
return null;
}
long lt = new Long(timestamp);
return new Date(lt);
}
/**
* 將時間戳轉換爲時間
*
* @param timestamp 時間戳字符串
* @return 時間字符串
*/
public static Date stampToDateTime(Long timestamp) {
if(timestamp == null){
return null;
}
return new Date(timestamp);
}
/**
* 將時間戳轉換爲時間
*
* @param timestamp 時間戳
* @return 時間字符串
*/
public static String stampToDateTimeStr(Long timestamp) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(timestamp);
res = simpleDateFormat.format(date);
return res;
}
/**
* 將時間戳轉換爲時間
*
* @param timestamp 時間戳
* @return 時間字符串
*/
public static String stampToDateStr(Long timestamp) {
String res;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date(timestamp);
res = simpleDateFormat.format(date);
return res;
}
public static void main(String args[]) {
//DateTimeUtil du =new DateTimeUtil();
//log.info(du.getCurrentDateString());
//log.info(du.getCurrentDate());
}
}
6、數組、字符串、對象工具類
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* * @projectName **-parent
* * @title ArrayUtils
* * @package com.**.util
* * @description 數組常用判斷類
* * @author IT_CREAT
* * @date
* * @version V1.0.0
*
*/
public class ArrayUtils {
/**
* 判斷數組是否爲空
* @param objects
* @return
*/
public static Boolean arrayIsEmpty(Object[] objects) {
Boolean bool = false;
if (objects == null || objects.length == 0) {
bool = true;
}
return bool;
}
/**
* 判斷集合是否爲空
* @param list
* @return
*/
public static Boolean listIsEmpty(List list) {
Boolean bool = false;
if (list == null || list.size() == 0) {
bool = true;
}
return bool;
}
/**
* 判斷字符串是否爲空
* @param str
* @return
*/
public static Boolean stringIsEmpty(String str) {
Boolean bool = false;
if (str == null || "".equals(str)) {
bool = true;
}
return bool;
}
/**
* 判斷對象是否爲空
* @param object
* @return
*/
public static Boolean objectIsEmpty(Object object) {
Boolean bool = false;
if (object == null) {
bool = true;
}
return bool;
}
/**
* 判斷是否所有字符串都爲空
* @param strs
* @return
*/
public static Boolean stringIsAllEmpty(String... strs) {
Boolean bool = true;
if (strs == null) {
return bool;
}
for (String str : strs) {
if (str != null && !"".equals(str)) {
bool = false;
break;
}
}
return bool;
}
/**
* 判斷是否所有對象都爲空
* @param objects
* @return
*/
public static Boolean objectIsAllEmpty(Object... objects) {
Boolean bool = true;
if (objects == null) {
return bool;
}
for (Object object : objects) {
if (object != null && !(object instanceof String)) {
bool = false;
break;
} else if ((object instanceof String) && object != null && !"".equals(object)) {
bool = false;
break;
}
}
return bool;
}
/**
* 數組轉集合
* @param objects
* @return
*/
public static List array2List(Object[] objects){
if(objects == null){
return null;
}
return new ArrayList<>(Arrays.asList(objects));
}
/**
* 集合轉數組
* @param list
* @return
*/
public static Object[] list2Array(List list){
if(list == null){
return null;
}
return list.toArray();
}
public static void main(String[] args) {
Boolean bo = false;
System.out.println(objectIsAllEmpty(bo, ""));
System.out.println(stringIsAllEmpty("123", ""));
List list = new ArrayList();
list.add("niahh");
list.add("zaaa");
System.out.println(array2List(list2Array(list)));
}
}