现在的时间格式是:
2011-11-25T13:42:00+08:00
我想把它转换为yyyy-MM-dd hh:mm:ss,该怎么处理呢? /**
* String转换为时间
* @param str
* @return
*/
public static Date ParseDate(String str){
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date addTime = null;
try {
addTime = dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return addTime;
}
我现在的是上面的方法,但是会报错,得不到结果。
2011-11-25T13:42:00+08:00
我想把它转换为yyyy-MM-dd hh:mm:ss,该怎么处理呢? /**
* String转换为时间
* @param str
* @return
*/
public static Date ParseDate(String str){
SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Date addTime = null;
try {
addTime = dateFormat.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
return addTime;
}
我现在的是上面的方法,但是会报错,得不到结果。
解决方案 »
- android webview显示问题
- android textview 垂直居中
- 期望一个LinearLayout 会随着时间推移而变长
- 如何实现android实时视频通话功能
- 求助android..CursorIndexOutOfBoundsException: Index 0 requested,with a size of 0
- 求助:sdk目录下的docs目录
- 关于ViewGroup实现拖动效果的问题
- arcgis for android 10.1.1访问 arcgis server10.1发布的缓存地图,加载不到图,只有放到最大层才能加载到图。
- Android中如何把在broadcastreceiver中耗时的任务交给service处理
- 为什么sdk里面啥文件都没啊,我还有什么没设置的吗?下面的文件都是空的。
- git clone下载内核warning: remote HEAD refers to nonexistent ref, unable to checkout
- 让所有的通讯录的姓名以Preference形式显示出来
为何不直接使用date型呢?
这是一种GMT时间格式,RSS格式都是用这种格式
这种格式会在不同的国家显示成不同,比如在中国是2011-11-28T17:59:50+08:00
在美国可能就是2011-11-28T23:59:50-08:00
我想把它转换成本地时间。
public class Test {
public static void main(String[] args){
try {
System.out.println(new Test().getString());
System.out.println(new Test().getValue());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getString() throws ParseException{
String time="2011-11-25T13:42:00+08:00";
int plusIndex=time.indexOf("+");
String date=time.substring(0, plusIndex);
String timeZone=time.substring(plusIndex);
int columnIndex=timeZone.indexOf(":");
String hour=timeZone.substring(0,columnIndex);
String minute=timeZone.substring(columnIndex+1);
DateFormat format=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date temp=format.parse(date);
long tempValue=temp.getTime();
String timezone = "GMT" + hour + ":" + minute; // 计算字符串中的时区与当前时区的时间差
long current_milli = System.currentTimeMillis();
TimeZone zone = TimeZone.getTimeZone(timezone);
long date_gmt_offset = zone.getOffset(current_milli);
long local_gmt_offset = TimeZone.getDefault().getOffset(current_milli); long offset_to_local = local_gmt_offset - date_gmt_offset;
long last=tempValue+offset_to_local;
DateFormat secondFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return secondFormat.format(last);
}
public String getValue() throws ParseException{
String time="2011-11-25T13:42:00+08:00";
int plusIndex=time.indexOf("+");
String date=time.substring(0, plusIndex);
String timeZone=time.substring(plusIndex);
timeZone=timeZone.replace(":", "");
time=date+timeZone;
DateFormat first=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
Date temp=first.parse(time);
DateFormat second=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return second.format(temp);
}
}以上是两种方法,因为琢磨了好多正则表达式,结果试了好几个,没成功,所以只能退而求其次了,采用了分割字符串的方法,更大家更正
dateFormat.format是将时间按固定格式转为字符串输出
所以SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
应该改为SimpleDateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
以匹配你的时间字符串。
不过("yyyy-MM-dd hh:mm:ss"); 我可能写错了,中间还有个T呢