将MySql DateTime类型转换为更友好的东西 [英] Converting MySql DateTime type into something more friendly

查看:125
本文介绍了将MySql DateTime类型转换为更友好的东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java Web应用程序,该应用程序读取MySql数据库并返回DateTime字段.将结果集中返回的DateTime字段转换为更具可读性的最佳方法是什么?

I have a Java web app that reads a MySql db and returns DateTime fields. What is the best way to convert the DateTime fields returned in the resultset into something more readable?

当前的DateTime字段显示为:

Currently the DateTime fields print as:

2008-12-14 16:30:00

2008-12-14 16:30:00

但是我想要更用户友好的东西:

but I would like something more user friendly like:

2008年12月14日16:30

14 Dec 2008 at 16:30

我正在从pojo中用这些dateTimes填充ArrayList.我想先将它们格式化,然后再添加到arrayList中,这样我就可以在JSP中打印arrayList的内容了.

I am populating an ArrayList with these dateTimes from a pojo. I would like to format them before adding to the arrayList, so then I can just print the contents of the arrayList in the JSP.

推荐答案

另一种选择是使用JSTL.格式库可以轻松显示任何格式的日期,并且它可以识别i18n.优点是您可以将日期保留为Date对象,并以此进行操作,但仅在需要显示时才将其转换.格式标签如下所示:

Another option is to use the JSTL. The formatting library makes it easy to display a date in any format and it is i18n aware. The advantage is that you can leave the date as a Date object and manipulate it as such but only convert it when you need to display. The format tag will look like this:

<fmt:formatDate value="${myDate}" dateStyle="MEDIUM"/>

就像我上面说的那样,一大优势是它可以识别i18n,因此您可以以本地化格式显示日期.

Like I said above, one big advantage is that it is i18n aware so you can display the date in a localized format.

完整语法为:

 <fmt:formatDate value="expression" 
     timeZone="expression"
     type="field" dateStyle="style" 
     timeStyle="style"
     pattern="expression"
     var="name" scope="scope"/>

这篇关于将MySql DateTime类型转换为更友好的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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