找到关于编程的34423 篇文章

如何在Java中检查给定日期是否为周末

Nancy Den
更新于 2019年7月30日 22:30:25

1K+ 次浏览

首先,显示当前日期:LocalDate date = LocalDate.now(); 现在,从上面的日期(当前日期)获取星期几:DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK)); 基于以上结果,使用SWITCH检查当前日期。如果日期是星期六/星期天,则为周末。示例import java.time.DayOfWeek; import java.time.temporal.ChronoField; import java.time.LocalDate; public class Demo {    public static void main(String[] argv) {       LocalDate date = LocalDate.now();       DayOfWeek day = DayOfWeek.of(date.get(ChronoField.DAY_OF_WEEK));       switch (day) {          case SATURDAY:             System.out.println("Weekend - Saturday");         ... 阅读更多

如何在Java中获取两个LocalDate之间的天、月和年?

Nancy Den
更新于 2019年7月30日 22:30:25

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);   ... 阅读更多

Java程序:向LocalDate添加时间段

Nancy Den
更新于 2024年11月8日 22:29:57

455 次浏览

在这个程序中,我们将学习如何使用Java的LocalDate类向日期添加特定时间段。 通过使用Java的Period类,我们可以指定一段时间(例如月份和天数),然后将此时间段添加到LocalDate。该程序演示了如何设置时间段并将其应用于给定日期以获得新的更新日期。 向LocalDate添加时间段的步骤 以下是向LocalDate添加时间段的步骤: 从java.time包导入LocalDate和Period,用于处理日期和时间段。 ... 阅读更多

Java程序:从Instant中减去秒和纳秒

Daniol Thomas
更新于 2019年7月30日 22:30:25

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("After subtracting seconds = "+resSeconds);       Instant resNanoSeconds = now.minusNanos(10000);       System.out.println("After subtracting nanoseconds = "+resNanoSeconds);    } } 输出 1975-11-02T06:42:20.078Z After subtracting seconds = 1975-11-02T06:41:30.078Z After subtracting nanoseconds = 1975-11-02T06:42:20.077990Z

如何在Java中获取两个Instant时间戳之间的秒和分?

Krantik Chavan
更新于 2019年7月30日 22:30:25

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("Seconds between Durations = "+seconds);   ... 阅读更多

如何在Java中获取两个Instant时间戳之间的持续时间?

Krantik Chavan
更新于 2019年7月30日 22:30:25

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

Java程序:从Instant获取毫秒

Krantik Chavan
更新于 2019年7月30日 22:30:25

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

Java程序:从Instant获取纪元秒

Krantik Chavan
更新于 2019年7月30日 22:30:25

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

Java程序:从纪元秒和毫秒创建Instant

Krantik Chavan
更新于 2019年7月30日 22:30:25

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

Java程序:将此持续时间转换为纳秒的总长度

Krantik Chavan
更新于 2019年7月30日 22:30:25

123 次浏览

有了这个,我们可以得到天、小时和分钟的纳秒数。 首先,设置持续时间:Duration d1 = Duration.ofDays(5); Duration d2 = Duration.ofHours(20); Duration d3 = Duration.ofMinutes(15); 将上述持续时间转换为纳秒:System.out.println("Nanoseconds in 5 days = "+d1.toNanos()); System.out.println("Nanoseconds in 20 hours = "+d2.toNanos()); System.out.println("Nanoseconds in 15 minutes = "+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("Nanoseconds in 5 days = "+d1.toNanos());       System.out.println("Nanoseconds in 20 hours = "+d2.toNanos());   ... 阅读更多

广告
© . All rights reserved.