generated from root/miduo_server
168 lines
5.5 KiB
Java
168 lines
5.5 KiB
Java
package com.jmfy.utils;
|
|
|
|
import com.google.gson.Gson;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.text.DateFormat;
|
|
import java.text.ParseException;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
|
|
public class JsonUtil {
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(JsonUtil.class);
|
|
private static JsonUtil jsonUtil;
|
|
|
|
private Gson gson = new Gson();
|
|
|
|
private static class InnerClass {
|
|
private static final JsonUtil SINGLETON = new JsonUtil();
|
|
}
|
|
|
|
public static JsonUtil getInstence() {
|
|
if (jsonUtil == null) {
|
|
jsonUtil = JsonUtil.InnerClass.SINGLETON;
|
|
}
|
|
return jsonUtil;
|
|
}
|
|
|
|
public Gson getGson() {
|
|
return gson;
|
|
}
|
|
|
|
public HashMap<String, String> getParameterMap(HttpServletRequest request) {
|
|
HashMap<String, String> map = new HashMap();
|
|
Enumeration paramNames = request.getParameterNames();
|
|
while (paramNames.hasMoreElements()) {
|
|
String paramName = (String) paramNames.nextElement();
|
|
String[] paramValues = request.getParameterValues(paramName);
|
|
String paramValue = paramValues[0];
|
|
map.put(paramName, paramValue);
|
|
LOGGER.info("parameter : " + paramName + "=" + paramValue);
|
|
}
|
|
return map;
|
|
}
|
|
|
|
public String getReward( HttpServletRequest request) {
|
|
StringBuilder reward = new StringBuilder();
|
|
String[] itemIds = request.getParameterValues("itemIds[]");
|
|
String[] itemNums = request.getParameterValues("itemNums[]");
|
|
return getReward(itemIds,itemNums);
|
|
}
|
|
|
|
|
|
public String getReward(String[] itemIds,String[] itemNums) {
|
|
StringBuilder reward = new StringBuilder();
|
|
|
|
if (itemIds.length ==0 || itemNums.length ==0 ){
|
|
return reward.toString();
|
|
}
|
|
for (int i = 0; i < itemIds.length; i++){
|
|
if (itemIds[i].isEmpty() || itemNums[i].isEmpty()){
|
|
return reward.toString();
|
|
}
|
|
if (reward.length() == 0){
|
|
reward = new StringBuilder(itemIds[i] + "#" + itemNums[i] );
|
|
}else{
|
|
reward.append("|").append(itemIds[i]).append("#").append(itemNums[i]).append("#");
|
|
}
|
|
}
|
|
return reward.toString();
|
|
}
|
|
|
|
|
|
public static String getServiceKey(String serviceName, String host, String port) {
|
|
StringBuilder sb = new StringBuilder();
|
|
return sb.append(serviceName).append("|")
|
|
.append(host).append("|")
|
|
.append(port).toString();
|
|
}
|
|
|
|
/**
|
|
* 获取日期前一天
|
|
* @param specifiedDay
|
|
* @return
|
|
*/
|
|
public static String getSpecifiedDayBefore(String specifiedDay){
|
|
//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
Calendar c = Calendar.getInstance();
|
|
Date date=null;
|
|
try {
|
|
date = new SimpleDateFormat("yy-MM-dd").parse(specifiedDay);
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
c.setTime(date);
|
|
int day=c.get(Calendar.DATE);
|
|
c.set(Calendar.DATE,day-1);
|
|
|
|
String dayBefore=new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
|
|
return dayBefore;
|
|
}
|
|
public static String date2TimeStamp(String date_str){
|
|
try {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
return String.valueOf(sdf.parse(date_str).getTime()/1000);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return "";
|
|
}
|
|
//年月日时分秒 转为毫秒的时间戳
|
|
public static String date3TimeStamp(String date_str){
|
|
try {
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
return String.valueOf(sdf.parse(date_str).getTime());
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return "";
|
|
}
|
|
public static String timeStamp2Date(String seconds) {
|
|
if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
|
|
return "";
|
|
}
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
return sdf.format(new Date(Long.valueOf(seconds)));
|
|
}
|
|
//
|
|
public static long getAppointTimeInXDay(long date, int hour) {
|
|
Calendar calendar = Calendar.getInstance();
|
|
calendar.setTimeInMillis(date);
|
|
calendar.set(Calendar.HOUR_OF_DAY, hour);
|
|
calendar.set(Calendar.MINUTE, 0);
|
|
calendar.set(Calendar.SECOND, 0);
|
|
calendar.set(Calendar.MILLISECOND, 0);
|
|
return calendar.getTimeInMillis();
|
|
}
|
|
|
|
public static List<String> getDays(String startTime, String endTime) {
|
|
|
|
// 返回的日期集合
|
|
List<String> days = new ArrayList<String>();
|
|
|
|
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
|
try {
|
|
Date start = dateFormat.parse(startTime);
|
|
Date end = dateFormat.parse(endTime);
|
|
|
|
Calendar tempStart = Calendar.getInstance();
|
|
tempStart.setTime(start);
|
|
|
|
Calendar tempEnd = Calendar.getInstance();
|
|
tempEnd.setTime(end);
|
|
tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
|
|
while (tempStart.before(tempEnd)) {
|
|
days.add(dateFormat.format(tempStart.getTime()));
|
|
tempStart.add(Calendar.DAY_OF_YEAR, 1);
|
|
}
|
|
} catch (ParseException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
return days;
|
|
}
|
|
}
|