ParameterUtil.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package com.chinaitop.depot.utils;
  2. import java.beans.BeanInfo;
  3. import java.beans.Introspector;
  4. import java.beans.PropertyDescriptor;
  5. import java.io.BufferedInputStream;
  6. import java.io.InputStream;
  7. import java.lang.reflect.Method;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.*;
  11. public class ParameterUtil {
  12. public static String getSysDateTime(){
  13. String temp_str="";
  14. Date dt = new Date();
  15. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  16. temp_str=sdf.format(dt);
  17. System.out.println("获取当前时间"+temp_str);
  18. return temp_str;
  19. }
  20. public static String getDateYMDHMS(Date date) {
  21. String str = "";
  22. SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  23. str=sdf.format(date);
  24. System.out.println("获取当前时间"+str);
  25. return str;
  26. }
  27. public static String getSysDate(){
  28. String temp_str="";
  29. Date dt = new Date();
  30. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  31. temp_str=sdf.format(dt);
  32. return temp_str;
  33. }
  34. public static Date string2datetime(String dateTime){
  35. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  36. Date date = null;
  37. try {
  38. date = dateFormat.parse(dateTime);
  39. } catch (ParseException e) {
  40. e.printStackTrace();
  41. }
  42. return date;
  43. }
  44. public static Date string2date(String dateTime){
  45. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  46. Date date = null;
  47. try {
  48. date = dateFormat.parse(dateTime);
  49. } catch (ParseException e) {
  50. e.printStackTrace();
  51. }
  52. return date;
  53. }
  54. public static String date2string(Date date){
  55. SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
  56. String str=sdf.format(date);
  57. return str;
  58. }
  59. /**
  60. * 获取指定时间的时间戳
  61. * @param datatime
  62. * @return
  63. */
  64. public static String getTimeStamp(String datatime){
  65. long timeStamp = 0;
  66. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  67. try {
  68. Date date = format.parse(datatime);
  69. timeStamp = date.getTime();
  70. } catch (ParseException e) {
  71. e.printStackTrace();
  72. }
  73. return String.valueOf(timeStamp);
  74. }
  75. /*
  76. * 将时间戳转换为时间
  77. */
  78. public static String stampToDate(String s){
  79. String res;
  80. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  81. long lt = new Long(s);
  82. Date date = new Date(lt);
  83. res = simpleDateFormat.format(date);
  84. return res;
  85. }
  86. public static void main(String[] args) {
  87. System.out.println(stampToDate("1504689208643"));
  88. System.out.println(getTimeStamp(getSysDateTime()));
  89. }
  90. public static String getBHS(String BHS){
  91. String[] str = BHS.split(",");
  92. StringBuffer sb = new StringBuffer();
  93. for(int i=0;i<str.length;i++){
  94. sb.append("'").append(str[i]).append("'").append(",");
  95. }
  96. String s=sb.toString();
  97. String t=s.substring(0,s.length()-1);
  98. return t;
  99. }
  100. //判断字段值是为空 不为空返回ture
  101. public static boolean isnotnull(Object name){
  102. return (null!=name&&!"".equals(name) && !"null".equals(name) && "null" != name);
  103. }
  104. //判断字段值是为空,为空返回ture
  105. public static boolean isnull(Object name){
  106. return (null==name || "".equals(name) || "null".equals(name) || "null" == name);
  107. }
  108. //判断字段值是相等 相等返回ture
  109. public static boolean isequal(Object name,Object value){
  110. if(name==value||name.equals(value)){
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * 判断list和str数组是否全等
  117. * @param list
  118. * @param str
  119. * @return
  120. */
  121. public static boolean isAllequal(List<String> list,String[] str){
  122. boolean isbz = true;
  123. for(int z=0;z<str.length;z++){
  124. if(!isequal(list.get(z),str[z])){
  125. isbz = false;
  126. break;
  127. }
  128. }
  129. return !isbz || !(str.length == list.size());
  130. }
  131. //判断字段值是否在某个集合里 在返回ture
  132. public static boolean isallequal(Object name,String[] value){
  133. for(int i=0;i<value.length;i++){
  134. if(name==value[i]||name.equals(value[i])){
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * 从properties得到路径
  142. * @param filePath
  143. * @return
  144. */
  145. public static Properties readProperties(String filePath) {
  146. Properties props = new Properties();
  147. try {
  148. InputStream in = new BufferedInputStream(ParameterUtil.class.getResourceAsStream(filePath));
  149. props.load(in);
  150. } catch (Exception e) {
  151. e.printStackTrace();
  152. }
  153. return props;
  154. }
  155. /**
  156. * 生成32位随机数
  157. *
  158. * @return 返回32位随机数
  159. */
  160. public static String getCode() {
  161. UUID uuid = UUID.randomUUID();
  162. return uuid.toString().replace("-", "");
  163. }
  164. /**
  165. * 实体类转Map共通方法
  166. *
  167. * @param bean 实体类
  168. * @return Map
  169. * @throws Exception
  170. */
  171. public static Map convertBean(Object bean) throws Exception {
  172. Class type = bean.getClass();
  173. Map returnMap = new HashMap();
  174. BeanInfo beanInfo = Introspector.getBeanInfo(type);
  175. PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
  176. for (PropertyDescriptor descriptor : propertyDescriptors) {
  177. String propertyName = descriptor.getName();
  178. if (!propertyName.equals("class")) {
  179. Method readMethod = descriptor.getReadMethod();
  180. Object result = readMethod.invoke(bean);
  181. if (result != null) {
  182. returnMap.put(propertyName, result);
  183. } else {
  184. returnMap.put(propertyName, "");
  185. }
  186. }
  187. }
  188. return returnMap;
  189. }
  190. }