JAVA时间转为格林威治时间
来源:互联网 发布:北京网络职业学院窦店 编辑:程序博客网 时间:2024/03/28 23:38
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class TimeTest {
public static String fromCalendar(final Calendar calendar) {
Date date = calendar.getTime();
String formatted = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss 'GMT'Z (z)", Locale.ENGLISH)
.format(date);
return formatted.substring(0, 22) + "" + formatted.substring(22);
}
/** Get current date and time formatted as ISO 8601 string. */
public static String now() {
return fromCalendar(GregorianCalendar.getInstance());
}
/** Transform ISO 8601 string to Calendar. */
public static Calendar toCalendar(final String iso8601string)
throws ParseException {
Calendar calendar = GregorianCalendar.getInstance();
String s = iso8601string.replace("Z", "+00:00");
try {
s = s.substring(0, 22) + s.substring(23);
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid length", 0);
}
Date date = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss 'GMT'Z (z)", Locale.ENGLISH).parse(s);
calendar.setTime(date);
return calendar;
}
public void test(){
}
public static void main(String args[])throws Exception{
String strTime=fromCalendar(GregorianCalendar.getInstance());
System.out.println(strTime);
Calendar can=toCalendar(strTime);
System.out.println(can.getTime());
}
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
public class TimeTest {
public static String fromCalendar(final Calendar calendar) {
Date date = calendar.getTime();
String formatted = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss 'GMT'Z (z)", Locale.ENGLISH)
.format(date);
return formatted.substring(0, 22) + "" + formatted.substring(22);
}
/** Get current date and time formatted as ISO 8601 string. */
public static String now() {
return fromCalendar(GregorianCalendar.getInstance());
}
/** Transform ISO 8601 string to Calendar. */
public static Calendar toCalendar(final String iso8601string)
throws ParseException {
Calendar calendar = GregorianCalendar.getInstance();
String s = iso8601string.replace("Z", "+00:00");
try {
s = s.substring(0, 22) + s.substring(23);
} catch (IndexOutOfBoundsException e) {
throw new ParseException("Invalid length", 0);
}
Date date = new SimpleDateFormat("EEE MMM dd yyyy hh:mm:ss 'GMT'Z (z)", Locale.ENGLISH).parse(s);
calendar.setTime(date);
return calendar;
}
public void test(){
}
public static void main(String args[])throws Exception{
String strTime=fromCalendar(GregorianCalendar.getInstance());
System.out.println(strTime);
Calendar can=toCalendar(strTime);
System.out.println(can.getTime());
}
}
输出结果:
Fri Aug 08 2014 01:13:45 GMT+0800 (CST)
Fri Aug 08 01:13:05 CST 2014
0 0
- JAVA时间转为格林威治时间
- 格林威治时间如何转为系统时间?
- 时间 格林威治时间
- java android格林威治时间转成本地时间
- android本地时间转成转成格林威治时间 java本地时间转成转成格林威治时间
- java获取给定时区时间,中国标准时.格林威治时间
- 美国洛杉矶时间转 格林威治时间
- java格式化格林威治时间
- js 时间格式转换 格林威治时间
- 时间编程--获取格林威治时间 gmtime
- 格林威治时间
- Java相对格林威治时间的计算
- 将格林威治时间转换为本地时间
- 获得当前时间的格林威治时间毫秒数
- c++ 时间类型详解 time_t 格林威治时间
- 获取本地时间和格林威治时间一点用法
- java如何把时间格式转为毫秒
- java将国际时间转为个时区时间
- Linux kernel学习之进程切换
- ios 记录一些问题的解决方案
- 多个集合做笛卡尔积
- 数据结构与算法-最小生成树—普里姆算法
- 2_转义字符
- JAVA时间转为格林威治时间
- 程序员要知道的英语词汇
- 3_关系和逻辑运算
- 4_自增自减
- Junit could not initialize proxy - no Session (Hibernate Open session view)
- uva 10635 - Prince and Princess
- B2B线上交易为什么这么难?
- 5_普通位运算
- C# .NET VS2010加载Web服务 传参失败