java常用函数收集(一)

Java代码 复制代码
  1. /**  
  2.  * 根据传入的格式获取日期  
  3.  *   
  4.  * @param format  
  5.  *            如:YYYYMMDD || MM/dd/yyyy, hh:mm:ss  
  6.  * @return 字符串的日期  
  7.  */  
  8. public String getSysDate(String format) {   
  9.     String dateStr = "";   
  10.     try {   
  11.         Format formatter;   
  12.         Date date = new Date();   
  13.         formatter = new SimpleDateFormat(format);   
  14.         dateStr = formatter.format(date);   
  15.     } catch (Exception e) {   
  16.         System.out.println(e);   
  17.     }   
  18.     return dateStr;   
  19. }   
  20. /**  
  21.  * 根据传入的格式获取日期  
  22.  *   
  23.  * @param format  
  24.  *            如:YYYYMMDD || MM/dd/yyyy, hh:mm:ss  
  25.  * @return 字符串的日期  
  26.  */  
  27. public String getFormatDate(Date date, String format) {   
  28.     String dateStr = "";   
  29.     try {   
  30.         Format formatter;   
  31.         formatter = new SimpleDateFormat(format);   
  32.         dateStr = formatter.format(date);   
  33.     } catch (Exception e) {   
  34.         System.out.println(e);   
  35.     }   
  36.     return dateStr;   
  37. }   
  38. /**  
  39.  * 获取分割后的字符串数组信息  
  40.  *   
  41.  * @param Str  
  42.  * @param Split  
  43.  * @return 字符串数组  
  44.  */  
  45. public String[] getSplit(String Str, String Split) {   
  46.     return Str.split(Split);   
  47. }   
  48. /**  
  49.  * 把字符串转换成指定的日期格式  
  50.  *   
  51.  * @param str  
  52.  * @param format  
  53.  * @return  
  54.  */  
  55. public Date Convert(String str, String format) {   
  56.     java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(format);   
  57.     try {   
  58.         java.util.Date d = sdf.parse(str);   
  59.         return d;   
  60.     } catch (Exception ex) {   
  61.         ex.printStackTrace();   
  62.         return null;   
  63.     }   
  64. }   
  65. /**  
  66.  * 获取月的天数  
  67.  *   
  68.  * @param year  
  69.  * @param month  
  70.  * @return  
  71.  */  
  72. public static int getdays(String year, String month) {   
  73.     int yearInt = Integer.parseInt(year);   
  74.     int monthInt = Integer.parseInt(month);   
  75.     int monthdays = 31;   
  76.     switch (monthInt) {   
  77.     case 1:   
  78.     case 3:   
  79.     case 5:   
  80.     case 7:   
  81.     case 8:   
  82.     case 10:   
  83.     case 12: {   
  84.         monthdays = 31;   
  85.         break;   
  86.     }   
  87.     case 2: {   
  88.         if (isLeapyear(yearInt)) {   
  89.             monthdays = 29;   
  90.         } else {   
  91.             monthdays = 28;   
  92.         }   
  93.         break;   
  94.     }   
  95.     case 4:   
  96.     case 6:   
  97.     case 9:   
  98.     case 11: {   
  99.         monthdays = 30;   
  100.         break;   
  101.     }   
  102.     }   
  103.     return monthdays;   
  104. }   
  105.   
  106. /**  
  107.  * 判断闰年  
  108.  *   
  109.  * @param year  
  110.  * @return  
  111.  */  
  112. public static boolean isLeapyear(int year) {   
  113.     if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {   
  114.         return true;   
  115.     } else {   
  116.         return false;   
  117.     }   
  118. }   
  119.   
  120. /**  
  121.  * 判断某天是星期几  
  122.  *   
  123.  * @param strDate  
  124.  * @return 0 表示是星期天  
  125.  */  
  126. public static int getWeekByDate(String strDate) {   
  127.     int dayOfWeek = 0;   
  128.     try {   
  129.   
  130.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
  131.         Calendar calendar = Calendar.getInstance();   
  132.         Date date = new Date();   
  133.         date = sdf.parse(strDate);   
  134.         calendar.setTime(date);   
  135.         dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);   
  136.     } catch (Exception e) {   
  137.         e.printStackTrace();   
  138.     }   
  139.     return dayOfWeek - 1;   
  140. }   
  141. /**  
  142.  * 判断字符串是不是数字  
  143.  *   
  144.  * @param str  
  145.  * @return  
  146.  */  
  147. public static boolean isNumeric(String str) {   
  148.     Pattern pattern = Pattern.compile("[0-9]*");   
  149.     Matcher isNum = pattern.matcher(str);   
  150.     if (!isNum.matches()) {   
  151.         return false;   
  152.     }   
  153.     return true;   
  154. }   
  155. /**  
  156.  * 获得距给定日期countday的字符串格式  
  157.  *   
  158.  * @param date  
  159.  * @param countday  
  160.  * @param flag  
  161.  *            为true表示日期前,为false表示日期后  
  162.  * @return YYYY-MM-DD  
  163.  */  
  164. public String getDateString(Date date, int countday, boolean flag) {   
  165.     String datestr = "";   
  166.     if (flag) {   
  167.         datestr = getFormatDate(new Date((new Date()).getTime() - countday   
  168.                 * 24 * 60 * 60 * 1000l), "yyyy-MM-dd");   
  169.     } else {   
  170.         datestr = getFormatDate(new Date((new Date()).getTime() + countday   
  171.                 * 24 * 60 * 60 * 1000l), "yyyy-MM-dd");   
  172.     }   
  173.     return datestr;   
  174. }   
  175. /***************************************************************************  
  176.  * 根据两个时间判断时间差  
  177.  * @throws ParseException   
  178.  * @throws ParseException   
  179.  **************************************************************************/  
  180. public Long getDateDifference(Date date1,Date date2) throws ParseException {   
  181. //      Date date1 = new SimpleDateFormat("yyyy-mm-dd").parse("2008-3-31");   
  182. //      Date date2 = new SimpleDateFormat("yyyy-mm-dd").parse("2008-3-30");   
  183.     // 日期相减得到相差的日期   
  184.     long day = (date1.getTime() - date2.getTime()) / (24 * 60 * 60 * 1000) > 0 ? (date1   
  185.             .getTime() - date2.getTime())   
  186.             / (24 * 60 * 60 * 1000)   
  187.             : (date2.getTime() - date1.getTime()) / (24 * 60 * 60 * 1000);   
  188.     return day;   
  189.   
  190. }   
  191. /***************************************************************************  
  192.  * 根据两个时间来判断时间的差值  
  193.  * @param days  
  194.  * @return  
  195.  */  
  196. public Long getDateDifference1(Date date1,Date date2) throws ParseException {   
  197.     // 日期相减得到相差的日期   
  198.     long day = (date1.getTime() - date2.getTime())/ (24 * 60 * 60 * 1000);   
  199.     return day;   
  200. }   
  201. /***************************************************************************  
  202.  * 返回当前时间的一个时间差时间  
  203.  * @param days  
  204.  * @return  
  205.  */  
  206. public static String Ds(int days) {   
  207.     SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd");   
  208.     Calendar calendar = Calendar.getInstance();   
  209.     int day = calendar.get(Calendar.DAY_OF_YEAR);   
  210.     calendar.set(Calendar.DAY_OF_YEAR, day - days);   
  211.     Date cc = calendar.getTime();   
  212.     return form.format(cc);   
  213. }   
  214. /*************************************************************************  
  215.  * 获取系统当前时间  
  216.  */  
  217. public static Date getSystemDate(){   
  218.     SimpleDateFormat   sf   =   new   SimpleDateFormat("yyyy-MM-dd");      
  219.     Date   date   =   new   Date();                                                                    
  220.     try {   
  221.         return new SimpleDateFormat("yyyy-mm-dd").parse(sf.format(date));   
  222.     } catch (ParseException e) {   
  223.     }   
  224.     return null;   
  225. }   
  226.  /**  
  227.    * 判断是否为整数  
  228.    *   
  229.    * @param str 传入的字符串  
  230.    * @return 是整数返回true,否则返回false  
  231.    */  
  232.   public static boolean isInteger(String str) {   
  233.     Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");   
  234.     return pattern.matcher(str).matches();   
  235.  }   
  236. /**  
  237.    * 判断是否为浮点数,包括double和float  
  238.    *   
  239.    * @param str 传入的字符串  
  240.    * @return 是浮点数返回true,否则返回false  
  241.    */  
  242.   public static boolean isDouble(String str) {   
  243.     Pattern pattern = Pattern.compile("^[-\\+]?[.\\d]*$");   
  244.     return pattern.matcher(str).matches();   
  245.   }   
  246. /**  
  247.    * 判断输入的字符串是否符合Email样式.  
  248.    *   
  249.    * @param str 传入的字符串  
  250.    * @return 是Email样式返回true,否则返回false  
  251.    */  
  252.   public static boolean isEmail(String str) {   
  253.     Pattern pattern = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");   
  254.     return pattern.matcher(str).matches();   
  255.   }   
  256. /**  
  257.    * 判断输入的字符串是否为纯汉字  
  258.    *   
  259.    * @param str 传入的字符窜  
  260.    * @return 如果是纯汉字返回true,否则返回false  
  261.    */  
  262.   public static boolean isChinese(String str) {   
  263.     Pattern pattern = Pattern.compile("[\u0391-\uFFE5]+$");   
  264.     return pattern.matcher(str).matches();   
  265.   }   
  266.   
  267. /**  
  268.    * 是否为空白,包括null和""  
  269.    *   
  270.    * @param str  
  271.    * @return  
  272.    */  
  273.   public static boolean isBlank(String str) {   
  274.     return str == null || str.trim().length() == 0;   
  275.   }   
  276. /**  
  277.    * 判断是否为质数  
  278.    *   
  279.    * @param x  
  280.    * @return  
  281.    */  
  282.   public static boolean isPrime(int x) {   
  283.     if (x <= 7) {   
  284.       if (x == 2 || x == 3 || x == 5 || x == 7)   
  285.         return true;   
  286.     }   
  287.     int c = 7;   
  288.     if (x % 2 == 0)   
  289.       return false;   
  290.     if (x % 3 == 0)   
  291.       return false;   
  292.     if (x % 5 == 0)   
  293.       return false;   
  294.     int end = (int) Math.sqrt(x);   
  295.     while (c <= end) {   
  296.       if (x % c == 0) {   
  297.         return false;   
  298.       }   
  299.       c += 4;   
  300.       if (x % c == 0) {   
  301.         return false;   
  302.       }   
  303.       c += 2;   
  304.       if (x % c == 0) {   
  305.         return false;   
  306.       }   
  307.       c += 4;   
  308.       if (x % c == 0) {   
  309.         return false;   
  310.       }   
  311.       c += 2;   
  312.       if (x % c == 0) {   
  313.         return false;   
  314.       }   
  315.       c += 4;   
  316.       if (x % c == 0) {   
  317.         return false;   
  318.       }   
  319.       c += 6;   
  320.       if (x % c == 0) {   
  321.         return false;   
  322.       }   
  323.       c += 2;   
  324.       if (x % c == 0) {   
  325.         return false;   
  326.       }   
  327.       c += 6;   
  328.     }   
  329.     return true;   
  330.   }   

你可能感兴趣的:(java,C++,c,C#)