提交 061f0244 编写于 作者: C chenjianqiang

v1.0

上级 b888b50d
package com.lishihe.hosts.utils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
/**
* @author: jacky
* @date: 2020/6/11 16:28
* @apiNote: JDK 8 新日期类 格式化与字符串转换 工具类
*/
public class DateUtil {
public static final DateTimeFormatter DFY_MD_HMS = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static final DateTimeFormatter DFY_MD = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
* LocalDateTime 转时间戳
*
* @param localDateTime /
* @return /
*/
public static Long getTimeStamp(LocalDateTime localDateTime) {
return localDateTime.atZone(ZoneId.systemDefault()).toEpochSecond();
}
/**
* 时间戳转LocalDateTime
*
* @param timeStamp /
* @return /
*/
public static LocalDateTime fromTimeStamp(Long timeStamp) {
return LocalDateTime.ofEpochSecond(timeStamp, 0, OffsetDateTime.now().getOffset());
}
/**
* LocalDateTime 转 Date
* Jdk8 后 不推荐使用 {@link Date} Date
*
* @param localDateTime /
* @return /
*/
public static Date toDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
}
/**
* LocalDate 转 Date
* Jdk8 后 不推荐使用 {@link Date} Date
*
* @param localDate /
* @return /
*/
public static Date toDate(LocalDate localDate) {
return toDate(localDate.atTime(LocalTime.now(ZoneId.systemDefault())));
}
/**
* Date转 LocalDateTime
* Jdk8 后 不推荐使用 {@link Date} Date
*
* @param date /
* @return /
*/
public static LocalDateTime toLocalDateTime(Date date) {
return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
/**
* 日期 格式化
*
* @param localDateTime /
* @param patten /
* @return /
*/
public static String localDateTimeFormat(LocalDateTime localDateTime, String patten) {
DateTimeFormatter df = DateTimeFormatter.ofPattern(patten);
return df.format(localDateTime);
}
/**
* 日期 格式化
*
* @param localDateTime /
* @param df /
* @return /
*/
public static String localDateTimeFormat(LocalDateTime localDateTime, DateTimeFormatter df) {
return df.format(localDateTime);
}
/**
* 日期格式化 yyyy-MM-dd HH:mm:ss
*
* @param localDateTime /
* @return /
*/
public static String localDateTimeFormatAllMate(LocalDateTime localDateTime) {
return DFY_MD_HMS.format(localDateTime);
}
/**
* 日期格式化 yyyy-MM-dd
*
* @param localDateTime /
* @return /
*/
public static String localDateTimeFormatyMd(LocalDateTime localDateTime) {
return DFY_MD.format(localDateTime);
}
/**
* 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd
*
* @param localDateTime /
* @return /
*/
public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, String pattern) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
}
/**
* 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd
*
* @param localDateTime /
* @return /
*/
public static LocalDateTime parseLocalDateTimeFormat(String localDateTime, DateTimeFormatter dateTimeFormatter) {
return LocalDateTime.from(dateTimeFormatter.parse(localDateTime));
}
/**
* 字符串转 LocalDateTime ,字符串格式 yyyy-MM-dd HH:mm:ss
*
* @param localDateTime /
* @return /
*/
public static LocalDateTime parseLocalDateTimeFormatyMdHms(String localDateTime) {
return LocalDateTime.from(DFY_MD_HMS.parse(localDateTime));
}
}
package com.lishihe.hosts.utils;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* http 工具类
*
* @author chen
*/
public class HttpUtils {
/**
* POST请求-默认请求头
*
* @param json 请求JSON对象
* @param url 请求地址
* @return 请求所响应的内容串
*/
public static String post(JSONObject json, String url) {
return post(json, null, url);
}
/**
* POST请求
*
* @param json 请求JSON对象
* @param headerMap 请求头组
* @param url 请求地址
* @return 请求所响应的内容串
*/
public static String post(JSONObject json, Map<String, String> headerMap, String url) {
String result = "";
HttpPost post = new HttpPost(url);
try {
if (headerMap != null) {
Set<Map.Entry<String, String>> set = headerMap.entrySet();
for (Map.Entry<String, String> entry : set) {
post.setHeader(entry.getKey(), entry.getValue());
}
} else {
post.addHeader("Authorization", "Basic YWRtaW46");
post.setHeader("Content-Type", "application/json;charset=utf-8");
}
CloseableHttpClient httpClient = HttpClients.createDefault();
StringEntity postingString = new StringEntity(json.toString(), "utf-8");
post.setEntity(postingString);
HttpResponse response = httpClient.execute(post);
InputStream in = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
strber.append(line + '\n');
}
br.close();
in.close();
result = strber.toString();
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
result = "服务器异常";
}
} catch (Exception e) {
System.out.println("请求异常");
throw new RuntimeException(e);
} finally {
post.abort();
}
return result;
}
/**
* GET请求
*
* @param paramMap 请求实体组
* @param headerMap 请求头组
* @param url 请求地址
* @return 请求所响应的内容串
*/
public static String get(Map<String, String> paramMap, Map<String, String> headerMap, String url) {
String result = "";
HttpGet get = new HttpGet(url);
try {
if (headerMap != null) {
Set<Map.Entry<String, String>> set = headerMap.entrySet();
for (Map.Entry<String, String> entry : set) {
get.setHeader(entry.getKey(), entry.getValue());
}
} else {
get.setHeader("Connection", "keep-alive");
get.setHeader("Cache-Control", "no-cache");
get.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36 MicroMessenger/7.0.9.501 NetType/WIFI MiniProgramEnv/Windows WindowsWechat");
get.setHeader("content-type", "application/json; charset=utf-8");
get.setHeader("Accept-Encoding", "gzip, deflate, br");
get.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
get.setHeader("Accept-Language", "zh-CN");
}
CloseableHttpClient httpClient = HttpClients.createDefault();
List<NameValuePair> params = setHttpParams(paramMap);
String param = URLEncodedUtils.format(params, "UTF-8");
get.setURI(URI.create(url + "?" + param));
HttpResponse response = httpClient.execute(get);
result = getHttpEntityContent(response);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
result = "服务器异常";
}
} catch (Exception e) {
System.out.println("请求异常");
throw new RuntimeException(e);
} finally {
get.abort();
}
return result;
}
/**
* GET请求-默认请求头
*
* @param paramMap 请求实体组
* @param url 请求地址
* @return 请求所响应的内容串
*/
public static String get(Map<String, String> paramMap, String url) {
return get(paramMap, null, url);
}
public static List<NameValuePair> setHttpParams(Map<String, String> paramMap) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> set = paramMap.entrySet();
for (Map.Entry<String, String> entry : set) {
params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
return params;
}
public static String getHttpEntityContent(HttpResponse response) throws UnsupportedOperationException, IOException {
String result = "";
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream in = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8"));
StringBuilder strber = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
strber.append(line + '\n');
}
br.close();
in.close();
result = strber.toString();
}
return result;
}
}
<!-- Plugin Configuration File. Read more: https://plugins.jetbrains.com/docs/intellij/plugin-configuration-file.html -->
<idea-plugin>
<!-- Unique identifier of the plugin. It should be FQN. It cannot be changed between the plugin versions. -->
<id>com.lishihe.hosts</id>
<!-- Public plugin name should be written in Title Case.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-name -->
<name>Hosts</name>
<!-- A displayed Vendor name or Organization ID displayed on the Plugins Page. -->
<vendor email="buchen@lishihe.com" url="https://www.lishihe.com">LSH</vendor>
<!-- Description of the plugin displayed on the Plugin Page and IDE Plugin Manager.
Simple HTML elements (text formatting, paragraphs, and lists) can be added inside of <![CDATA[ ]]> tag.
Guidelines: https://plugins.jetbrains.com/docs/marketplace/plugin-overview-page.html#plugin-description -->
<description><![CDATA[
change your sys hosts at now ~<br>
use this plugin is easy<br>
插件帮助开发工作者快速切换hosts文件而无需安装其他软件。<br>
Plugins help developers quickly switch hosts files without installing additional software.<br>
プラグインは、開発者が追加のソフトウェアをインストールせずにホストファイルをすばやく切り替えるのに役立ちます。<br>
Плагины помогают разработчикам быстро переключать файлы хостов без установки дополнительного программного обеспечения.<br>
<em>v1.0</em>
]]></description>
<!-- Product and plugin compatibility requirements.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-compatibility.html -->
<depends>com.intellij.modules.platform</depends>
<extensions defaultExtensionNs="com.intellij">
<toolWindow id="Hosts" anchor="bottom" factoryClass="com.lishihe.hosts.factory.ToolWindowsFactory"
secondary="true" canCloseContents="false"/>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册