java格林尼治时间等转成常规日期类型字符串
来源:互联网 发布:网络舆情危机 编辑:程序博客网 时间:2024/05/17 23:38
private Date formatDate(String string) {SimpleDateFormat resultSdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");SimpleDateFormat resultSdfdate = new SimpleDateFormat("yyyy-MM-dd");if (string != null) {if (string.contains("CST")) {long d2 = Date.parse(string);Date datetime = new Date(d2);return datetime;} else if (string.contains("Z")) {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'.'sss'Z'");java.util.Date datetime;try {datetime = sdf.parse(string);return (Date) datetime;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else if (string.contains("-")&&string.contains(":")) {Date newDate;try {newDate = resultSdf.parse(string);return newDate;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} else if(string.contains("-")&&!string.contains(":")){Date newDate;try {newDate = resultSdfdate.parse(string);return newDate;} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}}else {Date longDate = new Date(Long.parseLong(string));return longDate;}}return null;}
上述方法可以转如下格式:
2014-06-27T08:11:07.168Z
Fri Aug 15 08:00:37 CST 2014
1404359095333
2014-08-29
Mask Description
Uppercase M unlike CF timeFormat's m to avoid conflict with months.MMMinutes; leading zero for single-digit minutes.
Uppercase MM unlike CF timeFormat's mm to avoid conflict with months.sSeconds; no leading zero for single-digit seconds.ssSeconds; leading zero for single-digit seconds.l or LMilliseconds. l gives 3 digits. L gives 2 digits.tLowercase, single-character time marker string: a or p.
No equivalent in CF.ttLowercase, two-character time marker string: am or pm.
No equivalent in CF.TUppercase, single-character time marker string: A or P.
Uppercase T unlike CF's t to allow for user-specified casing.TTUppercase, two-character time marker string: AM or PM.
Uppercase TT unlike CF's tt to allow for user-specified casing.ZUS timezone abbreviation, e.g. EST or MDT. With non-US timezones or in the Opera browser, the GMT/UTC offset is returned, e.g.GMT-0500
No equivalent in CF.oGMT/UTC timezone offset, e.g. -0500 or +0230.
No equivalent in CF.SThe date's ordinal suffix (st, nd, rd, or th). Works well with d.
No equivalent in CF.'…' or "…"Literal character sequence. Surrounding quotes are removed.
No equivalent in CF.UTC:Must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The “UTC:” prefix is removed.
No equivalent in CF.
格林尼治时间转化详解:
首先 Mon Dec 09 22:06:24 格林尼治标准时间+0800 2013 字段一个格林尼治标准时间时间,一般情况下字段中不会含有中文,对于这种格式有两种解决方法
1剔除中文字符串
public static String convertGMTToLoacale(String gmt){
String cc = gmt.substring(0, 19) + gmt.substring(33, 38);
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss yyyy",new Locale("English"));
try {
Date date = sdf.parse(cc);
SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM");
String result = dateformat.format(date);
return result;
} catch (ParseException e) {
}
return "";
}
2.第二种方法是在不进行字符串剔除的情况下:
在simpleDateFormat方法中将格式字符串变换为:"EEE MMM dd HH:mm:ss 格林尼治标准时间+0800 yyyy" 就可以了。这样就可一将时间转换为Date类型:
private DateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss 格林尼治标准时间+0800 yyyy",Locale.ENGLISH);
3.第三种方法:
java.text.SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy",Locale.US);
System.out.println(sdf.format(new Date()));
Date d;
try {
d = sdf.parse(str);
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(d));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date datetime=new Date(d2);
System.out.println(datetime.toLocaleString());
- java格林尼治时间等转成常规日期类型字符串
- 将String类型的日期字符串转成 java.sql.date 日期格式
- java中String类型的日期转成Date类型
- JAVA取两个日期之间日期转成字符串_hanCSDN_20130426
- .net字符串转成日期
- 字符串函数,日期函数,类型转换等
- JAVA取前N天的日期转成字符串_hanCSDN_20130425
- java中如何能把一个字符串转成日期对象
- js中将字符串转成日期
- 字符串转成日期的函数
- java MongoDB 字符串日期类型查询和日期类型查询
- java将二进制字符串转成int类型数据
- java中字符串日期类等处理
- java字符串日期类型转换成日期类型(日期类型相加、日期类型间隔天数)
- 字符串格式日期转成日期格式
- 把字符串转成number类型
- oracle字符串转成date类型
- python time 和datetime类型转换,字符串型变量转成日期型变量
- 服务器反推技术
- sqlplus "/as sysdba" TNS:net service name is incorrectly specified
- Python使用smtplib发送邮件
- cocos2dx sqlite第三方库的资源保存和调用
- Linux CentOS安装SDL、ffmpeg
- java格林尼治时间等转成常规日期类型字符串
- C#中,关闭程序时,要求弹出对话框询问用户是否保存
- 纪念困扰我两天的问题Brute-Force算法
- Opencv中Mat图的长、宽、行、列以及图像像素
- Git 常用命令整理
- 华为usg2220防火墙配置
- WebService工作原理
- 解决 “fatal error C1083: ”无法打开包括文件
- linux终端窗口可以切屏——tmux利器之常用命令