获取当前时间的Java代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/833768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java code for getting current time
提问by
I am searching code in java
for fetching or synchronizing my local PC system time into my application.
我正在搜索代码以java
将我的本地 PC 系统时间提取或同步到我的应用程序中。
回答by Raibaz
Not really sure about what you meant, but you probably just need
不太确定你的意思,但你可能只需要
Date d = new Date();
回答by sleske
Both
两个都
new java.util.Date()
new java.util.Date()
and
和
System.currentTimeMillis()
System.currentTimeMillis()
will give you current system time.
会给你当前的系统时间。
回答by Nuno Furtado
Like said above you can use
如上所述,您可以使用
Date d = new Date();
or use
或使用
Calendar.getInstance();
or if you want it in millis
或者如果你想以毫秒为单位
System.currentTimeMillis()
回答by Alex
To get system time use Calendar.getInstance().getTime()
要获取系统时间,请使用 Calendar.getInstance().getTime()
And you should get the new instance of Calendar each time to have currenttime.
并且您应该每次都获得 Calendar 的新实例以获取当前时间。
To changesystem time from java code you can use a command line
要从Java 代码更改系统时间,您可以使用命令行
回答by Roman
You can use new Date () and it'll give you current time.
您可以使用 new Date () ,它会给您当前时间。
If you need to represent it in some format (and usually you need to do it) then use formatters.
如果您需要以某种格式表示它(通常您需要这样做),请使用格式化程序。
DateFormat df = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM, new Locale ("en", "EN"));
String formattedDate = df.format (new Date ());
回答by mP.
System.currentTimeMillis()
System.currentTimeMillis()
everything else works off that.. eg new Date() calls System.currentTimeMillis().
其他一切都可以解决这个问题……例如 new Date() 调用 System.currentTimeMillis()。
回答by Artur
Try this:
尝试这个:
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class currentTime {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println( sdf.format(cal.getTime()) );
}
}
You can format SimpleDateFormat in the way you like. For any additional information you can look in java api:
您可以按照自己喜欢的方式格式化 SimpleDateFormat。有关任何其他信息,您可以查看 java api:
回答by mini-me
new Date().getTime() is bugged.
new Date().getTime() 被窃听。
Date date = new Date();
System.out.println(date);
System.out.println(date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds());
long t1 = date.getTime();
System.out.println((t1 / 1000 / 60 / 60) % 24 + ":" + (t1 / 1000 / 60) % 60 + ":" + (t1 / 1000) % 60);
long t2 = System.currentTimeMillis();
System.out.println((t2 / 1000 / 60 / 60) % 24 + ":" + (t2 / 1000 / 60) % 60 + ":" + (t2 / 1000) % 60);
It returns me the wrong time millis. System.currentTimeMillis()
too. Since I ask the Date instanceto tell me the corresponding time millis it must return the matching ones not others from a different time zone. Funny how deprecated methods are the only ones which return correct values.
它返回给我错误的时间毫秒。System.currentTimeMillis()
也。由于我要求 Date实例告诉我相应的时间毫秒,因此它必须返回匹配的时间,而不是来自不同时区的其他时间。有趣的是,弃用的方法是唯一返回正确值的方法。
回答by Adi
I understand this is quite an old question. But would like to clarify that:
我知道这是一个很老的问题。但要澄清的是:
Date d = new Date()
is depriciated in the current versions of Java. The recommended way is using a calendar object. For eg:
在当前版本的 Java 中被贬低。推荐的方法是使用日历对象。例如:
Calendar cal = Calendar.getInstance();
Date currentTime = cal.getTime();
I hope this will help people who may refer this question in future. Thank you all.
我希望这会对将来可能会提及此问题的人有所帮助。谢谢你们。
回答by Basil Bourque
tl;dr
tl;博士
Instant.now() // UTC
…or…
…或者…
ZonedDateTime.now(
// Specify time zone.
ZoneId.of( "Pacific/Auckland" )
)
Details
细节
The bundled java.util.Date
/.Calendar
classes are notoriously troublesome. Avoid them. They are now legacy, supplanted by the java.timeframework.
捆绑的java.util.Date
/.Calendar
类是出了名的麻烦。避开它们。它们现在是遗留的,被java.time框架取代。
Instead, use either:
相反,请使用:
- java.time
Built-in with Java 8 and later. Official successor to Joda-Time.
Back-ported to Java 6 & 7and to Android. - Joda-Time
Third-party library, open-source, free-of-cost.
- java.time
内置于 Java 8 及更高版本。Joda-Time 的正式继任者。
向后移植到 Java 6 & 7和Android。 - Joda-Time
第三方库,开源,免费。
java.time
时间
ZonedDateTime zdt = ZonedDateTime.now();
If needed for old code, convert to java.util.Date. Go through at Instant
which is a moment on the timeline in UTC.
如果旧代码需要,请转换为 java.util.Date。通过UTCInstant
时间轴上的某个时刻。
java.util.Date date = java.util.Date.from( zdt.toInstant() );
Time Zone
时区
Better to specify explicitly your desired/expected time zonerather than rely implicitly on the JVM's current default time zone.
最好明确指定您想要/预期的时区,而不是隐式依赖 JVM 的当前默认时区。
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = ZonedDateTime.now( zoneId ); // Pass desired/expected time zone.
Joda-Time
乔达时间
FYI, the Joda-Timeproject is now in maintenance mode, with the team advising migration to the java.timeclasses.
仅供参考,Joda-Time项目现在处于维护模式,团队建议迁移到java.time类。
DateTime now = DateTime.now();
To convert from a Joda-Time DateTimeobject to a java.util.Date for inter-operating with other classes…
要将 Joda-Time DateTime对象转换为 java.util.Date 以便与其他类进行互操作......
java.util.Date date = now.toDate();
Search StackOverflow before posting. Your question has already been asked and answered.
发帖前搜索 StackOverflow。您的问题已经有人提出并得到解答。
About java.time
关于 java.time
The java.timeframework is built into Java 8 and later. These classes supplant the troublesome old legacydate-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
该java.time框架是建立在Java 8和更高版本。这些类取代了麻烦的旧的遗留日期时间类,例如java.util.Date
, Calendar
, & SimpleDateFormat
。
The Joda-Timeproject, now in maintenance mode, advises migration to the java.timeclasses.
现在处于维护模式的Joda-Time项目建议迁移到java.time类。
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
要了解更多信息,请参阅Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范是JSR 310。
Where to obtain the java.time classes?
从哪里获得 java.time 类?
- Java SE 8, Java SE 9, and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6and Java SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- Later versions of Android bundle implementations of the java.time classes.
- For earlier Android, the ThreeTenABPproject adapts ThreeTen-Backport(mentioned above). See How to use ThreeTenABP….
- Java SE 8、Java SE 9及更高版本
- 内置。
- 具有捆绑实现的标准 Java API 的一部分。
- Java 9 添加了一些小功能和修复。
- Java SE 6和Java SE 7
- 多的java.time功能后移植到Java 6和7在ThreeTen-反向移植。
- 安卓
- 更高版本的 Android 捆绑实现 java.time 类。
- 对于早期的 Android,ThreeTenABP项目采用了ThreeTen-Backport(上面提到过)。请参阅如何使用ThreeTenABP ...。
The ThreeTen-Extraproject extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
该ThreeTen-额外项目与其他类扩展java.time。该项目是未来可能添加到 java.time 的试验场。你可能在这里找到一些有用的类,比如Interval
,YearWeek
,YearQuarter
,和更多。