How to format a java.sql Timestamp for displaying?

前端 未结 7 1864
借酒劲吻你
借酒劲吻你 2020-12-12 20:13

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

7条回答
  •  温柔的废话
    2020-12-12 20:48

    Use String.format (or 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

    The first 'T' stands for:

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

    and the character following that 'T':

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

提交回复
热议问题