找到 4330 篇文章 关于 Java 8
894 次浏览
设置两个 Java 日期:LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29);现在,使用 Period 类中的 between() 方法获取两个日期之间的差值:Period p = Period.between(date1, date2);现在,获取年、月和日:p.getYears() p.getMonths() p.getDays()示例import java.time.LocalDate; import java.time.Period; public class Demo { public static void main(String[] args) { LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29); System.out.println("Date 1 = "+date1); System.out.println("Date 2 = "+date2); Period p = Period.between(date1, date2); System.out.println("Period = "+p); ... 阅读更多
211 次浏览
让我们首先设置一个 Instant:Instant now = Instant.ofEpochMilli(184142540078l);现在让我们从 Instant 中减去秒:Instant resSeconds = now.minusSeconds(50);现在让我们从 Instant 中减去纳秒:Instant resNanoSeconds = now.minusNanos(10000);示例import java.time.Instant; public class Demo { public static void main(String[] args) { Instant now = Instant.ofEpochMilli(184142540078l); System.out.println(now); Instant resSeconds = now.minusSeconds(50); System.out.println("减去秒后 = "+resSeconds); Instant resNanoSeconds = now.minusNanos(10000); System.out.println("减去纳秒后 = "+resNanoSeconds); } }输出1975-11-02T06:42:20.078Z 减去秒后 = 1975-11-02T06:41:30.078Z 减去纳秒后 = 1975-11-02T06:42:20.077990Z
3K+ 次浏览
以下是两个 Instant 时间戳:Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935);获取两个 Instant 之间的 Duration:Duration res = Duration.between(one, two);现在,获取两个时间戳之间的秒数:long seconds = res.getSeconds();现在,获取两个时间戳之间的分钟数:long minutes = res.abs().toMinutes();示例import java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant one = Instant.ofEpochSecond(1355836728); Instant two = Instant.ofEpochSecond(1355866935); Duration res = Duration.between(one, two); System.out.println(res); long seconds = res.getSeconds(); System.out.println("Duration 之间的秒数 = "+seconds); ... 阅读更多
19K+ 次浏览
让我们首先使用 ofEpochSeconds() 方法设置两个 Instant,使用自 1970-01-01T00:00:00Z 纪元以来的秒数。现在获取上述两个 Instant 之间的持续时间:Duration res = Duration.between(one, two);示例import java.time.Duration; import java.time.Instant; public class Demo { public static void main(String[] args) { Instant one = Instant.ofEpochSecond(1845836728); Instant two = Instant.ofEpochSecond(1845866935); Duration res = Duration.between(one, two); System.out.println(res); } }输出PT8H23M27S
191 次浏览
创建一个 Instant 并使用作为参数传递的自 1970-01-01T00:00:00Z 纪元以来的毫秒数获取 Instant。这是使用 ofEpochMilli() 完成的:Instant instant = Instant.ofEpochMilli(342627282920l);现在,从 Instant 获取纪元毫秒:instant. toEpochMilli();示例import java.time.Instant; public class Demo { public static void main(String[] args) { Instant instant = Instant.ofEpochMilli(1262347200000l); long res = instant.toEpochMilli(); System.out.println(res); } }输出1262347200000
193 次浏览
创建一个 Instant 并使用作为参数传递的自 1970-01-01T00:00:00Z 纪元以来的毫秒数获取 Instant。这是使用 ofEpochMilli() 完成的:Instant instant = Instant.ofEpochMilli(342627282920l);现在,从 Instant 获取纪元秒:instant.getEpochSecond();示例import java.time.Instant; public class Demo { public static void main(String[] args) { Instant instant = Instant.ofEpochMilli(342627282920l); long res = instant.getEpochSecond(); System.out.println(res); } }输出342627282
1K+ 次浏览
从纪元秒创建 Instant示例import java.time.Instant; public class Demo { public static void main(String[] args) { Instant instant = Instant.ofEpochSecond(282829279); System.out.println(instant); } }输出1978-12-18T11:41:19Z从纪元毫秒创建 Instant示例import java.time.Instant; public class Demo { public static void main(String[] args) { Instant instant = Instant.ofEpochMilli(272827282728l); System.out.println(instant); } }输出1978-08-24T17:21:22.728Z
123 次浏览
通过此,获取天、小时和分钟的纳秒数。首先,设置 Duration:Duration d1 = Duration.ofDays(5); Duration d2 = Duration.ofHours(20); Duration d3 = Duration.ofMinutes(15);将上述 Duration 转换为纳秒:System.out.println("5 天的纳秒数 = "+d1.toNanos()); System.out.println("20 小时的纳秒数 = "+d2.toNanos()); System.out.println("15 分钟的纳秒数 = "+d3.toNanos());示例import java.time.Duration; public class Demo { public static void main(String[] args) { Duration d1 = Duration.ofDays(5); Duration d2 = Duration.ofHours(20); Duration d3 = Duration.ofMinutes(15); System.out.println("5 天的纳秒数 = "+d1.toNanos()); System.out.println("20 小时的纳秒数 = "+d2.toNanos()); ... 阅读更多
202 次浏览
在本文中,我们使用 Java 计算给定持续时间中的分钟数。Duration 类可以执行基于时间计算,如小时、分钟、天等,并且可以轻松地在这些单位之间转换。使用 toMinutes() 方法,您可以从给定的 Duration 获取分钟数。问题陈述给定一个表示一段时间段的 Duration 对象,编写一个 Java 程序来计算指定持续时间中的分钟数。输入 Duration = 25 天,10 小时输出 25 天中的分钟数 = 3600010 小时中的分钟数 = 600 获取分钟数的步骤 ... 阅读更多