All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.net.vidyo.yd.common.utils.ValueUtil Maven / Gradle / Ivy

package cn.net.vidyo.yd.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValueUtil {

    public static void putMapMoneyItem(Map data,String key,long money){
        putMapMoneyItem(data, key, money,true);
    }
    public static void putMapMoneyItem(Map data,String key,long money,boolean isAddMoneyString){
        data.put(key,money);
        if(isAddMoneyString){
            data.put(key+"String",money100IntToStringMoney(money));
        }
    }

    public static String convertTimeToString(long time){
        if(time==0) return "";
        Date date=new Date(time);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String format = f.format(date);
        return format;
    }
    public static boolean checkIsNumber(String content) {
        try {
            Double num1 = Double.parseDouble(content);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    public static Date UTCToCST(String UTCStr) throws ParseException {
        return UTCToCST(UTCStr, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    }

    public static Date UTCToCST(String UTCStr, String format) throws ParseException {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(UTCStr);
        System.out.println("UTC时间: " + date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
        //calendar.getTime() 返回的是Date类型,也可以使用calendar.getTimeInMillis()获取时间戳
        return calendar.getTime();
    }

    public static String convertMoneyString(int money) {
        double d = money / 100;
        return String.valueOf(d);
    }

    public static int convertStringToMoneyInt(String content) {
        double v = toDouble(content, 0L);
        v = v * 100;
        return (int) v;
    }

    public static String convertMoneyIntToMoneyString(int money) {
        double val = money;
        val = val / 100;
        String content = String.valueOf(val);
        if (content.indexOf(".") < 0) {
            content = content + ".00";
        }
        if (content.endsWith(".0")) {
            content = content + "0";
        }
        content = "¥" + content;
        return content;
    }

    public static String convertMoneyIntToNegativeMoneyString(int money) {
        double val = money;
        val = val / 100;
        String content = String.valueOf(val);
        if (content.indexOf(".") < 0) {
            content = content + ".00";
        }
        if (content.endsWith(".0")) {
            content = content + "0";
        }
        content = "¥-" + content;
        return content;
    }

    //
    public static long mapItemToIntFromDateString(Map data, String key) {
        String s = mapItemToString(data, key);
        return DateTimeUtil.convertDateToTimeMillis(s, DateTimeUtil.SHORT_DATE_FORMAT);
    }

//    public static int mapItemToMoneyInt(Map data, String key) {
//        return mapItemToMoneyInt(data, key, 0);
//    }
//
//    public static int mapItemToMoneyInt(Map data, String key, int defaultValue) {
//        String s = mapItemToString(data, key);
//        if (s.length() == 0) {
//            return defaultValue;
//        }
//        double val = Double.valueOf(s);
//        val = val * 100;
//        return (int) val;
//    }

    public static Date mapItemToDate(Map data, String key, Date defaultValue) {
        String content = mapItemToString(data, key, "");
        if (content.length() == 0) {
            return defaultValue;
        }
        if (content.length() < 10) {
            return defaultValue;
        }
        try {
            return UTCToCST(content);
        } catch (ParseException e) {
            return defaultValue;
        }
    }
//    public static String mapItemToStringDate(Map data, String key, String defaultValue) {
//        //2021-07-13T16:13:28.362Z
//        String content = mapItemToString(data, key, "");
//        if(content==null || content.length()==0){
//            return defaultValue;
//        }
//        if(content.length()<10){
//            return defaultValue;
//        }
//        content= content.substring(0,10);
//        content=content.replace("-","");
//        return content;
//    }

    public static long mapItemToLongTimeByDayStart(Map data, String key, long defaultValue) {
        //2022-01-14T10:54:37.615Z
        String s = mapItemToString(data, key, "");
        if (s.length() == 0) {
            return defaultValue;
        }
        return DateTimeUtil.convertDateStringToLongStartTime(s, DateTimeUtil.SHORT_DATE_FORMAT);
    }

    public static long mapItemToLongTimeByDayEnd(Map data, String key, long defaultValue) {
        //2022-01-14T10:54:37.615Z
        String s = mapItemToString(data, key, "");
        if (s.length() == 0) {
            return defaultValue;
        }
        return DateTimeUtil.convertDateStringToLongStopTime(s, DateTimeUtil.SHORT_DATE_FORMAT);
    }

//    /**
//     *
//     * 

Description: 本地时间转化为UTC时间

// * @param localTime // * @return // * @author wgs // * @date 2018年10月19日 下午2:23:43 // * // */ // public static Date localToUTC(String localTime) { // DateTimeUtil.par // SimpleDateFormat localFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //// SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // try { // localFormat.setTimeZone(TimeZone.getTimeZone("GMT")); // Date date = localFormat.parse(localTime); // return date; // } catch (ParseException e) { // return new Date(); // } //// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //// Date localDate= null; //// try { //// localDate = sdf.parse(localTime); //// } catch (ParseException e) { //// e.printStackTrace(); //// } //// long localTimeInMillis=localDate.getTime(); //// /** long时间转换成Calendar */ //// Calendar calendar= Calendar.getInstance(); //// calendar.setTimeInMillis(localTimeInMillis); //// /** 取得时间偏移量 */ //// int zoneOffset = calendar.get(java.util.Calendar.ZONE_OFFSET); //// /** 取得夏令时差 */ //// int dstOffset = calendar.get(java.util.Calendar.DST_OFFSET); //// /** 从本地时间里扣除这些差量,即可以取得UTC时间*/ //// calendar.add(java.util.Calendar.MILLISECOND, -(zoneOffset + dstOffset)); //// /** 取得的时间就是UTC标准时间 */ //// Date utcDate=new Date(calendar.getTimeInMillis()); //// return utcDate; // } // /** // * // *

Description:UTC时间转化为本地时间

// * @param utcTime // * @return // * @author wgs // * @date 2018年10月19日 下午2:23:24 // * // */ // public static Date utcToLocal(String utcTime){ //// SimpleDateFormat localFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //// SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // SimpleDateFormat utcFormat = new SimpleDateFormat("yyyy/MM/dd"); // try { //// utcFormat.setTimeZone(TimeZone.getTimeZone("UTC")); // Date date = utcFormat.parse(utcTime); // return date; // } catch (ParseException e) { // return new Date(); // } //// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //// sdf.setTimeZone(TimeZone.getTimeZone("UTC")); //// Date utcDate = null; //// try { //// utcDate = sdf.parse(utcTime); //// } catch (ParseException e) { //// e.printStackTrace(); //// } //// sdf.setTimeZone(TimeZone.getDefault()); //// Date locatlDate = null; //// String localTime = sdf.format(utcDate.getTime()); //// try { //// locatlDate = sdf.parse(localTime); //// } catch (ParseException e) { //// e.printStackTrace(); //// } //// return locatlDate; // } //
// public static int mapItemToInt(Map data, String key) { return mapItemToInt(data, key, 0); } public static int mapItemToInt(Map data, String key, int defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return toInt(data.get(key), defaultValue); } public static long mapItemToLong(Map data, String key) { return mapItemToLong(data, key, 0); } public static long mapItemToLong(Map data, String key, long defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return toLong(data.get(key), defaultValue); } public static boolean mapItemToBool(Map data, String key) { return mapItemToBool(data, key, false); } public static boolean mapItemToBool(Map data, String key, boolean defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return toBool(data.get(key), defaultValue); } public static String mapItemToString(Map data, String key) { return mapItemToString(data, key, ""); } public static String mapItemToString(Map data, String key, String defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return data.get(key).toString(); } public static double mapItemToDouble(Map data, String key) { return mapItemToDouble(data, key, 0); } public static double mapItemToDouble(Map data, String key, double defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return toDouble(data.get(key), defaultValue); } public static Date mapItemToStandardDate(Map data, String key) { return mapItemToStandardDate(data, key, null); } public static Date mapItemToStandardDate(Map data, String key, Date defaultValue) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return mapItemToDate(data, key, df, defaultValue); } public static Date mapItemToDate(Map data, String key, SimpleDateFormat simpleDateFormat, Date defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return toDate(data.get(key), simpleDateFormat, defaultValue); } public static long mapItemMoneyTo100IntMoney(Map data, String key) { return mapItemMoneyTo100IntMoney(data, key, 0); } public static long mapItemMoneyTo100IntMoney(Map data, String key, long defaultValue) { if (data == null || !data.containsKey(key)) { return defaultValue; } return moneyStringTo100IntMoney(data.get(key), defaultValue); } // // public static String dateToString(long time) { Date date = new Date(time); SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd"); String format = f.format(new Date(time)); return format; } public static long stringToLongDate(String timeString) { return 0L; } public static Date toDate(Object value) { return toDate(value, null); } public static Date toDate(Object value, Date defaultValue) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return toDate(value, df, defaultValue); } public static Date toDate(Object value, SimpleDateFormat simpleDateFormat, Date defaultValue) { String s = toString(value); if (s.length() == 0) { return defaultValue; } try { Date date = simpleDateFormat.parse(s); return date; } catch (ParseException e) { return defaultValue; } } public static String toString(Object value) { return toString(value, ""); } public static String toString(Object value, String defaultValue) { if (value == null) { return defaultValue; } return value.toString(); } public static int toInt(Object value) { return toInt(value, 0); } public static int toInt(Object value, int defaultValue) { String s = toString(value, ""); if (s == null || s.length() == 0) { return defaultValue; } if (s.endsWith(".0")) { s = s.substring(0, s.length() - 2); } if (s.indexOf("E") > 0) { Long aLong = scienceToLong(s); return aLong.intValue(); } if (s.indexOf("e") > 0) { Long aLong = scienceToLong(s); return aLong.intValue(); } return Integer.valueOf(s); } public static boolean toBool(Object value) { return toBool(value, false); } public static boolean toBool(Object value, boolean defaultValue) { String s = toString(value, ""); if (s == null || s.length() == 0) { return defaultValue; } return Boolean.valueOf(s); } public static long toLong(Object value) { return toLong(value, 0); } public static long toLong(Object value, long defaultValue) { String s = toString(value, ""); if (s == null || s.length() == 0) { return defaultValue; } if (s.endsWith(".0")) { s = s.substring(0, s.length() - 2); } if (s.indexOf("E") > 0) { return scienceToLong(s); } if (s.indexOf("e") > 0) { return scienceToLong(s); } return Long.valueOf(s); } /** * 科学计数法形式字符串转Long * * @param str s * @return s */ public static Long scienceToLong(String str) { BigDecimal demical = new BigDecimal(str); // str = str.replaceAll(",", ""); // Long aLong = Long.parseLong(str); return demical.longValue(); } public static double toDouble(Object value) { return toDouble(value, 0); } public static double toDouble(Object value, double defaultValue) { String s = toString(value, ""); if (s == null || s.length() == 0) { return defaultValue; } if (s.indexOf("e") > 0 || s.indexOf("E") > 0) { return scienceToLong(s); } return Double.valueOf(s); } public static BigDecimal toBigDecimal(Object value) { double v = toDouble(value, 0L); return BigDecimal.valueOf(v); } public static long moneyStringTo100IntMoney(Object value) { return moneyStringTo100IntMoney(value, 0); } public static long moneyStringTo100IntMoney(Object value, long defaultValue) { BigDecimal v = toBigDecimal(value); BigDecimal b = BigDecimal.valueOf(100); v = v.multiply(b); try { return v.longValue(); } catch (Exception e) { return defaultValue; } } public static String money100IntToStringMoney(Object value) { return money100IntToStringMoney(value, "0.00"); } public static String money100IntToStringMoney(Object value, String defaultValue) { BigDecimal v = toBigDecimal(value); BigDecimal b = BigDecimal.valueOf(100); v = v.divide(b); b =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数 try { return "¥"+b+""; } catch (Exception e) { return defaultValue; } } // public static String filterEmoji(String nick_name) { //nick_name 所获取的用户昵称 if (nick_name == null) { return nick_name; } Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\ud83e\udc00-\ud83e\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE); Matcher emojiMatcher = emoji.matcher(nick_name); if (emojiMatcher.find()) { //将所获取的表情转换为* nick_name = emojiMatcher.replaceAll("*"); return nick_name; } return nick_name; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy