java - getMinutes、getHour 和 getSeconds 被删除了吗?

标签 java time timestamp datetime-format

public Timestamp Timestamp() {
    int hours = new Timestamp(System.currentTimeMillis()).getHours();
    int minutes = new Timestamp(System.currentTimeMillis()).getMinutes();
    int seconds = new Timestamp(System.currentTimeMillis()).getSeconds();

    String activity = "\n" + minutes + ":" + hours + ":" + seconds;

    return null;
}

getHoursgetMinutesgetSeconds 方法被删除并且不起作用?

我正在尝试将时间戳存储到 txt 文件中,并在其他地方调用它来创建 Activity 日志。

最佳答案

当您使用System.currentTimeMillis()时,我假设这是Java。 “删除”,您的意思是您的 IDE 中的方法名称有删除线吗?

如果是这种情况,则意味着这些方法已被弃用。实际上,Timestamp inherits those methods from Date ,这些是 deprecated since Java 1.1 .

如果您希望将当天的当前时间格式化为String,我建议您使用new date/time API 。在 Java 8 及更高版本中,这些是 native 的,位于 java.time 包中。对于较低版本,您可以使用Threeten Backport ,并且相同的类将在 org. Threeten.bp 包中提供:

// current time
LocalTime now = LocalTime.now();
// formatter (hours:minutes:seconds)
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("HH:mm:ss");
// format the LocalTime to String        
String activity = now.format(fmt);

我使用了一种格式HH:mm:ss,这意味着“小时:分钟:秒”(与您使用的顺序不同:“分钟:小时:秒”),并且还包含 2数字(因此“9”变为“09”)。如果这不是您需要的确切格式,check in the javadoc如何获得不同的格式。

关于java - getMinutes、getHour 和 getSeconds 被删除了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49309414/

相关文章:

Java:如何将布局管理器中的按钮居中并在调整大小时将其重新调整到中心?

java - org.hibernate.AssertionFailure : possible non-threadsafe access to the session

java - 使用 LittleEndian 将短裤数组写入文件的好方法

c - 从 TP-Link 722N 获取 rx/tx 时间戳

java - 如何使用postman调用REST API进行azure文件存储?

javascript - 标准 JavaScript 中的日期格式

java - 没有秒的解析即时

php - PHP 中 UNIX 的 ATOM 日期格式

nhibernate - NHibernate 中的 DateTime 精度和 NHibernate SchemeExport 中对 DateTime2 的支持

java - oracle jdbc中的PreparedStatement和setTimestamp