![](/img/trans.png)
[英]How to format a java.sql.Timestamp(yyyy-MM-dd HH:mm:ss.S) to a date(yyyy-MM-dd HH:mm:ss)
[英]How to get current timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"
如何在Java中以字符串格式獲取時間戳? “yyyy.MM.dd.HH.mm.ss”
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp());
這就是我所擁有的,但 Timestamp() 需要一個參數......
代替
new Timestamp();
和
new java.util.Date()
因為Timestamp
沒有默認構造函數,或者您可以使用以下方法:
new Timestamp(System.currentTimeMillis());
使用java.util.Date
類而不是 Timestamp。
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
這將為您提供指定格式的當前日期。
僅使用現代java.time類。 永遠不要使用像SimpleDateFormat
、 Date
或java.sql.Timestamp
這樣糟糕的遺留類。
ZonedDateTime // Represent a moment as perceived in the wall-clock time used by the people of a particular region ( a time zone).
.now( // Capture the current moment.
ZoneId.of( "Africa/Tunis" ) // Specify the time zone using proper Continent/Region name. Never use 3-4 character pseudo-zones such as PDT, EST, IST.
) // Returns a `ZonedDateTime` object.
.format( // Generate a `String` object containing text representing the value of our date-time object.
DateTimeFormatter.ofPattern( "uuuu.MM.dd.HH.mm.ss" )
) // Returns a `String`.
或者使用JVM當前的默認時區。
ZonedDateTime
.now( ZoneId.systemDefault() )
.format( DateTimeFormatter.ofPattern( "uuuu.MM.dd.HH.mm.ss" ) )
現代方法使用如上所示的java.time類。
如果您的JDBC 驅動程序符合JDBC 4.2 ,您可以直接與數據庫交換java.time對象。 使用PreparedStatement::setObject
和ResultSet::getObject
。
如果您的 JDBC 驅動程序尚不符合 JDBC 4.2 以支持java.time類型,則必須回退到使用 java.sql 類。
OffsetDateTime odt = OffsetDateTime.now( ZoneOffset.UTC ) ; // Capture the current moment in UTC.
myPreparedStatement.setObject( … , odt ) ;
OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;
java.sql 類型,例如java.sql.Timestamp
,應該只用於傳入和傳出數據庫。 在 Java 8 及更高版本中立即轉換為 java.time 類型。
java.time.Instant
java.sql.Timestamp
映射到java.time.Instant
,即 UTC 時間線上的時刻。 注意添加到舊類的新轉換方法toInstant
。
java.sql.Timestamp ts = myResultSet.getTimestamp( … );
Instant instant = ts.toInstant();
應用所需/預期的時區 ( ZoneId
) 以獲得ZonedDateTime
。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.ofInstant( instant , zoneId );
使用DateTimeFormatter
生成您的字符串。 模式代碼與java.text.SimpleDateFormat
的代碼相似,但不完全一樣,因此請仔細閱讀文檔。
DateTimeFormatter formatter = DateTimeFormatter.ofPattern( "uuuu.MM.dd.HH.mm.ss" );
String output = zdt.format( formatter );
這種特定格式的確切含義是模棱兩可的,因為它沒有任何與 UTC 或時區偏移的指示。
如果您對此事有任何發言權,我建議您考慮使用標准ISO 8601格式,而不是自己滾動。 標准格式與您的非常相似。 例如:
2016-02-20T03:26:32+05:30
。
java.time 類默認使用這些標准格式,因此無需指定模式。 ZonedDateTime
類通過附加時區名稱擴展了標准格式(明智的改進)。
String output = zdt.toString(); // Example: 2007-12-03T10:15:30+01:00[Europe/Paris]
您可以從 java.time 轉換回java.sql.Timestamp
。 從ZonedDateTime
中提取一個Instant
。
新方法已添加到舊類中,以促進與 java.time 類之間的轉換。
java.sql.Timestamp ts = java.sql.Timestamp.from( zdt.toInstant() );
java.time框架內置於 Java 8 及更高版本中。 這些類取代了麻煩的舊日期時間類,例如java.util.Date
、 Calendar
和SimpleDateFormat
。
現在處於維護模式的Joda-Time項目建議遷移到java.time類。
要了解更多信息,請參閱Oracle 教程。 並在 Stack Overflow 上搜索許多示例和解釋。 規范是JSR 310 。
您可以直接與您的數據庫交換java.time對象。 使用符合JDBC 4.2或更高版本的JDBC 驅動程序。 不需要字符串,不需要java.sql.*
類。
從哪里獲得 java.time 類?
ThreeTen-Extra項目通過附加類擴展了 java.time。 該項目是未來可能添加到 java.time 的試驗場。 您可以在這里找到一些有用的類,例如Interval
、 YearWeek
、 YearQuarter
等等。
您可以使用 java.util.Date 而不是 Timestamp :
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
如果您使用 java 8 或更新版本,請使用現代java.time
類。
String s = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
Basil Bourque 的回答非常好。 但是太長了。 很多人都沒有耐心讀下去。 前 3 個答案太老了,可能會誤導 Java 新手。所以我為新來的開發人員提供了這個簡短而現代的答案。 希望這個答案可以減少可怕的SimpleDateFormat
的使用。
您可以使用以下內容:
new java.sql.Timestamp(System.currentTimeMillis()).getTime()
結果:
1539594988651
更合適的方法是在構造函數中指定一個 Locale 區域作為參數。 下面的示例使用美國語言環境區域。 日期格式對區域設置敏感,並使用區域設置來定制與用戶區域區域設置(Java 平台 SE 7)的習俗和約定相關的信息
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss", Locale.US).format(new Date());
我正在使用這個
String timeStamp = new SimpleDateFormat("dd/MM/yyyy_HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println(timeStamp);
如果你的日期就像 let mydate = "2022-15-06";
讓 newDate = Date.now(mydate)
現在在 newDate 你有當前的時間戳
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.