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;
}
getHours
、getMinutes
和 getSeconds
方法被删除并且不起作用?
我正在尝试将时间戳存储到 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/