如何在Java中以字符串格式获取当前时间戳? “yyyy.MM.dd.HH.mm.ss”

时间:2014-04-14 19:32:39

标签: java date datetime timestamp

如何在Java中以字符串格式获取时间戳? " yyyy.MM.dd.HH.mm.ss"

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Timestamp());

这就是我所拥有的,但Timestamp()需要一个参数......

9 个答案:

答案 0 :(得分:173)

替换

new Timestamp();

new java.util.Date()

因为Timestamp没有默认构造函数,或者您可以使用该方法执行此操作:

new Timestamp(System.currentTimeMillis());

答案 1 :(得分:134)

使用java.util.Date类而不是时间戳。

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());

这将以指定的格式为您提供当前日期。

答案 2 :(得分:29)

您可以使用java.util.Date而不是Timestamp:

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());

答案 3 :(得分:23)

TL;博士

仅使用现代 java.time 类。切勿使用可疑的遗留类,例如SimpleDateFormatDatejava.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`. 

java.time & JDBC 4.2

现代方法使用 java.time 类,如上所示。

如果JDBC驱动程序符合JDBC 4.2,则可以直接与数据库交换 java.time 对象。使用PreparedStatement::setObjectResultSet::getObject

仅在JDBC 4.2

之前对驱动程序使用java.sql

如果您的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时间轴上的一个时刻。

java.sql.Timestamp ts = myResultSet.getTimestamp( … );
Instant instant = ts.toInstant(); 

时区

应用所需/预期的时区以获得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 );

这种特殊格式的含义不明确,因为它没有offset-from-UTC或时区的任何迹象。

ISO 8601

如果您对此事有任何发言权,建议您考虑使用标准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.sql

您可以将java.time转换回java.sql.Timestamp。从Instant中提取ZonedDateTime

旧类中添加了新方法,以便于转换为java.time类或从java.time类转换。

java.sql.Timestamp ts = java.sql.Timestamp.from( zdt.toInstant() );

关于 java.time

java.time框架内置于Java 8及更高版本中。这些类取代了麻烦的旧legacy日期时间类,例如java.util.DateCalendar和& SimpleDateFormat

现在位于Joda-Timemaintenance mode项目建议迁移到java.time类。

要了解详情,请参阅Oracle Tutorial。并搜索Stack Overflow以获取许多示例和解释。规范是JSR 310

您可以直接与数据库交换 java.time 对象。使用符合JDBC driver或更高版本的JDBC 4.2。不需要字符串,不需要java.sql.*类。

从哪里获取java.time类?

ThreeTen-Extra项目使用其他类扩展java.time。该项目是未来可能添加到java.time的试验场。您可以在此处找到一些有用的课程,例如IntervalYearWeekYearQuartermore

答案 4 :(得分:6)

更合适的方法是将Locale区域指定为构造函数中的参数。以下示例使用美国区域设置区域。日期格式设置是区域设置敏感的,并使用区域设置来定制与用户所在区域的习惯和惯例相关的信息Locale (Java Platform SE 7)

String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss", Locale.US).format(new Date());

答案 5 :(得分:4)

您可以使用以下

new java.sql.Timestamp(System.currentTimeMillis()).getTime()

Result : 1539594988651

希望这会有所帮助。只是我的建议,而不是奖励积分。

答案 6 :(得分:1)

如果使用Java 8或更高版本,请使用现代的java.time类。

String s = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());

罗勒·布尔克(Basil Bourque)的回答非常好。但这太长了。许多人没有耐心阅读它。前3个答案过时,可能会误导Java新手。因此,我为新开发者提供了这个简短而现代的答案。希望这个答案可以减少可怕的SimpleDateFormat的使用。

答案 7 :(得分:0)

我正在使用这个

String timeStamp = new SimpleDateFormat("dd/MM/yyyy_HH:mm:ss").format(Calendar.getInstance().getTime());
System.out.println(timeStamp);

答案 8 :(得分:-2)

使用以下代码获取当前时间戳:

Timestamp ts = new Timestamp(date.getTime());

供参考

https://www.db-fiddle.com/f/wk344Gt6hm98xEhM4jei92/8