如何在java中转换日期
我是android新手。 我必须将以下日期转换为此时间戳( Wed Oct 12 14:17:42 GMT+05:30 2011
)
Thu, 27 May 2010 12:37:27 GMT
这是我通过标头从服务器获取的日期。 我已将其转换为String对象。 现在我必须将其转换为日期格式,如: Wed Oct 12 14:17:42 GMT+05:30 2011
请问你能帮助我如何使用时间戳将其转换为( Wed Oct 12 14:17:42 GMT+05:30 2011
)这种格式。
看看提供parse()
和format()
方法的DateFormat
或SimpleDateFormat
。 DateFormat
提供了一堆标准格式,而SimpleDateFormat
允许您提供自己的格式表达式。
输入日期示例:
//note that you need the locale as well, since the weekdays and months are written in a specific language, English in that case SimpleDateFormat parseFormat = new SimpleDateFormat( "E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH ); SimpleDateFormat writeFormat = new SimpleDateFormat( "E MMM dd HH:mm:ss z yyyy", Locale.ENGLISH ); writeFormat.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); Date tDate = parseFormat.parse( "Wed, 12 Oct 2011 14:17:42 GMT" ); System.out.println(writeFormat.format(tDate )); //Wed Oct 12 14:17:42 GMT 2011
编辑:如果您想要更有用的API,请尝试JodaTime 。