字符串转换为最新的Andr​​oid [英] Convert string to date Android

查看:166
本文介绍了字符串转换为最新的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换一个String,它重新presents存储SQLite中的日期。

I'm trying to convert a String that represents a date stored in SQLITE.

日期被存储到源码如下:

The date was stored into sqlite as follows:

Date date;

date.toString();

使用Java文档根据的toString()方法:

According with Java documentation, toString() method:

返回一个字符串,再此日期presentation。该格式是
  相当于用一个SimpleDateFormat的与格式字符串EEE MMM
  DD HH:MM:SS ZZZ YYYY,它看起来像周二6月22日13时07分00秒
  1999年PDT当前的默认时区和语言环境使用。如果你
  需要对时区或区域控制,使用的SimpleDateFormat
  代替。

Returns a string representation of this Date. The formatting is equivalent to using a SimpleDateFormat with the format string "EEE MMM dd HH:mm:ss zzz yyyy", which looks something like "Tue Jun 22 13:07:00 PDT 1999". The current default time zone and locale are used. If you need control over the time zone or locale, use SimpleDateFormat instead.

直到这里,这很好,但是,当我试图让字符串,并将其再次转换到今天为止,Java的抛出异常。

Until here, it's fine but, when I try to get the String and convert it to date again, Java throws an exception.

弦乐来自sqlite的:

The String comes from sqlite:

Mon Jan 20 18:26:25 BRT 2014

所以,我做的:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

Date date= sdf.parse("Mon Jan 20 18:26:25 BRT 2014");

我在做什么错了?

What I'm doing wrong?

感谢。

推荐答案

试试这个:

String w = "Mon Jan 20 18:26:25 BRT 2014";
SimpleDateFormat pre = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
try{
    Date date = pre.parse(w);
    System.out.println(sdf.format(date));
}catch(Exception e){
    e.printStackTrace();
}

输出:

20/01/2014

这篇关于字符串转换为最新的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