java中如何转换日期

发布于 2024-12-11 07:54:33 字数 328 浏览 0 评论 0原文

我是安卓新手。 我必须将以下日期转换为这个时间戳(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) 这种格式使用时间戳。

I am new in android .
I have to convert following date into this time stamp (Wed Oct 12 14:17:42 GMT+05:30 2011)

Thu, 27 May 2010 12:37:27 GMT

This is the date that I am getting from the server through the header. I have converted it into the String object. Now I have to convert it into the Date format like: Wed Oct 12 14:17:42 GMT+05:30 2011

Please could you help me how should I convert it into the (Wed Oct 12 14:17:42 GMT+05:30 2011) this format using timestamp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘴硬脾气大 2024-12-18 07:54:33

查看提供 parse()format() 方法的 DateFormatSimpleDateFormatDateFormat 提供了一系列标准格式,而 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

Have a look at DateFormat or SimpleDateFormat which provide parse() and format() methods. DateFormat provides a bunch of standard formats whereas SimpleDateFormat allows you to provide your own format expression.

Example for your input date:

//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

Edit: If you want a more usable API, try JodaTime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文