JAVA 工具类集合

这里暂时存放一些JAVA的工具类

获取百分比

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class PercentUtil {

    public static String getDividePercent(Integer molecular, Integer denominator) {
        if (denominator == 0) {
            return "0";
        }
        String percent = new DecimalFormat("0.00").format((float)molecular / denominator);
        NumberFormat nf = NumberFormat.getPercentInstance();
        return nf.format(Float.valueOf(percent));
    }
}

获取当前是今年的第几周

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class YearWeekUtil {

    public static String getCurrentYearWeek() {
        return getCurrnetYear() + "-" + getCurrentWeek();
    }

    private static String getCurrnetYear() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        return dateFormat.format(new Date());
    }

    private static String getCurrentWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        return String.valueOf(calendar.get(Calendar.WEEK_OF_YEAR));
    }
}

判断字符串时间格式先后

public class TimeUtil {
 
    // 判断失效时间和当前时间,当前时间 < 失效时间 ? true : false
    public static Boolean isExpired(String endtime) {
 
        if (StringUtils.isEmpty(endtime)) {
            return Boolean.TRUE;
        }
 
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
 
            Date dateCurrent = sdf.parse(currentTime);
            Date dateUser = sdf.parse(endtime);
 
            return dateUser.before(dateCurrent);
        } catch (ParseException e) {
            log.error(" ", e);
            return Boolean.TRUE;
        }
    }
}

JSONArray排序

Collections.sort-A

    private JSONArray sortJSONArrayByVV(JSONArray srcArray) {
        JSONArray dis = new JSONArray();
        List<JSONObject> jsonArrayList = new ArrayList<>();
        for (int i = 0; i < srcArray.size(); i++) {
            jsonArrayList.add((JSONObject) srcArray.get(i));
        }
        Collections.sort(jsonArrayList, new Comparator<JSONObject>() {
 
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                Long vv1 = o1.getLong("vv");
                Long vv2 = o2.getLong("vv");
                return -vv1.compareTo(vv2);
            }
        });
 
        for (int i = 0; i < jsonArrayList.size(); i++) {
            dis.add(jsonArrayList.get(i));
        }
        return dis;
    }

Collections.sort-A

String jsonArrStr = "[ { \"ID\": \"135\", \"Name\": \"Fargo Chan\" },{ \"ID\": \"432\", \"Name\": \"Aaron Luke\" },{ \"ID\": \"252\", \"Name\": \"Dilip Singh\" }]";
 
    JSONArray jsonArr = new JSONArray(jsonArrStr);
    JSONArray sortedJsonArray = new JSONArray();
 
    List<JSONObject> jsonValues = new ArrayList<JSONObject>();
    for (int i = 0; i < jsonArr.length(); i++) {
        jsonValues.add(jsonArr.getJSONObject(i));
    }
    Collections.sort( jsonValues, new Comparator<JSONObject>() {
        //You can change "Name" with "ID" if you want to sort by ID
        private static final String KEY_NAME = "Name";
 
        @Override
        public int compare(JSONObject a, JSONObject b) {
            String valA = new String();
            String valB = new String();
 
            try {
                valA = (String) a.get(KEY_NAME);
                valB = (String) b.get(KEY_NAME);
            } 
            catch (JSONException e) {
                //do something
            }
 
            return valA.compareTo(valB);
            //if you want to change the sort order, simply use the following:
            //return -valA.compareTo(valB);
        }
    });
 
    for (int i = 0; i < jsonArr.length(); i++) {
        sortedJsonArray.put(jsonValues.get(i));
    }

Collections.sort-C

Collections.sort(boards, Comparator.comparing(BoardBean::getScore).reversed());

JAVA读写文件

package main.com.imekaku;

import org.junit.Test;

import java.io.*;

public class FileTest {

    @Test
    public void test1() {
        File file = new File("/Users/lee/temp/jsontest/src/main/com.imekaku/files/hello.txt");
        File fileResult = new File("/Users/lee/temp/jsontest/src/main/com.imekaku/files/result.txt");

        if (fileResult.exists()) {
            fileResult.delete();
        }

        if (file.exists() && file.isFile()) {
            try {
                InputStreamReader inputStreamReader = new InputStreamReader(
                        new FileInputStream(file), "utf-8"
                );
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
                        new FileOutputStream(fileResult, true), "utf-8"
                );
                BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

                String lineTxt;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    System.out.println(lineTxt);
                    bufferedWriter.write(lineTxt);
                    bufferedWriter.newLine();
                }

                bufferedWriter.close();
                outputStreamWriter.close();

                bufferedReader.close();
                inputStreamReader.close();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部