涉及到正则表达式,时区转换。

/**

*

* 按格式 yyyy-MM-dd HH:mm:ss 以指定GMT时区进行解析,返回对应的当前系统时区当地时间。

* @param dateString  格式 yyyy-MM-dd HH:mm:ss

* @param timeZoneGMT  格式GMT+8,GMT-7,GMT+09:00,GMT-06:00

* @return

* @throws ParseException

*/

public static Date parseGMTDate(String dateString, String timeZoneGMT) throws ParseException {

String timeZoneGMTRegex = "^GMT[-+](\\d{1,2})(:?(\\d\\d))?$";

if (timeZoneGMT.matches(timeZoneGMTRegex)) {

TimeZone timeZone = TimeZone.getTimeZone(timeZoneGMT);

return parseDate(dateString, "yyyy-MM-dd HH:mm:ss", timeZone,null);

}

throw new ParseException(String.format("input GMT time zone format ['%s'] error ",timeZoneGMT),0);

}

/**

* 按指定格式 <code>pattern</code> 和指定时区进行解析,返回对应的当前系统时区当地时间。

*

* @param date

* @param pattern  格式参考 {@link SimpleDateFormat}

* @param timeZone

* @return

* @throws ParseException

*/

public static Date parseDate(String date, String pattern, TimeZone timeZone,Locale locale) throws ParseException {

Objects.requireNonNull(date, "date");

Objects.requireNonNull(pattern, "pattern");

SimpleDateFormat format = getSimpleDateFormat(pattern, timeZone, locale);

return format.parse(date);

}

private static SimpleDateFormat getSimpleDateFormat(String pattern, TimeZone timeZone, Locale locale) {

if (timeZone == null) {

timeZone = TimeZone.getDefault();

}

// 每次实例化 SimpleDateFormat 需要解析 pattern,非常耗时,但其本身又是线程不安全的,因此借用 ThreadLocal

LRU<String, SimpleDateFormat> formatMap = formats.get();

if (formatMap == null) {

formatMap = new LRU<String, SimpleDateFormat>(128);

formats.set(formatMap);

}

String formatId = pattern + timeZone.getID();

SimpleDateFormat format = formatMap.get(formatId);

if (format == null) {

if(locale != null){

format = new SimpleDateFormat(pattern,locale);

}else{

format = new SimpleDateFormat(pattern);

}

if (!format.getTimeZone().equals(timeZone)) {

format.setTimeZone(timeZone);

}

formatMap.put(formatId, format);

}

return format;

}

解析GMT+N时区,返回日期类型的更多相关文章

  1. 解析MySQL中存储时间日期类型的选择问题

    解析MySQL中存储时间日期类型的选择问题_Mysql_脚本之家 https://www.jb51.net/article/125715.htm 一般应用中,我们用timestamp,datetime ...

  2. springmvc 1.接受日期类型的参数 2.后台返回json串的格式处理(返回json串null值处理为"")

    springmvc中的配置: <bean id="dateConvert" class="com.iomp.util.DateConvert"/> ...

  3. 解决ASP.NET MVC返回的JsonResult 中 日期类型数据格式问题,和返回的属性名称转为“驼峰命名法”和循环引用问题

    DateTime类型数据格式问题 问题 在使用ASP.NET MVC 在写项目的时候发现,返回给前端的JSON数据,日期类型是 Date(121454578784541) 的格式,需要前端来转换一下才 ...

  4. 【规范建议】服务端接口返回字段类型与iOS端的解析

    一.本文档的写作目的 App需要跟产品.UI.后台.服务器.测试打交道,app的产出是其他端人员产出的综合体现.与其他端人员沟通就像是开发写接口,也就是面向接口编程的思想. 本文档讲解针对的是服务端返 ...

  5. Java | 日期类型的绍介和操作

    Date类 Date类在java.util.Date,Date类表示特定的瞬间,精确到毫秒.(毫秒是千分之一秒)毫秒可以对时间和日期进行计算,可以把日期转换为毫秒进行计算,计算完毕,再把毫秒转换为日期 ...

  6. java和数据库中日期类型的常见用法

    (1)java中日期类型:Date.Timestamp(2)数据库中:Date.Timestamp(3)字符串和Date之间的格式化转换:    SimpleDateFormat类方法: format ...

  7. SpringBoot对接收及返回Instant类型的处理(转)

    一:处理post请求json中的Instant类型1.springboot中日期格式化配置: spring: jackson: date-format: yyyy-MM-dd HH:mm:ss tim ...

  8. Swift根据日期字符串返回日期是星期几

    最近在做的一个IOS项目中需要根据日期得出日期代表的是星期几,日期以字符串的形式获得,于是该方法可以简单描述如下: /* * 根据日期格式字符串返回日期代表星期几 * 参数:dateTime,字符串类 ...

  9. Java 基础【09】 日期类型

    java api中日期类型的继承关系 java.lang.Object --java.util.Date --java.sql.Date --java.sql.Time --java.sql.Time ...

随机推荐

  1. python 嵌套列表

  2. @atcoder - AGC036E@ ABC String

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个仅由 A, B, C 组成的字符串 S. 求 S 的一个 ...

  3. @atcoder - AGC036D@ Negative Cycle

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个 N 个点的有向带权图,从 0 编号到 N - 1.一开 ...

  4. LRJ-Example-06-01-Uva210

    #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstdlib> #include <cstrin ...

  5. Laravel获取所有的数据库表及结构

    遇到一个需求,需要修改数据库中所有包含email的字段的表,要把里面的长度改为128位.Laravel获取所有的表,然后循环判断表里面有没有email这个字段.代码如下: use Illuminate ...

  6. Laravel 5.6 安装 guzzlehttp

    环境:Laravel 5.6 安装  composer require guzzlehttp/guzzle 在vendor文件夹下,vendor\guzzlehttp\guzzle 引入 use Gu ...

  7. hdu 1255 覆盖的面积 (Bruceforce)

    Problem - 1255 暴力统计覆盖超过一次的区域.1y. 代码如下: #include <cstdio> #include <cstring> #include < ...

  8. 使用基于Apache Spark的随机森林方法预测贷款风险

    使用基于Apache Spark的随机森林方法预测贷款风险   原文:Predicting Loan Credit Risk using Apache Spark Machine Learning R ...

  9. python基础十三之内置函数

    内置函数 操作字符串代码 eval和exec print(eval('1+2')) # 简单的计算 有返回值 exec('for i in range(10):print(i)') # 简单的流程控制 ...

  10. 12174 - Shuffle——[滑动窗口]

    You are listening to your music collection using the shuffle function to keep the music surprising. ...