Java中的格式化日期 [英] Format date in Java

查看:117
本文介绍了Java中的格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下字符串:

Mon Sep 14 15:24:40 UTC 2009

我需要将其格式化为如下字符串:

I need to format it into a string like this:

14/9/2009

我如何用Java做到这一点?

How do I do it in Java?

推荐答案

使用 SimpleDateFormat (单击javadoc链接查看模式)将一个模式中的字符串解析为一个完整的< a href =http://download.oracle.com/javase/6/docs/api/java/util/Date.html =nofollow> 日期 并使用另一个格式将已解析的日期格式化为另一种模式的字符串。

Use SimpleDateFormat (click the javadoc link to see patterns) to parse the string in one pattern to a fullworthy Date and use another one to format the parsed Date to a string in another pattern.

String string1 = "Mon Sep 14 15:24:40 UTC 2009";
Date date = new SimpleDateFormat("EEE MMM d HH:mm:ss Z yyyy").parse(string1);
String string2 = new SimpleDateFormat("d/M/yyyy").format(date);
System.out.println(string2); // 14/9/2009

这篇关于Java中的格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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