查找当前日期和后天';java中的日期
如何在java中查找当前日期。我发现了很多,但每次我都得到同样的命令 日期d=新日期();或者类似的东西 每个这样的命令都返回1970年的日期。 我不明白,把日期定在1970年有什么好处? 有没有什么方法可以让我得到当前时间并在其中添加一秒。 我的真正目的是将长值转换为日期,并在其中添加第二个值。 5:40:12应该给我5:40:13再加一秒。查找当前日期和后天';java中的日期,java,date,Java,Date,如何在java中查找当前日期。我发现了很多,但每次我都得到同样的命令 日期d=新日期();或者类似的东西 每个这样的命令都返回1970年的日期。 我不明白,把日期定在1970年有什么好处? 有没有什么方法可以让我得到当前时间并在其中添加一秒。 我的真正目的是将长值转换为日期,并在其中添加第二个值。 5:40:12应该给我5:40:13再加一秒。 任何帮助都将不胜感激,因为我厌倦了1970年的日期。Java.Util.date类已被弃用,我建议使用 Java.Util.Calendar inste
任何帮助都将不胜感激,因为我厌倦了1970年的日期。Java.Util.date类已被弃用,我建议使用
Java.Util.Calendar instead.
如果您希望在当前日期后再添加一秒,请尝试以下操作:
Calendar currentTime = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
calendar.add(currentTime.SECOND, 1);
System.out.println(currentTime.getTime());
但是,使用date类时接收1970年日期的原因是该类的工作时间为毫秒,因此必须将long值乘以1000才能将其转换为日期,下面是一个示例
Date currentDate = new Date( YourLongValue * 1000);
不推荐使用Java.Util.Date类,我建议使用
Java.Util.Calendar instead.
如果您希望在当前日期后再添加一秒,请尝试以下操作:
Calendar currentTime = Calendar.getInstance(); // gets a calendar using the default time zone and locale.
calendar.add(currentTime.SECOND, 1);
System.out.println(currentTime.getTime());
但是,使用date类时接收1970年日期的原因是该类的工作时间为毫秒,因此必须将long值乘以1000才能将其转换为日期,下面是一个示例
Date currentDate = new Date( YourLongValue * 1000);
我的真正目的是将长值转换为日期,并在其中添加第二个值。5:40:12应该给我5:40:13再加一秒
麻烦的java.util.Date
类现在是遗留类,被java.time类取代
Instant.ofEpochMilli( yourLongIntegerGoesHere ) // A moment on the timeline in UTC represented a count of nanoseconds since the epoch of `1970-01-01T00:00:00Z`.
.atZone( ZoneId.of( "Pacific/Auckland" ) // Time zone for the region whose wall-clock time you want to see.
.plusSeconds( 1 )
.toLocalTime() // Extract just the time-of-day without date and without time zone.
.toString() // Generate a string representing the time-of-day value in standard ISO 8601 format.
05:40:13
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 该项目专门为Android采用了ThreeTen Backport(如上所述)
- 看
java.util.Date
类现在是遗留类,被java.time类取代
Instant.ofEpochMilli( yourLongIntegerGoesHere ) // A moment on the timeline in UTC represented a count of nanoseconds since the epoch of `1970-01-01T00:00:00Z`.
.atZone( ZoneId.of( "Pacific/Auckland" ) // Time zone for the region whose wall-clock time you want to see.
.plusSeconds( 1 )
.toLocalTime() // Extract just the time-of-day without date and without time zone.
.toString() // Generate a string representing the time-of-day value in standard ISO 8601 format.
05:40:13
关于java.time 该框架内置于Java8及更高版本中。这些类取代了麻烦的旧日期时间类,例如,& 该项目现已启动,建议迁移到类 要了解更多信息,请参阅。并搜索堆栈溢出以获得许多示例和解释。规格是 从哪里获得java.time类
- ,及以后
- 内置的李>
- 标准JavaAPI的一部分,带有捆绑实现
- Java9添加了一些次要功能和修复
- 及
- 大部分java.time功能都在中向后移植到Java6和Java7
-
- 该项目专门为Android采用了ThreeTen Backport(如上所述)
- 看