一些数据格式的转换(JAVA以及JS)

[JAVA]

日期类型:Sat Oct 08 18:57:38 CST 2022转化为2022年10月08日 18时57分38秒

Calendar calendar = Calendar.getInstance();
//获取当前日期的年

String timeStr = calendar.get(Calendar.YEAR)+"年"+(calendar.get(Calendar.MONTH)+1)+"月"+calendar.get(Calendar.DATE)+"日 "+calendar.get(Calendar.HOUR_OF_DAY)+"点"+calendar.get(Calendar.MINUTE)+"分"+calendar.get(Calendar.SECOUD)+"秒";

数字类型:

【按照指定格式转换】BigDecimal——String

DecimalFormat decimalFormat = new DecimalFormat("0.0000#");
String format = decimalFormat.format(new BigDecimal(100));

【JS--thymeleaf中日期类型的格式转换写法】

//使用该方法可直接转换的数据
[[${#dates.format(item.createTime, 'yyyy-MM-dd HH:mm:ss')}][[${startNodeRecord.createTime.toLocaleString()}]]

//使用上边方法不能转换的数据可以使用下边方法进行转换
<!--时间格式转换方法-->
<script>
    function format(formatTime) {
        let a = new Date(formatTime).getTime();
        const date = new Date(a);
        const Y = date.getFullYear() + '-';
        const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '  ';
        const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        const s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        const dateString = Y + M + D + h + m + s;
        return dateString;
    }
</script>

表单数据转换成字符串

var data = $("表单id").serialize();

将data序列化的字符串转换成标准的json字符串,去除所有转义字符

decodeURIComponent(data,true);

json字符串转换成map集合

/**
 *json转换为map
 */
function jsonToMap(jsonStr){
    return this.objToStrMap(JSON.parse(jsonStr));
}

表单数据转换成数组

var arr = $("表单id").serializeArray();

猜你喜欢

转载自blog.csdn.net/snowing1997/article/details/127451870
今日推荐