博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java自定义工具类编写
阅读量:3951 次
发布时间:2019-05-24

本文共 10704 字,大约阅读时间需要 35 分钟。

java自定义工具类编写

得到一天的最后时刻

public static Date getDateOneDayEnd(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.MINUTE, 59); calendar.set(Calendar.SECOND, 59); Date date1 = calendar.getTime(); return date1; }

得到一天的开始时刻

public static Date getOneDatefirst(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date date1 = calendar.getTime(); return date1; }

得到明天

public static Date addOnyDay(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1); Date date1 = calendar.getTime(); return date1; }

得到昨天

public static Date reduceOnyDay(Date date) throws ParseException {
Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1); Date date1 = calendar.getTime(); return date1; }

得到两个时间的间隔天数

public static long betweenTwoDays(Date date1, Date date2) {
long a1 = date1.getTime(); long a2 = date2.getTime(); long a3 = a2 - a1; a3 = a3 / (3600 * 24 * 1000); return a3; }

解析日期字符串

public static final SimpleDateFormat DATE_TIME_FORMAT = new SimpleDateFormat("yyyyMMddHHmmss");    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");    public static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss"); /**     * @param: String      * @return: java.util.Date     */    public static Date parseDateTime(String dateTime) {
try {
return DATE_TIME_FORMAT.parse(dateTime); } catch (ParseException e) {
e.printStackTrace(); } return null; }

用java8封装的新类实现

/**     *     * @param localDateTime     * @return 一天的最后时刻     */    public static LocalDateTime  getDateEnd(LocalDateTime localDateTime){
return localDateTime.withHour(23) .withMinute(59) .withSecond(59) .withNano(999999999); } /** * * @param localDateTime * @return 返回一天的开始时刻 */ public static LocalDateTime getDateFirst(LocalDateTime localDateTime){
return localDateTime.withHour(0) .withMinute(0) .withSecond(0) .withNano(0); } /** * * @param localDateTime * @return 返回明天 */ public static LocalDateTime plusOneDay(LocalDateTime localDateTime){
return localDateTime.plus(1, ChronoUnit.DAYS); } /** * * @param localDateTime * @return 返回昨天 */ public static LocalDateTime minuOneDay(LocalDateTime localDateTime){
return localDateTime.minus(1, ChronoUnit.DAYS); } /** * * @param localDateTime1 * @param localDateTime2 * @return 两个日期的间隔天数 */ public static int betweenTwoTime(LocalDateTime localDateTime1,LocalDateTime localDateTime2){
long secondone =localDateTime1.toEpochSecond(ZoneOffset.of("+8")); long secondtwo=localDateTime2.toEpochSecond(ZoneOffset.of("+8")); int day= (int) (secondtwo-secondone/(3600*24)); return day ; }

判断传入值是否为空

/**判断传入值是否为空*/	public static boolean isEmpty(String str) {
if ( str == null || "".equals(str) || "null".equals(str) ) {
return true; } else {
return false; } }

判断传入字符串是否不为空

/**     *     * @param string     * @return boolean     */    public static boolean isNotEmpty(String string){
if(!"".equals(string)&&string!=null) return true; else return false; }

数字字符串转换为Integer数组(Integer.parseInt只能传入数字组成的字符串),会去重

/**     * String字符串转数字数组     * @param strings:传入的数组字符串     * @param separator:(自定义常量 )分隔符支持','及'&'等自定义分隔符     * */    public static Integer[] stringToArray(String strings,String separator){
try {
List
list = new ArrayList<>(); String[] strings1 = strings.split(separator); for (String str1 : strings1) {
if(!NumberUtil.isEmpty(str1)){
Integer id = Integer.parseInt(str1); if(!list.contains(id)){
list.add(id); } } } return list.toArray(new Integer[list.size()]); } catch (RuntimeException e) {
throw e; } }

注意传入的要是数字符字数组不是英文字符数组,stream迭代输出

@Test    void test7(){
String s="31 23 12 3 123 1 23 12 31 23"; Integer integer[]=NumberUtil.stringToArray(s,Separator); Arrays.stream(integer).forEach(e->System.out.println(e)); }

字符串换为字符串数组

/**     *     * @param strings     * @param separator     * @return String[]     */    public static  String[] stringToArrayString(String strings,String separator){
try {
String string[]=strings.split(separator); return string; }catch (RuntimeException e) {
throw e; } }

stream输出

@Test    void test7(){
String s="31 23 12 3 123 1 23 12 31 23"; Integer integer[]=NumberUtil.stringToArray(s,Separator); Arrays.stream(integer).forEach(e->System.out.println(e)); String string[]=NumberUtil.stringToArrayString(s,Separator); Arrays.stream(string).forEach(e->System.out.println(e)); }

数字字符串转List

