如何格式化用于显示的 java.sql 时间戳?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1156468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:20:18  来源:igfitidea点击:

How to format a java.sql Timestamp for displaying?

javadatetimeformatting

提问by nos

How do I formate a java.sql Timestamp to my liking ? ( to a string, for display purposes)

我如何根据自己的喜好格式化 java.sql 时间戳?(到一个字符串,用于显示目的)

采纳答案by ChssPly76

java.sql.Timestampextends java.util.Date. You can do:

java.sql.Timestamp延伸java.util.Date。你可以做:

String s = new SimpleDateFormat("MM/dd/yyyy").format(myTimestamp);

Or to also include time:

或者还包括时间:

String s = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(myTimestamp);

回答by DreadPirateShawn

If you're using MySQL and want the database itself to perform the conversion, use this:

如果您使用 MySQL 并希望数据库本身执行转换,请使用以下命令:

DATE_FORMAT(date,format)

DATE_FORMAT(日期,格式)

If you prefer to format using Java, use this:

如果您更喜欢使用 Java 进行格式化,请使用以下命令:

java.text.SimpleDateFormat

java.text.SimpleDateFormat

SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy");
dateFormat.format( new Date() );

回答by erickson

Use a DateFormat. In an internationalized application, use the format provide by getInstance. If you want to explicitly control the format, create a new SimpleDateFormatyourself.

使用一个DateFormat. 在国际化应用程序中,使用由getInstance. 如果要明确控制格式,请SimpleDateFormat自己创建一个新格式。

回答by Brian Agnew

For this particular question, the standard suggestion of java.text.SimpleDateFormatworks, but has the unfortunate side effect that SimpleDateFormatis not thread-safeand can be the source of particularly nasty problems since it'll corrupt your output in multi-threaded scenarios, and you won't get any exceptions!

对于这个特定的问题,标准的建议是java.text.SimpleDateFormat有效的,但有一个不幸的副作用,SimpleDateFormat它不是线程安全的,并且可能是特别令人讨厌的问题的根源,因为它会在多线程场景中破坏您的输出,而您不会得到任何例外!

I would stronglyrecommend looking at Jodafor anything like this. Why ? It's a much richer and more intuitive time/date library for Java than the current library (and the basis of the up-and-coming new standard Java date/time library, so you'll be learning a soon-to-be-standard API).

强烈建议您查看Joda以获取此类信息。为什么 ?它是一个比当前库更丰富、更直观的 Java 时间/日期库(也是即将到来的新标准 Java 日期/时间库的基础,因此您将学习一个即将成为标准的库API)。

回答by user85421

Use String.format (or java.util.Formatter):

使用 String.format (或java.util.Formatter):

Timestamp timestamp = ...
String.format("%1$TD %1$TT", timestamp)

EDIT:
please see the documentation of Formatter to know what TD and TT means: click on java.util.Formatter

编辑:
请参阅格式化程序的文档以了解 TD 和 TT 的含义:单击java.util.Formatter

The first 'T' stands for:

第一个“T”代表:

't', 'T'    date/time   Prefix for date and time conversion characters.

and the character following that 'T':

以及“T”后面的字符:

'T'     Time formatted for the 24-hour clock as "%tH:%tM:%tS".
'D'     Date formatted as "%tm/%td/%ty". 

回答by 1909

String timeFrSSHStr = timeFrSSH.toString();

String timeFrSSHStr = timeFrSSH.toString();