/**     *      * @param strings 数字字符串     * @param separator     * @return  List
*/ public static List
stringToIntegerList(String strings, String separator) {
try {
String[] splits = strings.split(separator); List
list = new ArrayList<>(); for (String string : splits) {
if (NumberUtil.isNotEmpty(string)) {
Integer integer = Integer.parseInt(string); if (!list.contains(integer)) {
list.add(integer); } } } return list; }catch (RuntimeException e){
throw e; } }
@Test    void test8(){
String ss=" 23 12 3 123 12 312312 3 123 12"; List
list=NumberUtil.stringToIntegerList(ss,Separator); list.stream().forEach(e->System.out.println(e)); }

字符串转List

/**     *      * @param strings     * @param separator     * @return List
*/ public static List
stringToListString(String strings,String separator){
try {
List
list=new ArrayList<>(); String[] splits = strings.split(separator); for(String string:splits){
if(NumberUtil.isNotEmpty(string)){
if(!list.contains(string)){
list.add(string); } } } return list; }catch (RuntimeException e){
throw e; } }
@Test    void test9(){
String s="qeqe12c121xe123h12b31 23 12 21 321 3s 123 12 321 s3 123 s21 d1 23 123c0"; List
list=NumberUtil.stringToListString(s,Separator); list.stream().forEach(e->System.out.println(e)); }

得到字符串的编码

/**     *     * @param string     * @return encode     */    public static String getEncoding(String string) {
String encode = "GB2312"; try {
if (string.equals(new String(string.getBytes(encode), encode))) {
String s = encode; return s; } } catch (Exception exception) {
} encode = "ISO-8859-1"; try {
if (string.equals(new String(string.getBytes(encode), encode))) {
String s1 = encode; return s1; } } catch (Exception exception1) {
} encode = "UTF-8"; try {
if (string.equals(new String(string.getBytes(encode), encode))) {
String s2 = encode; return s2; } } catch (Exception exception2) {
} encode = "GBK"; try {
if (string.equals(new String(string.getBytes(encode), encode))) {
String s3 = encode; return s3; } } catch (Exception exception3) {
} return "";
@Test    void test11(){
String s="werqew "; System.out.println(NumberUtil.getEncoding(s)); }

集合分割,比如长度为6的分割为长度为2的

/**     * @param list     * @param count 每一个子集合的长度     * @param 
* @return 分割后的集合的集合 */ public static
List
> splitCollection(List
list, int count) {
if (list == null || count < 1) return null; List
> returnlist = new ArrayList<>(); int size = list.size();//总长 if (size < count) { returnlist.add(list); } else { int pre = size / count; int last = size % count; //前面每个pre for (int i = 0; i < pre; i++) { List
itemList = new ArrayList<>(); for (int j = 0; j < count; j++) { itemList.add(list.get(i * count + j)); } returnlist.add(itemList); } //last的进行处理 if (last > 0) { List
itemList = new ArrayList<>(); for (int i = 0; i < last; i++) { itemList.add(list.get(pre * count + i)); } returnlist.add(itemList); } } return returnlist; }
@Test    void test12() {
List
list = new ArrayList<>(); list.add("as"); list.add("eeweqw"); list.add("qweqwe"); list.add("as"); list.add("12321"); list.add("dss"); List
> lists = NumberUtil.splitCollection(list, 2); lists.stream().forEach(e -> System.out.println(e)); }

匹配正则表达式

//只含字母或者数字  public static final String REGEX_NUMBER_LETTER="^[A-Za-z0-9]+$";    public static boolean matchesRegex(String content, String regex) {
Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(content); if (matcher.matches()) return true; else return false; }
@Test    void test13() {
String s = "123j1231c12d3x1212"; System.out.println(RegexUtil.matchesRegex(s, RegexUtil.REGEX_NUMBER_LETTER)); }

转载地址:http://chrwi.baihongyu.com/

你可能感兴趣的文章
linux学习之source命令与alias(别名)使用
查看>>
MYSQL常用查询
查看>>
安装Linux虚拟机绑定IP操作
查看>>
centos7离线安装 mysql
查看>>
mysql学习使用一(查询)
查看>>
Linux 学习之sed命令详解
查看>>
JAVA基础——常用IO使用
查看>>
spring框架pom.xml文件解析
查看>>
代码比较工具DiffMerge的下载和使用
查看>>
linux学习之vim全选,全部复制,全部删除
查看>>
linux 学习之awk命令
查看>>
linux学习之查找文件find,locate,whereis使用
查看>>
JS中$含义及用法
查看>>
web学习之ajax记录
查看>>
web学习之ajax参数解析
查看>>
linux学习之curl命令使用
查看>>
java模板引擎中主要三个JSP,Freemarker,Velocity简述
查看>>
javascript学习之$(function() {})
查看>>
kafka初识
查看>>
mysql存储过程 --游标的使用 取每行记录
查看>>